From 49d3f2514b4edcabae3b08286016c98b584bb3a2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 17 Oct 2010 01:24:53 +0000 Subject: _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 --- Include/import.h | 6 ++++-- Modules/signalmodule.c | 2 +- Python/import.c | 44 ++++++++++++++++++++++++++++++++++---------- Python/importdl.c | 34 ++++++++++++++++++++++++---------- Python/pythonrun.c | 8 ++++---- 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/Include/import.h b/Include/import.h index 3311581..3e81e78 100644 --- a/Include/import.h +++ b/Include/import.h @@ -40,8 +40,10 @@ PyAPI_FUNC(int) _PyImport_ReleaseLock(void); PyAPI_FUNC(void) _PyImport_ReInitLock(void); -PyAPI_FUNC(PyObject *)_PyImport_FindExtension(char *, char *); -PyAPI_FUNC(int)_PyImport_FixupExtension(PyObject*, char *, char *); +PyAPI_FUNC(PyObject *)_PyImport_FindBuiltin(char *); +PyAPI_FUNC(PyObject *)_PyImport_FindExtensionUnicode(char *, PyObject *); +PyAPI_FUNC(int)_PyImport_FixupBuiltin(PyObject*, char *); +PyAPI_FUNC(int)_PyImport_FixupExtensionUnicode(PyObject*, char *, PyObject *); struct _inittab { char *name; diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index d34e177..4691285 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -951,7 +951,7 @@ PyOS_InitInterrupts(void) { PyObject *m = PyInit_signal(); if (m) { - _PyImport_FixupExtension(m, "signal", "signal"); + _PyImport_FixupBuiltin(m, "signal"); Py_DECREF(m); } } diff --git a/Python/import.c b/Python/import.c index 5a09c97..6715dc9 100644 --- a/Python/import.c +++ b/Python/import.c @@ -121,7 +121,7 @@ typedef unsigned short mode_t; static long pyc_magic = MAGIC; static const char *pyc_tag = TAG; -/* See _PyImport_FixupExtension() below */ +/* See _PyImport_FixupExtensionUnicode() below */ static PyObject *extensions = NULL; /* This table is defined in config.c: */ @@ -561,10 +561,10 @@ PyImport_GetMagicTag(void) once, we keep a static dictionary 'extensions' keyed by module name (for built-in modules) or by filename (for dynamically loaded modules), containing these modules. A copy of the module's - dictionary is stored by calling _PyImport_FixupExtension() + dictionary is stored by calling _PyImport_FixupExtensionUnicode() immediately after the module initialization function succeeds. A copy can be retrieved from there by calling - _PyImport_FindExtension(). + _PyImport_FindExtensionUnicode(). Modules which do support multiple initialization set their m_size field to a non-negative number (indicating the size of the @@ -573,7 +573,7 @@ PyImport_GetMagicTag(void) */ int -_PyImport_FixupExtension(PyObject *mod, char *name, char *filename) +_PyImport_FixupExtensionUnicode(PyObject *mod, char *name, PyObject *filename) { PyObject *modules, *dict; struct PyModuleDef *def; @@ -613,18 +613,31 @@ _PyImport_FixupExtension(PyObject *mod, char *name, char *filename) if (def->m_base.m_copy == NULL) return -1; } - PyDict_SetItemString(extensions, filename, (PyObject*)def); + PyDict_SetItem(extensions, filename, (PyObject*)def); return 0; } +int +_PyImport_FixupBuiltin(PyObject *mod, char *name) +{ + int res; + PyObject *filename; + filename = PyUnicode_FromString(name); + if (filename == NULL) + return -1; + res = _PyImport_FixupExtensionUnicode(mod, name, filename); + Py_DECREF(filename); + return res; +} + PyObject * -_PyImport_FindExtension(char *name, char *filename) +_PyImport_FindExtensionUnicode(char *name, PyObject *filename) { PyObject *mod, *mdict; PyModuleDef* def; if (extensions == NULL) return NULL; - def = (PyModuleDef*)PyDict_GetItemString(extensions, filename); + def = (PyModuleDef*)PyDict_GetItem(extensions, filename); if (def == NULL) return NULL; if (def->m_size == -1) { @@ -655,12 +668,23 @@ _PyImport_FindExtension(char *name, char *filename) return NULL; } if (Py_VerboseFlag) - PySys_WriteStderr("import %s # previously loaded (%s)\n", + PySys_FormatStderr("import %s # previously loaded (%U)\n", name, filename); return mod; } +PyObject * +_PyImport_FindBuiltin(char *name) +{ + PyObject *res, *filename; + filename = PyUnicode_FromString(name); + if (filename == NULL) + return NULL; + res = _PyImport_FindExtensionUnicode(name, filename); + Py_DECREF(filename); + return res; +} /* Get the module object corresponding to a module name. First check the modules dictionary if there's one there, @@ -2121,7 +2145,7 @@ init_builtin(char *name) { struct _inittab *p; - if (_PyImport_FindExtension(name, name) != NULL) + if (_PyImport_FindBuiltin(name) != NULL) return 1; for (p = PyImport_Inittab; p->name != NULL; p++) { @@ -2138,7 +2162,7 @@ init_builtin(char *name) mod = (*p->initfunc)(); if (mod == 0) return -1; - if (_PyImport_FixupExtension(mod, name, name) < 0) + if (_PyImport_FixupBuiltin(mod, name) < 0) return -1; /* FixupExtension has put the module into sys.modules, so we can release our own reference. */ diff --git a/Python/importdl.c b/Python/importdl.c index 507222b..9caed45 100644 --- a/Python/importdl.c +++ b/Python/importdl.c @@ -27,10 +27,16 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp) dl_funcptr p0; PyObject* (*p)(void); struct PyModuleDef *def; + PyObject *result; - if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { + path = PyUnicode_DecodeFSDefault(pathname); + if (path == NULL) + return NULL; + + if ((m = _PyImport_FindExtensionUnicode(name, path)) != NULL) { Py_INCREF(m); - return m; + result = m; + goto finally; } lastdot = strrchr(name, '.'); if (lastdot == NULL) { @@ -45,26 +51,26 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp) p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp); p = (PyObject*(*)(void))p0; if (PyErr_Occurred()) - return NULL; + goto error; if (p == NULL) { PyErr_Format(PyExc_ImportError, "dynamic module does not define init function (PyInit_%.200s)", shortname); - return NULL; + goto error; } oldcontext = _Py_PackageContext; _Py_PackageContext = packagecontext; m = (*p)(); _Py_PackageContext = oldcontext; if (m == NULL) - return NULL; + goto error; if (PyErr_Occurred()) { Py_DECREF(m); PyErr_Format(PyExc_SystemError, "initialization of %s raised unreported exception", shortname); - return NULL; + goto error; } /* Remember pointer to module init function. */ @@ -72,17 +78,25 @@ _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp) def->m_base.m_init = p; /* Remember the filename as the __file__ attribute */ - path = PyUnicode_DecodeFSDefault(pathname); if (PyModule_AddObject(m, "__file__", path) < 0) PyErr_Clear(); /* Not important enough to report */ + else + Py_INCREF(path); - if (_PyImport_FixupExtension(m, name, pathname) < 0) - return NULL; + if (_PyImport_FixupExtensionUnicode(m, name, path) < 0) + goto error; if (Py_VerboseFlag) PySys_WriteStderr( "import %s # dynamically loaded from %s\n", name, pathname); - return m; + result = m; + goto finally; + +error: + result = NULL; +finally: + Py_DECREF(path); + return result; } #endif /* HAVE_DYNAMIC_LOADING */ diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f72c9d7..fe99fd2 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -255,7 +255,7 @@ Py_InitializeEx(int install_sigs) bimod = _PyBuiltin_Init(); if (bimod == NULL) Py_FatalError("Py_Initialize: can't initialize builtins modules"); - _PyImport_FixupExtension(bimod, "builtins", "builtins"); + _PyImport_FixupBuiltin(bimod, "builtins"); interp->builtins = PyModule_GetDict(bimod); if (interp->builtins == NULL) Py_FatalError("Py_Initialize: can't initialize builtins dict"); @@ -271,7 +271,7 @@ Py_InitializeEx(int install_sigs) if (interp->sysdict == NULL) Py_FatalError("Py_Initialize: can't initialize sys dict"); Py_INCREF(interp->sysdict); - _PyImport_FixupExtension(sysmod, "sys", "sys"); + _PyImport_FixupBuiltin(sysmod, "sys"); PySys_SetPath(Py_GetPath()); PyDict_SetItemString(interp->sysdict, "modules", interp->modules); @@ -577,7 +577,7 @@ Py_NewInterpreter(void) interp->modules = PyDict_New(); interp->modules_reloading = PyDict_New(); - bimod = _PyImport_FindExtension("builtins", "builtins"); + bimod = _PyImport_FindBuiltin("builtins"); if (bimod != NULL) { interp->builtins = PyModule_GetDict(bimod); if (interp->builtins == NULL) @@ -588,7 +588,7 @@ Py_NewInterpreter(void) /* initialize builtin exceptions */ _PyExc_Init(); - sysmod = _PyImport_FindExtension("sys", "sys"); + sysmod = _PyImport_FindBuiltin("sys"); if (bimod != NULL && sysmod != NULL) { PyObject *pstderr; interp->sysdict = PyModule_GetDict(sysmod); -- cgit v0.12