Message189967
So I think I have come up with a way to expose a new method that makes this use-case doable and in a sane manner. Richard, let me know what you think so that I know that this makes sense before I commit myself to the new method (init_module_attrs())::
--- a/Lib/multiprocessing/forking.py Fri May 24 13:51:21 2013 +0200
+++ b/Lib/multiprocessing/forking.py Fri May 24 08:06:17 2013 -0400
@@ -449,7 +449,7 @@
elif main_name != 'ipython':
# 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 +460,17 @@
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:
+ 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 |
|
Date |
User |
Action |
Args |
2013-05-25 15:36:10 | brett.cannon | set | recipients:
+ brett.cannon, sbt |
2013-05-25 15:36:10 | brett.cannon | set | messageid: <1369496170.6.0.434489745172.issue17314@psf.upfronthosting.co.za> |
2013-05-25 15:36:10 | brett.cannon | link | issue17314 messages |
2013-05-25 15:36:09 | brett.cannon | create | |
|