Author: Armin Rigo <ar...@tunes.org>
Branch: py3.5
Changeset: r89904:85b6fb286e66
Date: 2017-02-02 18:19 +0100
http://bitbucket.org/pypy/pypy/changeset/85b6fb286e66/

Log:    Mess with import locks, when giving a .pyc file name on the command-
        line

diff --git a/pypy/interpreter/app_main.py b/pypy/interpreter/app_main.py
--- a/pypy/interpreter/app_main.py
+++ b/pypy/interpreter/app_main.py
@@ -728,8 +728,26 @@
             if IS_WINDOWS:
                 filename = filename.lower()
             if filename.endswith('.pyc') or filename.endswith('.pyo'):
+                # We don't actually load via SourcelessFileLoader
+                # because '__main__' must not be listed inside
+                # 'importlib._bootstrap._module_locks' (it deadlocks
+                # test_multiprocessing_main_handling.test_script_compiled)
+                from importlib._bootstrap_external import MAGIC_NUMBER
+                import marshal
                 loader = SourcelessFileLoader('__main__', filename)
-                args = (loader.load_module, loader.name)
+                mainmodule.__loader__ = loader
+                @hidden_applevel
+                def execfile(filename, namespace):
+                    with open(filename, 'rb') as f:
+                        if f.read(4) != MAGIC_NUMBER:
+                            raise RuntimeError("Bad magic number in .pyc file")
+                        if len(f.read(8)) != 8:
+                            raise RuntimeError("Truncated .pyc file")
+                        co = marshal.load(f)
+                    if type(co) is not type((lambda:0).__code__):
+                        raise RuntimeError("Bad code object in .pyc file")
+                    exec_(co, namespace)
+                args = (execfile, filename, mainmodule.__dict__)
             else:
                 filename = sys.argv[0]
                 for hook in sys.path_hooks:
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to