[Python-checkins] python/dist/src/Python import.c,2.192.6.2,2.192.6.3
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Thu, 13 Feb 2003 09:01:35 -0800
Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv3041
Modified Files:
Tag: release22-maint
import.c
Log Message:
Backport 2.217 and 2.218:
Provide access to the import lock, fixing SF bug #580952. This is
mostly from SF patch #683257, but I had to change unlock_import() to
return an error value to avoid fatal error.
Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.192.6.2
retrieving revision 2.192.6.3
diff -C2 -d -r2.192.6.2 -r2.192.6.3
*** import.c 1 Jun 2002 18:26:22 -0000 2.192.6.2
--- import.c 13 Feb 2003 17:01:28 -0000 2.192.6.3
***************
*** 162,166 ****
return;
}
! if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) {
PyThreadState *tstate = PyEval_SaveThread();
PyThread_acquire_lock(import_lock, 1);
--- 162,167 ----
return;
}
! if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
! {
PyThreadState *tstate = PyEval_SaveThread();
PyThread_acquire_lock(import_lock, 1);
***************
*** 171,182 ****
}
! static void
unlock_import(void)
{
long me = PyThread_get_thread_ident();
if (me == -1)
! return; /* Too bad */
if (import_lock_thread != me)
! Py_FatalError("unlock_import: not holding the import lock");
import_lock_level--;
if (import_lock_level == 0) {
--- 172,183 ----
}
! static int
unlock_import(void)
{
long me = PyThread_get_thread_ident();
if (me == -1)
! return 0; /* Too bad */
if (import_lock_thread != me)
! return -1;
import_lock_level--;
if (import_lock_level == 0) {
***************
*** 184,187 ****
--- 185,189 ----
PyThread_release_lock(import_lock);
}
+ return 1;
}
***************
*** 189,193 ****
#define lock_import()
! #define unlock_import()
#endif
--- 191,195 ----
#define lock_import()
! #define unlock_import() 0
#endif
***************
*** 205,208 ****
--- 207,238 ----
}
+ static PyObject *
+ imp_acquire_lock(PyObject *self, PyObject *args)
+ {
+ if (!PyArg_ParseTuple(args, ":acquire_lock"))
+ return NULL;
+ #ifdef WITH_THREAD
+ lock_import();
+ #endif
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ static PyObject *
+ imp_release_lock(PyObject *self, PyObject *args)
+ {
+ if (!PyArg_ParseTuple(args, ":release_lock"))
+ return NULL;
+ #ifdef WITH_THREAD
+ if (unlock_import() < 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "not holding the import lock");
+ return NULL;
+ }
+ #endif
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
/* Helper for sys */
***************
*** 1657,1661 ****
lock_import();
result = import_module_ex(name, globals, locals, fromlist);
! unlock_import();
return result;
}
--- 1687,1696 ----
lock_import();
result = import_module_ex(name, globals, locals, fromlist);
! if (unlock_import() < 0) {
! Py_XDECREF(result);
! PyErr_SetString(PyExc_RuntimeError,
! "not holding the import lock");
! return NULL;
! }
return result;
}
***************
*** 2427,2430 ****
--- 2462,2477 ----
";
+ static char doc_acquire_lock[] = "\
+ acquire_lock() -> None\n\
+ Acquires the interpreter's import lock for the current thread.\n\
+ This lock should be used by import hooks to ensure thread-safety\n\
+ when importing modules.\n\
+ On platforms without threads, this function does nothing.";
+
+ static char doc_release_lock[] = "\
+ release_lock() -> None\n\
+ Release the interpreter's import lock.\n\
+ On platforms without threads, this function does nothing.";
+
static PyMethodDef imp_methods[] = {
{"find_module", imp_find_module, 1, doc_find_module},
***************
*** 2434,2437 ****
--- 2481,2486 ----
{"new_module", imp_new_module, 1, doc_new_module},
{"lock_held", imp_lock_held, 1, doc_lock_held},
+ {"acquire_lock", imp_acquire_lock, 1, doc_acquire_lock},
+ {"release_lock", imp_release_lock, 1, doc_release_lock},
/* The rest are obsolete */
{"get_frozen_object", imp_get_frozen_object, 1},