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

Skip to content

Commit 49d3f25

Browse files
author
Victor Stinner
committed
_PyImport_FixupExtension() and _PyImport_FindExtension() uses FS encoding
* Rename _PyImport_FindExtension() to _PyImport_FindExtensionUnicode(): the filename becomes a Unicode object instead of byte string * Rename _PyImport_FixupExtension() to _PyImport_FixupExtensionUnicode(): the filename becomes a Unicode object instead of byte string
1 parent 3e85dfd commit 49d3f25

5 files changed

Lines changed: 67 additions & 27 deletions

File tree

Include/import.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ PyAPI_FUNC(int) _PyImport_ReleaseLock(void);
4040

4141
PyAPI_FUNC(void) _PyImport_ReInitLock(void);
4242

43-
PyAPI_FUNC(PyObject *)_PyImport_FindExtension(char *, char *);
44-
PyAPI_FUNC(int)_PyImport_FixupExtension(PyObject*, char *, char *);
43+
PyAPI_FUNC(PyObject *)_PyImport_FindBuiltin(char *);
44+
PyAPI_FUNC(PyObject *)_PyImport_FindExtensionUnicode(char *, PyObject *);
45+
PyAPI_FUNC(int)_PyImport_FixupBuiltin(PyObject*, char *);
46+
PyAPI_FUNC(int)_PyImport_FixupExtensionUnicode(PyObject*, char *, PyObject *);
4547

