]> granicus.if.org Git - python/commitdiff
Add a new function imp.lock_held(), and use it to skip test_threaded_import
authorTim Peters <tim.peters@gmail.com>
Thu, 30 Aug 2001 05:16:13 +0000 (05:16 +0000)
committerTim Peters <tim.peters@gmail.com>
Thu, 30 Aug 2001 05:16:13 +0000 (05:16 +0000)
when that test is doomed to deadlock.

Doc/lib/libimp.tex
Lib/test/test_threaded_import.py
Misc/NEWS
Python/import.c

index 23391ffde36d49005747f1de80d6b715b36cd423..a96839fe7aeeae00ca6c12120317e1f7db192f49 100644 (file)
@@ -93,6 +93,19 @@ Return a new empty module object called \var{name}.  This object is
 \emph{not} inserted in \code{sys.modules}.
 \end{funcdesc}
 
+\begin{funcdesc}{lock_held}{}
+Return 1 if the import lock is currently held, else 0.
+On platforms without threads, always return 0.
+
+On platforms with threads, a thread executing an import holds an internal
+lock until the import is complete.
+This lock blocks other threads from doing an import until the original
+import completes, which in turn prevents other threads from seeing
+incomplete module objects constructed by the original thread while in
+the process of completing its import (and the imports, if any,
+triggered by that).
+\end{funcdesc}
+
 The following constants with integer values, defined in this module,
 are used to indicate the search result of \function{find_module()}.
 
index 1e1c55321f9260c575e0b33d1d6a93e08d2dca52..e022c5fe924118de39498fe0e2c0d6becfd0b566 100644 (file)
@@ -32,10 +32,12 @@ def task():
 
 def test_main():        # magic name!  see above
     global N, done
-    import sys
-    for modname in sys.modules:
-        if modname.find('autotest') >= 0:
-            raise TestSkipped("can't run from autotest")
+
+    import imp
+    if imp.lock_held():
+        # This triggers on, e.g., from test import autotest.
+        raise TestSkipped("can't run when import lock is held")
+
     done.acquire()
     for N in (20, 50) * 3:
         if verbose:
index 35737acc11593f6078a19a5d263936c5dde69472..d660df8e31f7e4d4a77ae0d4424f24e370b9fa94 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -8,6 +8,9 @@ Core
 
 Library
 
++ A new function, imp.lock_held(), returns 1 when the import lock is
+  currently held.  See the docs for the imp module.
+
 + pickle, cPickle and marshal on 32-bit platforms can now correctly read
   dumps containing ints written on platforms where Python ints are 8 bytes.
   When read on a box where Python ints are 4 bytes, such values are
@@ -17,6 +20,12 @@ Tools
 
 Build
 
+API
+
++ XXX Say something about Neil's GC rework, and that extensions that
+  don't upgrade to the new scheme will still compile but not actually
+  participate in GC.
+
 New platforms
 
 Tests
index 5c2a9adee4fb7c93d489c51b8e7704e293668ffd..52ad85e99e3a0bda3c091b02c224c02583f9c9ab 100644 (file)
@@ -181,6 +181,18 @@ unlock_import(void)
 
 #endif
 
+static PyObject *
+imp_lock_held(PyObject *self, PyObject *args)
+{
+       if (!PyArg_ParseTuple(args, ":lock_held"))
+               return NULL;
+#ifdef WITH_THREAD
+       return PyInt_FromLong(import_lock_thread != -1);
+#else
+       return PyInt_FromLong(0);
+#endif
+}
+
 /* Helper for sys */
 
 PyObject *
@@ -2339,12 +2351,19 @@ Create a new module.  Do not enter it in sys.modules.\n\
 The module name must include the full package name, if any.\
 ";
 
+static char doc_lock_held[] = "\
+lock_held() -> 0 or 1\n\
+Return 1 if the import lock is currently held.\n\
+On platforms without threads, return 0.\
+";
+
 static PyMethodDef imp_methods[] = {
        {"find_module",         imp_find_module,        1, doc_find_module},
        {"get_magic",           imp_get_magic,          1, doc_get_magic},
        {"get_suffixes",        imp_get_suffixes,       1, doc_get_suffixes},
        {"load_module",         imp_load_module,        1, doc_load_module},
        {"new_module",          imp_new_module,         1, doc_new_module},
+       {"lock_held",           imp_lock_held,          1, doc_lock_held},
        /* The rest are obsolete */
        {"get_frozen_object",   imp_get_frozen_object,  1},
        {"init_builtin",        imp_init_builtin,       1},