Index: errors.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/errors.c,v retrieving revision 2.75 diff -u -r2.75 errors.c --- errors.c 11 Dec 2002 14:04:59 -0000 2.75 +++ errors.c 15 Feb 2003 04:47:08 -0000 @@ -601,21 +601,26 @@ } +extern int PyImport_LockImport(int wait); +extern void PyImport_UnlockImport(void); + /* Function to issue a warning message; may raise an exception. */ int PyErr_Warn(PyObject *category, char *message) { PyObject *mod, *dict, *func = NULL; + int got_lock = PyImport_LockImport(0); - mod = PyImport_ImportModule("warnings"); - if (mod != NULL) { - dict = PyModule_GetDict(mod); - func = PyDict_GetItemString(dict, "warn"); - Py_DECREF(mod); + if (got_lock) { + mod = PyImport_ImportModule("warnings"); + if (mod != NULL) { + dict = PyModule_GetDict(mod); + func = PyDict_GetItemString(dict, "warn"); + Py_DECREF(mod); + } } if (func == NULL) { PySys_WriteStderr("warning: %s\n", message); - return 0; } else { PyObject *args, *res; @@ -630,8 +635,10 @@ if (res == NULL) return -1; Py_DECREF(res); - return 0; } + if (got_lock) + PyImport_UnlockImport(); + return 0; } Index: import.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/import.c,v retrieving revision 2.218 diff -u -r2.218 import.c --- import.c 12 Feb 2003 23:02:19 -0000 2.218 +++ import.c 15 Feb 2003 04:47:09 -0000 @@ -240,26 +240,42 @@ static long import_lock_thread = -1; static int import_lock_level = 0; -static void -lock_import(void) +static int +do_lock_import(int wait) { long me = PyThread_get_thread_ident(); if (me == -1) - return; /* Too bad */ + return 0; /* Too bad */ if (import_lock == NULL) import_lock = PyThread_allocate_lock(); if (import_lock_thread == me) { import_lock_level++; - return; + return 1; } if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) { - PyThreadState *tstate = PyEval_SaveThread(); - PyThread_acquire_lock(import_lock, 1); - PyEval_RestoreThread(tstate); + if (!wait) + return 0; + else { + PyThreadState *tstate = PyEval_SaveThread(); + PyThread_acquire_lock(import_lock, 1); + PyEval_RestoreThread(tstate); + } } import_lock_thread = me; import_lock_level = 1; + return 1; +} + +static void +lock_import(void) +{ + do_lock_import(1); +} + +int PyImport_LockImport(int wait) +{ + return do_lock_import(wait); } static int @@ -283,7 +299,16 @@ #define lock_import() #define unlock_import() 0 +int PyImport_LockImport(int) +{ + return 1; +} #endif + +void PyImport_UnlockImport(void) +{ + unlock_import(); +} static PyObject * imp_lock_held(PyObject *self, PyObject *args)