4648
struct _inittab {
4749
char *name;

Modules/signalmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ PyOS_InitInterrupts(void)
951951
{
952952
PyObject *m = PyInit_signal();
953953
if (m) {
954-
_PyImport_FixupExtension(m, "signal", "signal");
954+
_PyImport_FixupBuiltin(m, "signal");
955955
Py_DECREF(m);
956956
}
957957
}

Python/import.c

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ typedef unsigned short mode_t;
121121
static long pyc_magic = MAGIC;
122122
static const char *pyc_tag = TAG;
123123

124-
/* See _PyImport_FixupExtension() below */
124+
/* See _PyImport_FixupExtensionUnicode() below */
125125
static PyObject *extensions = NULL;
126126

127127
/* This table is defined in config.c: */
@@ -561,10 +561,10 @@ PyImport_GetMagicTag(void)
561561
once, we keep a static dictionary 'extensions' keyed by module name
562562
(for built-in modules) or by filename (for dynamically loaded
563563
modules), containing these modules. A copy of the module's
564-
dictionary is stored by calling _PyImport_FixupExtension()
564+
dictionary is stored by calling _PyImport_FixupExtensionUnicode()
565565
immediately after the module initialization function succeeds. A
566566
copy can be retrieved from there by calling
567-
_PyImport_FindExtension().
567+
_PyImport_FindExtensionUnicode().
568568
569569
Modules which do support multiple initialization set their m_size
570570
field to a non-negative number (indicating the size of the
@@ -573,7 +573,7 @@ PyImport_GetMagicTag(void)
573573
*/
574574

575575
int
576-
_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
576+
_PyImport_FixupExtensionUnicode(PyObject *mod, char *name, PyObject *filename)
577577
{
578578
PyObject *modules, *dict;
579579
struct PyModuleDef *def;
@@ -613,18 +613,31 @@ _PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
613613
if (def->m_base.m_copy == NULL)
614614
return -1;
615615
}
616-
PyDict_SetItemString(extensions, filename, (PyObject*)def);
616+
PyDict_SetItem(extensions, filename, (PyObject*)def);
617617
return 0;
618618
}
619619

620+
int
621+
_PyImport_FixupBuiltin(PyObject *mod, char *name)
622+
{
623+
int res;
624+
PyObject *filename;
625+
filename = PyUnicode_FromString(name);
626+
if (filename == NULL)
627+
return -1;
628+
res = _PyImport_FixupExtensionUnicode(mod, name, filename);
629+
Py_DECREF(filename);
630+
return res;
631+
}
632+
620633
PyObject *
621-
_PyImport_FindExtension(char *name, char *filename)
634+
_PyImport_FindExtensionUnicode(char *name, PyObject *filename)
622635
{
623636
PyObject *mod, *mdict;
624637
PyModuleDef* def;
625638
if (extensions == NULL)
626639
return NULL;
627-
def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
640+
def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
628641
if (def == NULL)
629642
return NULL;
630643
if (def->m_size == -1) {
@@ -655,12 +668,23 @@ _PyImport_FindExtension(char *name, char *filename)
655668
return NULL;
656669
}
657670
if (Py_VerboseFlag)
658-
PySys_WriteStderr("import %s # previously loaded (%s)\n",
671+
PySys_FormatStderr("import %s # previously loaded (%U)\n",
659672
name, filename);
660673
return mod;
661674

662675
}
663676

677+
PyObject *
678+
_PyImport_FindBuiltin(char *name)
679+
{
680+
PyObject *res, *filename;
681+
filename = PyUnicode_FromString(name);
682+
if (filename == NULL)
683+
return NULL;
684+
res = _PyImport_FindExtensionUnicode(name, filename);
685+
Py_DECREF(filename);
686+
return res;
687+
}
664688

665689
/* Get the module object corresponding to a module name.
666690
First check the modules dictionary if there's one there,
@@ -2121,7 +2145,7 @@ init_builtin(char *name)
21212145
{
21222146
struct _inittab *p;
21232147

2124-
if (_PyImport_FindExtension(name, name) != NULL)
2148+
if (_PyImport_FindBuiltin(name) != NULL)
21252149
return 1;
21262150

21272151
for (p = PyImport_Inittab; p->name != NULL; p++) {
@@ -2138,7 +2162,7 @@ init_builtin(char *name)
21382162
mod = (*p->initfunc)();
21392163
if (mod == 0)
21402164
return -1;
2141-
if (_PyImport_FixupExtension(mod, name, name) < 0)
2165+
if (_PyImport_FixupBuiltin(mod, name) < 0)
21422166
return -1;
21432167
/* FixupExtension has put the module into sys.modules,
21442168
so we can release our own reference. */

Python/importdl.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
2727
dl_funcptr p0;
2828
PyObject* (*p)(void);
2929
struct PyModuleDef *def;
30+
PyObject *result;
3031

31-
if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
32+
path = PyUnicode_DecodeFSDefault(pathname);
33+
if (path == NULL)
34+
return NULL;
35+
36+
if ((m = _PyImport_FindExtensionUnicode(name, path)) != NULL) {
3237
Py_INCREF(m);
33-
return m;
38+
result = m;
39+
goto finally;
3440
}
3541
lastdot = strrchr(name, '.');
3642
if (lastdot == NULL) {
@@ -45,44 +51,52 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
4551
p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
4652
p = (PyObject*(*)(void))p0;
4753
if (PyErr_Occurred())
48-
return NULL;
54+
goto error;
4955
if (p == NULL) {
5056
PyErr_Format(PyExc_ImportError,
5157
"dynamic module does not define init function (PyInit_%.200s)",
5258
shortname);
53-
return NULL;
59+
goto error;
5460
}
5561
oldcontext = _Py_PackageContext;
5662
_Py_PackageContext = packagecontext;
5763
m = (*p)();
5864
_Py_PackageContext = oldcontext;
5965
if (m == NULL)
60-
return NULL;
66+
goto error;
6167

6268
if (PyErr_Occurred()) {
6369
Py_DECREF(m);
6470
PyErr_Format(PyExc_SystemError,
6571
"initialization of %s raised unreported exception",
6672
shortname);
67-
return NULL;
73+
goto error;
6874
}
6975

7076
/* Remember pointer to module init function. */
7177
def = PyModule_GetDef(m);
7278
def->m_base.m_init = p;
7379

7480
/* Remember the filename as the __file__ attribute */
75-
path = PyUnicode_DecodeFSDefault(pathname);
7681
if (PyModule_AddObject(m, "__file__", path) < 0)
7782
PyErr_Clear(); /* Not important enough to report */
83+
else
84+
Py_INCREF(path);
7885

79-
if (_PyImport_FixupExtension(m, name, pathname) < 0)
80-
return NULL;
86+
if (_PyImport_FixupExtensionUnicode(m, name, path) < 0)
87+
goto error;
8188
if (Py_VerboseFlag)
8289
PySys_WriteStderr(
8390
"import %s # dynamically loaded from %s\n",
8491
name, pathname);
85-
return m;
92+
result = m;
93+
goto finally;
94+
95+
error:
96+
result = NULL;
97+
finally:
98+
Py_DECREF(path);
99+
return result;
86100
}
87101

88102
#endif /* HAVE_DYNAMIC_LOADING */

Python/pythonrun.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ Py_InitializeEx(int install_sigs)
255255
bimod = _PyBuiltin_Init();
256256
if (bimod == NULL)
257257
Py_FatalError("Py_Initialize: can't initialize builtins modules");
258-
_PyImport_FixupExtension(bimod, "builtins", "builtins");
258+
_PyImport_FixupBuiltin(bimod, "builtins");
259259
interp->builtins = PyModule_GetDict(bimod);
260260
if (interp->builtins == NULL)
261261
Py_FatalError("Py_Initialize: can't initialize builtins dict");
@@ -271,7 +271,7 @@ Py_InitializeEx(int install_sigs)
271271
if (interp->sysdict == NULL)
272272
Py_FatalError("Py_Initialize: can't initialize sys dict");
273273
Py_INCREF(interp->sysdict);
274-
_PyImport_FixupExtension(sysmod, "sys", "sys");
274+
_PyImport_FixupBuiltin(sysmod, "sys");
275275
PySys_SetPath(Py_GetPath());
276276
PyDict_SetItemString(interp->sysdict, "modules",
277277
interp->modules);
@@ -577,7 +577,7 @@ Py_NewInterpreter(void)
577577
interp->modules = PyDict_New();
578578
interp->modules_reloading = PyDict_New();
579579

580-
bimod = _PyImport_FindExtension("builtins", "builtins");
580+
bimod = _PyImport_FindBuiltin("builtins");
581581
if (bimod != NULL) {
582582
interp->builtins = PyModule_GetDict(bimod);
583583
if (interp->builtins == NULL)
@@ -588,7 +588,7 @@ Py_NewInterpreter(void)
588588
/* initialize builtin exceptions */
589589
_PyExc_Init();
590590

591-
sysmod = _PyImport_FindExtension("sys", "sys");
591+
sysmod = _PyImport_FindBuiltin("sys");
592592
if (bimod != NULL && sysmod != NULL) {
593593
PyObject *pstderr;
594594
interp->sysdict = PyModule_GetDict(sysmod);

0 commit comments

Comments
 (0)