Mercurial > cpython-lru_cache-weakref
annotate Lib/runpy.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | c0908cebf184 |
| children | 8c2635afbfe1 |
| rev | line source |
|---|---|
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1 """runpy.py - locating and running Python code using the module namespace |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
3 Provides support for locating and running Python scripts using the Python |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
4 module namespace instead of the native filesystem. |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
5 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
6 This allows Python code to play nicely with non-filesystem based PEP 302 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
7 importers when locating support scripts as well as when importing modules. |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
8 """ |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
9 # Written by Nick Coghlan <ncoghlan at gmail.com> |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
10 # to implement PEP 338 (Executing Modules as Scripts) |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
11 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
12 import sys |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
13 import imp |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
14 from pkgutil import read_code |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
15 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
16 from imp import get_loader |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
17 except ImportError: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
18 from pkgutil import get_loader |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
19 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
20 __all__ = [ |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
21 "run_module", "run_path", |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
22 ] |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
23 |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
24 class _TempModule(object): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
25 """Temporarily replace a module in sys.modules with an empty namespace""" |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
26 def __init__(self, mod_name): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
27 self.mod_name = mod_name |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
28 self.module = imp.new_module(mod_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
29 self._saved_module = [] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
30 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
31 def __enter__(self): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
32 mod_name = self.mod_name |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
33 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
34 self._saved_module.append(sys.modules[mod_name]) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
35 except KeyError: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
36 pass |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
37 sys.modules[mod_name] = self.module |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
38 return self |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
39 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
40 def __exit__(self, *args): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
41 if self._saved_module: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
42 sys.modules[self.mod_name] = self._saved_module[0] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
43 else: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
44 del sys.modules[self.mod_name] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
45 self._saved_module = [] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
46 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
47 class _ModifiedArgv0(object): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
48 def __init__(self, value): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
49 self.value = value |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
50 self._saved_value = self._sentinel = object() |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
51 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
52 def __enter__(self): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
53 if self._saved_value is not self._sentinel: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
54 raise RuntimeError("Already preserving saved value") |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
55 self._saved_value = sys.argv[0] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
56 sys.argv[0] = self.value |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
57 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
58 def __exit__(self, *args): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
59 self.value = self._sentinel |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
60 sys.argv[0] = self._saved_value |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
61 |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
62 def _run_code(code, run_globals, init_globals=None, |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
63 mod_name=None, mod_fname=None, |
| 44116 | 64 mod_loader=None, pkg_name=None): |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
65 """Helper to run code in nominated namespace""" |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
66 if init_globals is not None: |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
67 run_globals.update(init_globals) |
|
39022
34cb9a6ce12d
Revert the __module_name__ changes made in rev 47142. We'll revisit this in Python 2.6
Nick Coghlan <ncoghlan@gmail.com>
parents:
38951
diff
changeset
|
68 run_globals.update(__name__ = mod_name, |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
69 __file__ = mod_fname, |
| 44116 | 70 __loader__ = mod_loader, |
| 71 __package__ = pkg_name) | |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
72 exec code in run_globals |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
73 return run_globals |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
74 |
|
39022
34cb9a6ce12d
Revert the __module_name__ changes made in rev 47142. We'll revisit this in Python 2.6
Nick Coghlan <ncoghlan@gmail.com>
parents:
38951
diff
changeset
|
75 def _run_module_code(code, init_globals=None, |
|
42819
1b420e6e00eb
Revert misguided attempt at fixing incompatibility between -m and -i switches (better fix coming soon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
42357
diff
changeset
|
76 mod_name=None, mod_fname=None, |
| 44116 | 77 mod_loader=None, pkg_name=None): |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
78 """Helper to run code in new namespace with sys modified""" |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
79 with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
80 mod_globals = temp_module.module.__dict__ |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
81 _run_code(code, mod_globals, init_globals, |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
82 mod_name, mod_fname, mod_loader, pkg_name) |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
83 # Copy the globals of the temporary module, as they |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
84 # may be cleared when the temporary module goes away |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
85 return mod_globals.copy() |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
86 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
87 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
88 # This helper is needed due to a missing component in the PEP 302 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
89 # loader protocol (specifically, "get_filename" is non-standard) |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
90 # Since we can't introduce new features in maintenance releases, |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
91 # support was added to zipimporter under the name '_get_filename' |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
92 def _get_filename(loader, mod_name): |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
93 for attr in ("get_filename", "_get_filename"): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
94 meth = getattr(loader, attr, None) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
95 if meth is not None: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
96 return meth(mod_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
97 return None |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
98 |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
99 # Helper to get the loader, code and filename for a module |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
100 def _get_module_details(mod_name): |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
101 loader = get_loader(mod_name) |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
102 if loader is None: |
|
42349
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
103 raise ImportError("No module named %s" % mod_name) |
|
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
104 if loader.is_package(mod_name): |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
105 if mod_name == "__main__" or mod_name.endswith(".__main__"): |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
106 raise ImportError("Cannot use package as __main__ module") |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
107 try: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
108 pkg_main_name = mod_name + ".__main__" |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
109 return _get_module_details(pkg_main_name) |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
110 except ImportError, e: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
111 raise ImportError(("%s; %r is a package and cannot " + |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
112 "be directly executed") %(e, mod_name)) |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
113 code = loader.get_code(mod_name) |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
114 if code is None: |
|
42349
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
115 raise ImportError("No code object available for %s" % mod_name) |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
116 filename = _get_filename(loader, mod_name) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
117 return mod_name, loader, code, filename |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
118 |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
119 |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
120 def _get_main_module_details(): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
121 # Helper that gives a nicer error message when attempting to |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
122 # execute a zipfile or directory by invoking __main__.py |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
123 main_name = "__main__" |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
124 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
125 return _get_module_details(main_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
126 except ImportError as exc: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
127 if main_name in str(exc): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
128 raise ImportError("can't find %r module in %r" % |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
129 (main_name, sys.path[0])) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
130 raise |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
131 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
132 # This function is the actual implementation of the -m switch and direct |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
133 # execution of zipfiles and directories and is deliberately kept private. |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
134 # This avoids a repeat of the situation where run_module() no longer met the |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
135 # needs of mainmodule.c, but couldn't be changed because it was public |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
136 def _run_module_as_main(mod_name, alter_argv=True): |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
137 """Runs the designated module in the __main__ namespace |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
138 |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
139 Note that the executed module will have full access to the |
|
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
140 __main__ namespace. If this is not desirable, the run_module() |
|
57825
04588eff25a1
Fix comment typo.
R. David Murray <rdmurray@bitdance.com>
parents:
57255
diff
changeset
|
141 function should be used to run the module code in a fresh namespace. |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
142 |
|
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
143 At the very least, these variables in __main__ will be overwritten: |
|
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
144 __name__ |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
145 __file__ |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
146 __loader__ |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
147 __package__ |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
148 """ |
|
45428
b887dba2b5e1
Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery)
Nick Coghlan <ncoghlan@gmail.com>
parents:
44116
diff
changeset
|
149 try: |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
150 if alter_argv or mod_name != "__main__": # i.e. -m switch |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
151 mod_name, loader, code, fname = _get_module_details(mod_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
152 else: # i.e. directory or zipfile execution |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
153 mod_name, loader, code, fname = _get_main_module_details() |
|
45428
b887dba2b5e1
Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery)
Nick Coghlan <ncoghlan@gmail.com>
parents:
44116
diff
changeset
|
154 except ImportError as exc: |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
155 msg = "%s: %s" % (sys.executable, str(exc)) |
|
45428
b887dba2b5e1
Try to make command line error messages from runpy easier to understand (and suppress traceback cruft from the implicitly invoked runpy machinery)
Nick Coghlan <ncoghlan@gmail.com>
parents:
44116
diff
changeset
|
156 sys.exit(msg) |
| 44116 | 157 pkg_name = mod_name.rpartition('.')[0] |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
158 main_globals = sys.modules["__main__"].__dict__ |
|
57133
42d4cf827b9e
Some minor cleanups to private runpy code and docstrings
Nick Coghlan <ncoghlan@gmail.com>
parents:
51450
diff
changeset
|
159 if alter_argv: |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
160 sys.argv[0] = fname |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
161 return _run_code(code, main_globals, None, |
| 44116 | 162 "__main__", fname, loader, pkg_name) |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
163 |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
164 def run_module(mod_name, init_globals=None, |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
165 run_name=None, alter_sys=False): |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
166 """Execute a module's code without importing it |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
167 |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
168 Returns the resulting top level namespace dictionary |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
169 """ |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
170 mod_name, loader, code, fname = _get_module_details(mod_name) |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
171 if run_name is None: |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
172 run_name = mod_name |
| 44116 | 173 pkg_name = mod_name.rpartition('.')[0] |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
174 if alter_sys: |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
175 return _run_module_code(code, init_globals, run_name, |
| 44116 | 176 fname, loader, pkg_name) |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
177 else: |
|
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
178 # Leave the sys module alone |
| 44116 | 179 return _run_code(code, {}, init_globals, run_name, |
| 180 fname, loader, pkg_name) | |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
181 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
182 |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
183 # XXX (ncoghlan): Perhaps expose the C API function |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
184 # as imp.get_importer instead of reimplementing it in Python? |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
185 def _get_importer(path_name): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
186 """Python version of PyImport_GetImporter C API function""" |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
187 cache = sys.path_importer_cache |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
188 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
189 importer = cache[path_name] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
190 except KeyError: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
191 # Not yet cached. Flag as using the |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
192 # standard machinery until we finish |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
193 # checking the hooks |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
194 cache[path_name] = None |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
195 for hook in sys.path_hooks: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
196 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
197 importer = hook(path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
198 break |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
199 except ImportError: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
200 pass |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
201 else: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
202 # The following check looks a bit odd. The trick is that |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
203 # NullImporter throws ImportError if the supplied path is a |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
204 # *valid* directory entry (and hence able to be handled |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
205 # by the standard import machinery) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
206 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
207 importer = imp.NullImporter(path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
208 except ImportError: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
209 return None |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
210 cache[path_name] = importer |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
211 return importer |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
212 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
213 def _get_code_from_file(fname): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
214 # Check for a compiled file first |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
215 with open(fname, "rb") as f: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
216 code = read_code(f) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
217 if code is None: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
218 # That didn't work, so try it as normal source code |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
219 with open(fname, "rU") as f: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
220 code = compile(f.read(), fname, 'exec') |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
221 return code |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
222 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
223 def run_path(path_name, init_globals=None, run_name=None): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
224 """Execute code located at the specified filesystem location |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
225 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
226 Returns the resulting top level namespace dictionary |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
227 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
228 The file path may refer directly to a Python script (i.e. |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
229 one that could be directly executed with execfile) or else |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
230 it may refer to a zipfile or directory containing a top |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
231 level __main__.py script. |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
232 """ |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
233 if run_name is None: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
234 run_name = "<run_path>" |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
235 importer = _get_importer(path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
236 if isinstance(importer, imp.NullImporter): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
237 # Not a valid sys.path entry, so run the code directly |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
238 # execfile() doesn't help as we want to allow compiled files |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
239 code = _get_code_from_file(path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
240 return _run_module_code(code, init_globals, run_name, path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
241 else: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
242 # Importer is defined for path, so add it to |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
243 # the start of sys.path |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
244 sys.path.insert(0, path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
245 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
246 # Here's where things are a little different from the run_module |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
247 # case. There, we only had to replace the module in sys while the |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
248 # code was running and doing so was somewhat optional. Here, we |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
249 # have no choice and we have to remove it even while we read the |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
250 # code. If we don't do this, a __loader__ attribute in the |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
251 # existing __main__ module may prevent location of the new module. |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
252 main_name = "__main__" |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
253 saved_main = sys.modules[main_name] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
254 del sys.modules[main_name] |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
255 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
256 mod_name, loader, code, fname = _get_main_module_details() |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
257 finally: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
258 sys.modules[main_name] = saved_main |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
259 pkg_name = "" |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
260 with _TempModule(run_name) as temp_module, \ |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
261 _ModifiedArgv0(path_name): |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
262 mod_globals = temp_module.module.__dict__ |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
263 return _run_code(code, mod_globals, init_globals, |
|
65238
c0908cebf184
Merged revisions 85398 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57825
diff
changeset
|
264 run_name, fname, loader, pkg_name).copy() |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
265 finally: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
266 try: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
267 sys.path.remove(path_name) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
268 except ValueError: |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
269 pass |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
270 |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
57133
diff
changeset
|
271 |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
272 if __name__ == "__main__": |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
273 # Run the module specified as the next command line argument |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
274 if len(sys.argv) < 2: |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
275 print >> sys.stderr, "No module specified for execution" |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
276 else: |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
277 del sys.argv[0] # Make the requested module sys.argv[0] |
|
42825
543f2ba97fab
Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents:
42819
diff
changeset
|
278 _run_module_as_main(sys.argv[0]) |
