Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Added a new API function PyImport_ImportModuleNoBlock. It solves the problem with dead locks when mixing threads and imports
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index 18af288..5362584 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -370,7 +370,7 @@
if (context == NULL)
context = PyString_FromString("_ctypes.DllGetClassObject");
- mod = PyImport_ImportModule("ctypes");
+ mod = PyImport_ImportModuleNoBlock("ctypes");
if (!mod) {
PyErr_WriteUnraisable(context ? context : Py_None);
/* There has been a warning before about this already */
@@ -449,7 +449,7 @@
if (context == NULL)
context = PyString_FromString("_ctypes.DllCanUnloadNow");
- mod = PyImport_ImportModule("ctypes");
+ mod = PyImport_ImportModuleNoBlock("ctypes");
if (!mod) {
/* OutputDebugString("Could not import ctypes"); */
/* We assume that this error can only occur when shutting
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index e7c4bc1..a805b2d 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -2255,7 +2255,7 @@
update_lines_cols(void)
{
PyObject *o;
- PyObject *m = PyImport_ImportModule("curses");
+ PyObject *m = PyImport_ImportModuleNoBlock("curses");
if (!m)
return 0;
diff --git a/Modules/cjkcodecs/cjkcodecs.h b/Modules/cjkcodecs/cjkcodecs.h
index 71c54f0..4005bcf 100644
--- a/Modules/cjkcodecs/cjkcodecs.h
+++ b/Modules/cjkcodecs/cjkcodecs.h
@@ -245,7 +245,7 @@
static PyObject *cofunc = NULL;
if (cofunc == NULL) {
- PyObject *mod = PyImport_ImportModule("_multibytecodec");
+ PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
if (mod == NULL)
return NULL;
cofunc = PyObject_GetAttrString(mod, "__create_codec");
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index b94812b..83e547e 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -1305,7 +1305,7 @@
if (_PyString_Resize(&newfmt, usednew) < 0)
goto Done;
{
- PyObject *time = PyImport_ImportModule("time");
+ PyObject *time = PyImport_ImportModuleNoBlock("time");
if (time == NULL)
goto Done;
result = PyObject_CallMethod(time, "strftime", "OO",
@@ -1353,7 +1353,7 @@
time_time(void)
{
PyObject *result = NULL;
- PyObject *time = PyImport_ImportModule("time");
+ PyObject *time = PyImport_ImportModuleNoBlock("time");
if (time != NULL) {
result = PyObject_CallMethod(time, "time", "()");
@@ -1371,7 +1371,7 @@
PyObject *time;
PyObject *result = NULL;
- time = PyImport_ImportModule("time");
+ time = PyImport_ImportModuleNoBlock("time");
if (time != NULL) {
result = PyObject_CallMethod(time, "struct_time",
"((iiiiiiiii))",
@@ -3827,7 +3827,7 @@
if (!PyArg_ParseTuple(args, "ss:strptime", &string, &format))
return NULL;
- if ((module = PyImport_ImportModule("time")) == NULL)
+ if ((module = PyImport_ImportModuleNoBlock("time")) == NULL)
return NULL;
obj = PyObject_CallMethod(module, "strptime", "ss", string, format);
Py_DECREF(module);
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 0edf523..522cc89 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1236,7 +1236,7 @@
* the import and triggers an assertion.
*/
if (tmod == NULL) {
- tmod = PyImport_ImportModule("time");
+ tmod = PyImport_ImportModuleNoBlock("time");
if (tmod == NULL)
PyErr_Clear();
}
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index 6c4a6ad..5764c24 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -3269,7 +3269,7 @@
* If this fails, the import of this module will fail because an
* exception will be raised here; should we clear the exception?
*/
- copyreg = PyImport_ImportModule("copy_reg");
+ copyreg = PyImport_ImportModuleNoBlock("copy_reg");
if (copyreg != NULL) {
PyObject *func, *pickler;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 85725f4..696df0d 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -5651,7 +5651,7 @@
return posix_error();
if (struct_rusage == NULL) {
- PyObject *m = PyImport_ImportModule("resource");
+ PyObject *m = PyImport_ImportModuleNoBlock("resource");
if (m == NULL)
return NULL;
struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h
index a4382ab..fb9ec67 100644
--- a/Modules/socketmodule.h
+++ b/Modules/socketmodule.h
@@ -222,7 +222,7 @@
void *api;
DPRINTF("Importing the %s C API...\n", apimodule);
- mod = PyImport_ImportModule(apimodule);
+ mod = PyImport_ImportModuleNoBlock(apimodule);
if (mod == NULL)
goto onError;
DPRINTF(" %s package found\n", apimodule);
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 842ce94..5b4d210 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -515,7 +515,7 @@
static PyObject *
time_strptime(PyObject *self, PyObject *args)
{
- PyObject *strptime_module = PyImport_ImportModule("_strptime");
+ PyObject *strptime_module = PyImport_ImportModuleNoBlock("_strptime");
PyObject *strptime_result;
if (!strptime_module)
@@ -627,7 +627,7 @@
{
PyObject* m;
- m = PyImport_ImportModule("time");
+ m = PyImport_ImportModuleNoBlock("time");
if (m == NULL) {
return NULL;
}
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
index fe5e1b0..dec6091 100644
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -776,7 +776,7 @@
let's avoid a stack overflow. */
return NULL;
importing_zlib = 1;
- zlib = PyImport_ImportModule("zlib"); /* import zlib */
+ zlib = PyImport_ImportModuleNoBlock("zlib");
importing_zlib = 0;
if (zlib != NULL) {
decompress = PyObject_GetAttrString(zlib,