Mercurial > cpython-lru_cache-weakref
annotate Lib/runpy.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 4264cf00a75c |
| children | 5cce99aa1f58 |
| 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 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
14 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
15 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
|
16 except ImportError: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
17 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
|
18 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
19 __all__ = [ |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
20 "run_module", |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
21 ] |
|
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 |
|
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
|
24 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
|
25 mod_name=None, mod_fname=None, |
| 44116 | 26 mod_loader=None, pkg_name=None): |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
27 """Helper for _run_module_code""" |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 __file__ = mod_fname, |
| 44116 | 32 __loader__ = mod_loader, |
| 33 __package__ = pkg_name) | |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
34 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
|
35 return run_globals |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
36 |
|
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
|
37 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
|
38 mod_name=None, mod_fname=None, |
| 44116 | 39 mod_loader=None, pkg_name=None): |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
40 """Helper for run_module""" |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
41 # Set up the top level namespace dictionary |
|
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
|
42 temp_module = imp.new_module(mod_name) |
|
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
|
43 mod_globals = temp_module.__dict__ |
|
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
|
44 # Modify sys.argv[0] and sys.module[mod_name] |
|
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
|
45 saved_argv0 = sys.argv[0] |
|
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
|
46 restore_module = mod_name in sys.modules |
|
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
|
47 if restore_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
|
48 saved_module = sys.modules[mod_name] |
|
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
|
49 sys.argv[0] = mod_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
|
50 sys.modules[mod_name] = temp_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
|
51 try: |
|
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
|
52 _run_code(code, mod_globals, init_globals, |
| 44116 | 53 mod_name, mod_fname, |
| 54 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
|
55 finally: |
|
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
|
56 sys.argv[0] = saved_argv0 |
|
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
|
57 if restore_module: |
|
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
|
58 sys.modules[mod_name] = saved_module |
|
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
|
59 else: |
|
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
|
60 del sys.modules[mod_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
|
61 # 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
|
62 # 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
|
63 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
|
64 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
65 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
66 # 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
|
67 # 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
|
68 # 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
|
69 # 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
|
70 def _get_filename(loader, mod_name): |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
71 for attr in ("get_filename", "_get_filename"): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
72 meth = getattr(loader, attr, None) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
73 if meth is not None: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
74 return meth(mod_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45428
diff
changeset
|
75 return None |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37205
diff
changeset
|
76 |
|
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
|
77 # 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
|
78 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
|
79 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
|
80 if loader is None: |
|
42349
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
81 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
|
82 if loader.is_package(mod_name): |
|
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
83 raise ImportError(("%s is a package and cannot " + |
|
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
84 "be directly executed") % mod_name) |
|
36995
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
85 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
|
86 if code is None: |
|
42349
3fe643d62ecc
Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents:
39022
diff
changeset
|
87 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
|
88 filename = _get_filename(loader, mod_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
|
89 return loader, code, filename |
|
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
|
90 |
|
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
|
91 |
|
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
|
92 # XXX ncoghlan: Should this be documented and made public? |
|
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
|
93 # (Current thoughts: don't repeat the mistake that lead to its |
|
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
|
94 # creation when run_module() no longer met the needs of |
|
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
|
95 # mainmodule.c, but couldn't be changed because it was public) |
|
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
|
96 def _run_module_as_main(mod_name, set_argv0=True): |
|
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
|
97 """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
|
98 |
|
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 These __*__ magic variables will be overwritten: |
|
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 __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
|
101 __loader__ |
|
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
|
102 """ |
|
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
|
103 try: |
|
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
|
104 loader, code, fname = _get_module_details(mod_name) |
|
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
|
105 except ImportError as exc: |
|
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
|
106 # Try to provide a good error message |
|
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
|
107 # for directories, zip files and the -m switch |
|
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
|
108 if set_argv0: |
|
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
|
109 # For -m switch, just disply the exception |
|
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
|
110 info = str(exc) |
|
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
|
111 else: |
|
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
|
112 # For directories/zipfiles, let the user |
|
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
|
113 # know what the code was looking for |
|
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
|
114 info = "can't find '__main__.py' in %r" % sys.argv[0] |
|
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
|
115 msg = "%s: %s" % (sys.executable, info) |
|
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
|
116 sys.exit(msg) |
| 44116 | 117 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
|
118 main_globals = sys.modules["__main__"].__dict__ |
|
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 if set_argv0: |
|
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
|
120 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
|
121 return _run_code(code, main_globals, None, |
| 44116 | 122 "__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
|
123 |
|
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
|
124 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
|
125 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
|
126 """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
|
127 |
|
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
|
128 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
|
129 """ |
|
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
|
130 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
|
131 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
|
132 run_name = mod_name |
| 44116 | 133 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
|
134 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
|
135 return _run_module_code(code, init_globals, run_name, |
| 44116 | 136 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
|
137 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
|
138 # Leave the sys module alone |
| 44116 | 139 return _run_code(code, {}, init_globals, run_name, |
| 140 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
|
141 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
142 |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
143 if __name__ == "__main__": |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
144 # 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
|
145 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
|
146 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
|
147 else: |
|
f914630f419c
Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
148 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
|
149 _run_module_as_main(sys.argv[0]) |
