]> granicus.if.org Git - python/commitdiff
Close #10128: don't rerun __main__.py in multiprocessing
authorNick Coghlan <ncoghlan@gmail.com>
Thu, 19 Nov 2015 02:59:39 +0000 (12:59 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Thu, 19 Nov 2015 02:59:39 +0000 (12:59 +1000)
- backports issue #10845's mitigation of incompatibilities between
  the multiprocessing module and directory and zipfile execution
- Multiprocessing on Windows will now automatically skip rerunning top
  level __main__.py modules in spawned processes, rather than failing
  with AssertionError

Lib/multiprocessing/forking.py
Misc/NEWS

index 6bddfb74ff502a6677ce267780145f3abcf80bc6..d393817bb34db1ebd5e11d8cec89b3a681037cdb 100644 (file)
@@ -470,12 +470,26 @@ def prepare(data):
         process.ORIGINAL_DIR = data['orig_dir']
 
     if 'main_path' in data:
+        # XXX (ncoghlan): The following code makes several bogus
+        # assumptions regarding the relationship between __file__
+        # and a module's real name. See PEP 302 and issue #10845
+        # The problem is resolved properly in Python 3.4+, as
+        # described in issue #19946
+
         main_path = data['main_path']
         main_name = os.path.splitext(os.path.basename(main_path))[0]
         if main_name == '__init__':
             main_name = os.path.basename(os.path.dirname(main_path))
 
-        if main_name != 'ipython':
+        if main_name == '__main__':
+            # For directory and zipfile execution, we assume an implicit
+            # "if __name__ == '__main__':" around the module, and don't
+            # rerun the main module code in spawned processes
+            main_module = sys.modules['__main__']
+            main_module.__file__ = main_path
+        elif main_name != 'ipython':
+            # Main modules not actually called __main__.py may
+            # contain additional code that should still be executed
             import imp
 
             if main_path is None:
index ec443917c7f750c37161104fb8508898148f625d..1075daaa4082b7a8e847b84a0b03590d3d2ed750 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -55,6 +55,11 @@ Core and Builtins
 Library
 -------
 
+- Issue #10128: backport issue #10845's mitigation of incompatibilities between
+  the multiprocessing module and directory and zipfile execution.
+  Multiprocessing on Windows will now automatically skip rerunning __main__ in
+  spawned processes, rather than failing with AssertionError.
+
 - Issue #25578: Fix (another) memory leak in SSLSocket.getpeercer().
 
 - Issue #25590: In the Readline completer, only call getattr() once per