summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-06-07 15:45:41 (GMT)
committerBrett Cannon <brett@python.org>2013-06-07 15:45:41 (GMT)
commita33e11e436de15baa1f0feb9d2d60d28f137eb3d (patch)
treea03723a9a142ec9e278262617145c87067ef17e4 /Lib
parent22c039bf50151a30563d368afd33ab12eabea4cb (diff)
downloadcpython-a33e11e436de15baa1f0feb9d2d60d28f137eb3d.zip
cpython-a33e11e436de15baa1f0feb9d2d60d28f137eb3d.tar.gz
cpython-a33e11e436de15baa1f0feb9d2d60d28f137eb3d.tar.bz2
Issue #17314: Stop using imp in multiprocessing.forking and move over
to importlib.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/multiprocessing/forking.py22
1 files changed, 12 insertions, 10 deletions
diff --git a/Lib/multiprocessing/forking.py b/Lib/multiprocessing/forking.py
index 37c9a10..54c073d 100644
--- a/Lib/multiprocessing/forking.py
+++ b/Lib/multiprocessing/forking.py
@@ -450,6 +450,7 @@ def prepare(data):
# Main modules not actually called __main__.py may
# contain additional code that should still be executed
import imp
+ import importlib
if main_path is None:
dirs = None
@@ -460,16 +461,17 @@ def prepare(data):
assert main_name not in sys.modules, main_name
sys.modules.pop('__mp_main__', None)
- file, path_name, etc = imp.find_module(main_name, dirs)
+ # We should not try to load __main__
+ # since that would execute 'if __name__ == "__main__"'
+ # clauses, potentially causing a psuedo fork bomb.
+ loader = importlib.find_loader(main_name, path=dirs)
+ main_module = imp.new_module(main_name)
try:
- # We should not do 'imp.load_module("__main__", ...)'
- # since that would execute 'if __name__ == "__main__"'
- # clauses, potentially causing a psuedo fork bomb.
- main_module = imp.load_module(
- '__mp_main__', file, path_name, etc
- )
- finally:
- if file:
- file.close()
+ loader.init_module_attrs(main_module)
+ except AttributeError: # init_module_attrs is optional
+ pass
+ main_module.__name__ = '__mp_main__'
+ code = loader.get_code(main_name)
+ exec(code, main_module.__dict__)
sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module