Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08654e1

Browse files
author
Victor Stinner
committed
zipimport: get_module_code() returns modpath as a Unicode object
... instead of a char*. Encode the module path to the fileystem encoding (for PyImport_ExecCodeModuleEx) instead of utf-8.
1 parent 26fabe1 commit 08654e1

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

Modules/zipimport.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static PyObject *zip_directory_cache = NULL;
4949
static PyObject *read_directory(PyObject *archive);
5050
static PyObject *get_data(PyObject *archive, PyObject *toc_entry);
5151
static PyObject *get_module_code(ZipImporter *self, char *fullname,
52-
int *p_ispackage, char **p_modpath);
52+
int *p_ispackage, PyObject **p_modpath);
5353

5454

5555
#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
@@ -310,7 +310,8 @@ zipimporter_load_module(PyObject *obj, PyObject *args)
310310
{
311311
ZipImporter *self = (ZipImporter *)obj;
312312
PyObject *code = NULL, *mod, *dict;
313-
char *fullname, *modpath;
313+
char *fullname;
314+
PyObject *modpath = NULL, *modpath_bytes;
314315
int ispackage;
315316

316317
if (!PyArg_ParseTuple(args, "s:zipimporter.load_module",
@@ -352,17 +353,24 @@ zipimporter_load_module(PyObject *obj, PyObject *args)
352353
if (err != 0)
353354
goto error;
354355
}
355-
mod = PyImport_ExecCodeModuleEx(fullname, code, modpath);
356+
modpath_bytes = PyUnicode_EncodeFSDefault(modpath);
357+
if (modpath_bytes == NULL)
358+
goto error;
359+
mod = PyImport_ExecCodeModuleEx(fullname, code,
360+
PyBytes_AS_STRING(modpath_bytes));
361+
Py_DECREF(modpath_bytes);
356362
Py_CLEAR(code);
357363
if (mod == NULL)
358364
goto error;
359365

360366
if (Py_VerboseFlag)
361-
PySys_WriteStderr("import %s # loaded from Zip %s\n",
362-
fullname, modpath);
367+
PySys_FormatStderr("import %s # loaded from Zip %U\n",
368+
fullname, modpath);
369+
Py_DECREF(modpath);
363370
return mod;
364371
error:
365372
Py_XDECREF(code);
373+
Py_XDECREF(modpath);
366374
return NULL;
367375
}
368376

@@ -372,7 +380,8 @@ zipimporter_get_filename(PyObject *obj, PyObject *args)
372380
{
373381
ZipImporter *self = (ZipImporter *)obj;
374382
PyObject *code;
375-
char *fullname, *modpath;
383+
char *fullname;
384+
PyObject *modpath;
376385
int ispackage;
377386

378387
if (!PyArg_ParseTuple(args, "s:zipimporter.get_filename",
@@ -386,7 +395,7 @@ zipimporter_get_filename(PyObject *obj, PyObject *args)
386395
return NULL;
387396
Py_DECREF(code); /* Only need the path info */
388397

389-
return PyUnicode_FromString(modpath);
398+
return modpath;
390399
}
391400

392401
/* Return a bool signifying whether the module is a package or not. */
@@ -685,7 +694,8 @@ get_long(unsigned char *buf) {
685694
686695
A toc_entry is a tuple:
687696
688-
(__file__, # value to use for __file__, available for all files
697+
(__file__, # value to use for __file__, available for all files,
698+
# encoded to the filesystem encoding
689699
compress, # compression kind; 0 for uncompressed
690700
data_size, # size of compressed data on disk
691701
file_size, # size of decompressed data
@@ -1146,7 +1156,7 @@ get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
11461156
'fullname'. */
11471157
static PyObject *
11481158
get_module_code(ZipImporter *self, char *fullname,
1149-
int *p_ispackage, char **p_modpath)
1159+
int *p_ispackage, PyObject **p_modpath)
11501160
{
11511161
PyObject *toc_entry;
11521162
char *subname, path[MAXPATHLEN + 1];
@@ -1185,9 +1195,10 @@ get_module_code(ZipImporter *self, char *fullname,
11851195
Py_DECREF(code);
11861196
continue;
11871197
}
1188-
if (code != NULL && p_modpath != NULL)
1189-
*p_modpath = _PyUnicode_AsString(
1190-
PyTuple_GetItem(toc_entry, 0));
1198+
if (code != NULL && p_modpath != NULL) {
1199+
*p_modpath = PyTuple_GetItem(toc_entry, 0);
1200+
Py_INCREF(*p_modpath);
1201+
}
11911202
return code;
11921203
}
11931204
}

0 commit comments

Comments
 (0)