Issue #3080: PyImport_ImportModuleNoBlock() uses Unicode

This commit is contained in:
Victor Stinner 2011-03-13 21:57:27 -04:00
parent 41c5fecce0
commit 2e5f11aaa4
1 changed files with 14 additions and 11 deletions

View File

@ -2591,8 +2591,7 @@ PyImport_ImportModule(const char *name)
PyObject * PyObject *
PyImport_ImportModuleNoBlock(const char *name) PyImport_ImportModuleNoBlock(const char *name)
{ {
PyObject *result; PyObject *nameobj, *modules, *result;
PyObject *modules;
long me; long me;
/* Try to get the module from sys.modules[name] */ /* Try to get the module from sys.modules[name] */
@ -2600,14 +2599,16 @@ PyImport_ImportModuleNoBlock(const char *name)
if (modules == NULL) if (modules == NULL)
return NULL; return NULL;
result = PyDict_GetItemString(modules, name); nameobj = PyUnicode_FromString(name);
if (nameobj == NULL)
return NULL;
result = PyDict_GetItem(modules, nameobj);
if (result != NULL) { if (result != NULL) {
Py_DECREF(nameobj);
Py_INCREF(result); Py_INCREF(result);
return result; return result;
} }
else { PyErr_Clear();
PyErr_Clear();
}
#ifdef WITH_THREAD #ifdef WITH_THREAD
/* check the import lock /* check the import lock
* me might be -1 but I ignore the error here, the lock function * me might be -1 but I ignore the error here, the lock function
@ -2615,18 +2616,20 @@ PyImport_ImportModuleNoBlock(const char *name)
me = PyThread_get_thread_ident(); me = PyThread_get_thread_ident();
if (import_lock_thread == -1 || import_lock_thread == me) { if (import_lock_thread == -1 || import_lock_thread == me) {
/* no thread or me is holding the lock */ /* no thread or me is holding the lock */
return PyImport_ImportModule(name); result = PyImport_Import(nameobj);
} }
else { else {
PyErr_Format(PyExc_ImportError, PyErr_Format(PyExc_ImportError,
"Failed to import %.200s because the import lock" "Failed to import %U because the import lock"
"is held by another thread.", "is held by another thread.",
name); nameobj);
return NULL; result = NULL;
} }
#else #else
return PyImport_ImportModule(name); result = PyImport_Import(nameobj);
#endif #endif
Py_DECREF(nameobj);
return result;
} }
/* Forward declarations for helper routines */ /* Forward declarations for helper routines */