Use unicode (and bytes as appropriate)

This commit is contained in:
Neal Norwitz 2007-08-26 05:08:21 +00:00
parent 8abd2e6c6e
commit 3c814d2550
1 changed files with 27 additions and 24 deletions

View File

@ -144,11 +144,11 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
} }
} }
self->archive = PyString_FromString(buf); self->archive = PyUnicode_FromString(buf);
if (self->archive == NULL) if (self->archive == NULL)
return -1; return -1;
self->prefix = PyString_FromString(prefix); self->prefix = PyUnicode_FromString(prefix);
if (self->prefix == NULL) if (self->prefix == NULL)
return -1; return -1;
@ -180,10 +180,10 @@ zipimporter_repr(ZipImporter *self)
char *archive = "???"; char *archive = "???";
char *prefix = ""; char *prefix = "";
if (self->archive != NULL && PyString_Check(self->archive)) if (self->archive != NULL && PyUnicode_Check(self->archive))
archive = PyString_AsString(self->archive); archive = PyUnicode_AsString(self->archive);
if (self->prefix != NULL && PyString_Check(self->prefix)) if (self->prefix != NULL && PyUnicode_Check(self->prefix))
prefix = PyString_AsString(self->prefix); prefix = PyUnicode_AsString(self->prefix);
if (prefix != NULL && *prefix) if (prefix != NULL && *prefix)
return PyUnicode_FromFormat("<zipimporter object \"%.300s%c%.150s\">", return PyUnicode_FromFormat("<zipimporter object \"%.300s%c%.150s\">",
archive, SEP, prefix); archive, SEP, prefix);
@ -249,7 +249,7 @@ get_module_info(ZipImporter *self, char *fullname)
subname = get_subname(fullname); subname = get_subname(fullname);
len = make_filename(PyString_AsString(self->prefix), subname, path); len = make_filename(PyUnicode_AsString(self->prefix), subname, path);
if (len < 0) if (len < 0)
return MI_ERROR; return MI_ERROR;
@ -322,12 +322,12 @@ zipimporter_load_module(PyObject *obj, PyObject *args)
/* add __path__ to the module *before* the code gets /* add __path__ to the module *before* the code gets
executed */ executed */
PyObject *pkgpath, *fullpath; PyObject *pkgpath, *fullpath;
char *prefix = PyString_AsString(self->prefix); char *prefix = PyUnicode_AsString(self->prefix);
char *subname = get_subname(fullname); char *subname = get_subname(fullname);
int err; int err;
fullpath = PyString_FromFormat("%s%c%s%s", fullpath = PyUnicode_FromFormat("%s%c%s%s",
PyString_AsString(self->archive), PyUnicode_AsString(self->archive),
SEP, SEP,
*prefix ? prefix : "", *prefix ? prefix : "",
subname); subname);
@ -404,9 +404,9 @@ zipimporter_get_data(PyObject *obj, PyObject *args)
} }
path = buf; path = buf;
#endif #endif
len = PyString_Size(self->archive); len = PyUnicode_GET_SIZE(self->archive);
if ((size_t)len < strlen(path) && if ((size_t)len < strlen(path) &&
strncmp(path, PyString_AsString(self->archive), len) == 0 && strncmp(path, PyUnicode_AsString(self->archive), len) == 0 &&
path[len] == SEP) { path[len] == SEP) {
path = path + len + 1; path = path + len + 1;
} }
@ -416,7 +416,7 @@ zipimporter_get_data(PyObject *obj, PyObject *args)
PyErr_SetFromErrnoWithFilename(PyExc_IOError, path); PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
return NULL; return NULL;
} }
return get_data(PyString_AsString(self->archive), toc_entry); return get_data(PyUnicode_AsString(self->archive), toc_entry);
} }
static PyObject * static PyObject *
@ -453,7 +453,7 @@ zipimporter_get_source(PyObject *obj, PyObject *args)
} }
subname = get_subname(fullname); subname = get_subname(fullname);
len = make_filename(PyString_AsString(self->prefix), subname, path); len = make_filename(PyUnicode_AsString(self->prefix), subname, path);
if (len < 0) if (len < 0)
return NULL; return NULL;
@ -466,7 +466,7 @@ zipimporter_get_source(PyObject *obj, PyObject *args)
toc_entry = PyDict_GetItemString(self->files, path); toc_entry = PyDict_GetItemString(self->files, path);
if (toc_entry != NULL) { if (toc_entry != NULL) {
PyObject *bytes = get_data(PyString_AsString(self->archive), toc_entry); PyObject *bytes = get_data(PyUnicode_AsString(self->archive), toc_entry);
PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes)); PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes));
Py_XDECREF(bytes); Py_XDECREF(bytes);
return res; return res;
@ -794,7 +794,7 @@ get_data(char *archive, PyObject *toc_entry)
Py_ssize_t bytes_read = 0; Py_ssize_t bytes_read = 0;
long l; long l;
char *datapath; char *datapath;
long compress, data_size, file_size, file_offset; long compress, data_size, file_size, file_offset, bytes_size;
long time, date, crc; long time, date, crc;
if (!PyArg_ParseTuple(toc_entry, "slllllll", &datapath, &compress, if (!PyArg_ParseTuple(toc_entry, "slllllll", &datapath, &compress,
@ -826,13 +826,16 @@ get_data(char *archive, PyObject *toc_entry)
PyMarshal_ReadShortFromFile(fp); /* local header size */ PyMarshal_ReadShortFromFile(fp); /* local header size */
file_offset += l; /* Start of file data */ file_offset += l; /* Start of file data */
raw_data = PyString_FromStringAndSize((char *)NULL, compress == 0 ? bytes_size = compress == 0 ? data_size : data_size + 1;
data_size : data_size + 1); if (bytes_size == 0)
bytes_size++;
raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
if (raw_data == NULL) { if (raw_data == NULL) {
fclose(fp); fclose(fp);
return NULL; return NULL;
} }
buf = PyString_AsString(raw_data); buf = PyBytes_AsString(raw_data);
err = fseek(fp, file_offset, 0); err = fseek(fp, file_offset, 0);
if (err == 0) if (err == 0)
@ -1041,7 +1044,7 @@ get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
{ {
PyObject *data, *code; PyObject *data, *code;
char *modpath; char *modpath;
char *archive = PyString_AsString(self->archive); char *archive = PyUnicode_AsString(self->archive);
if (archive == NULL) if (archive == NULL)
return NULL; return NULL;
@ -1050,7 +1053,7 @@ get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
if (data == NULL) if (data == NULL)
return NULL; return NULL;
modpath = PyString_AsString(PyTuple_GetItem(toc_entry, 0)); modpath = PyUnicode_AsString(PyTuple_GetItem(toc_entry, 0));
if (isbytecode) { if (isbytecode) {
code = unmarshal_code(modpath, data, mtime); code = unmarshal_code(modpath, data, mtime);
@ -1075,7 +1078,7 @@ get_module_code(ZipImporter *self, char *fullname,
subname = get_subname(fullname); subname = get_subname(fullname);
len = make_filename(PyString_AsString(self->prefix), subname, path); len = make_filename(PyUnicode_AsString(self->prefix), subname, path);
if (len < 0) if (len < 0)
return NULL; return NULL;
@ -1085,7 +1088,7 @@ get_module_code(ZipImporter *self, char *fullname,
strcpy(path + len, zso->suffix); strcpy(path + len, zso->suffix);
if (Py_VerboseFlag > 1) if (Py_VerboseFlag > 1)
PySys_WriteStderr("# trying %s%c%s\n", PySys_WriteStderr("# trying %s%c%s\n",
PyString_AsString(self->archive), PyUnicode_AsString(self->archive),
SEP, path); SEP, path);
toc_entry = PyDict_GetItemString(self->files, path); toc_entry = PyDict_GetItemString(self->files, path);
if (toc_entry != NULL) { if (toc_entry != NULL) {
@ -1107,7 +1110,7 @@ get_module_code(ZipImporter *self, char *fullname,
continue; continue;
} }
if (code != NULL && p_modpath != NULL) if (code != NULL && p_modpath != NULL)
*p_modpath = PyString_AsString( *p_modpath = PyUnicode_AsString(
PyTuple_GetItem(toc_entry, 0)); PyTuple_GetItem(toc_entry, 0));
return code; return code;
} }