diff Python/import.c @ 66599:09ed411a1090

Issue #9573: os.fork now works when triggered as a side effect of import (the wisdom of actually relying on this remains questionable!)
author Nick Coghlan <ncoghlan@gmail.com>
date Thu, 02 Dec 2010 04:11:46 +0000
parents 724920a015f7
children 523a6fc53a70
line wrap: on
line diff
--- a/Python/import.c	Thu Dec 02 02:58:07 2010 +0000
+++ b/Python/import.c	Thu Dec 02 04:11:46 2010 +0000
@@ -325,8 +325,17 @@
 {
     if (import_lock != NULL)
         import_lock = PyThread_allocate_lock();
-    import_lock_thread = -1;
-    import_lock_level = 0;
+    if (import_lock_level > 1) {
+        /* Forked as a side effect of import */
+        long me = PyThread_get_thread_ident();
+        PyThread_acquire_lock(import_lock, 0);
+	/* XXX: can the previous line fail? */
+        import_lock_thread = me;
+        import_lock_level--;
+    } else {
+        import_lock_thread = -1;
+        import_lock_level = 0;
+    }
 }
 
 #endif