Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(30748)

Side by Side Diff: Lib/runpy.py

Issue 14285: Traceback wrong on ImportError while executing a package
Patch Set: Created 3 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/using/cmdline.rst ('k') | Lib/test/test_cmd_line_script.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """runpy.py - locating and running Python code using the module namespace 1 """runpy.py - locating and running Python code using the module namespace
2 2
3 Provides support for locating and running Python scripts using the Python 3 Provides support for locating and running Python scripts using the Python
4 module namespace instead of the native filesystem. 4 module namespace instead of the native filesystem.
5 5
6 This allows Python code to play nicely with non-filesystem based PEP 302 6 This allows Python code to play nicely with non-filesystem based PEP 302
7 importers when locating support scripts as well as when importing modules. 7 importers when locating support scripts as well as when importing modules.
8 """ 8 """
9 # Written by Nick Coghlan <ncoghlan at gmail.com> 9 # Written by Nick Coghlan <ncoghlan at gmail.com>
10 # to implement PEP 338 (Executing Modules as Scripts) 10 # to implement PEP 338 (Executing Modules as Scripts)
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname): 93 with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname):
94 mod_globals = temp_module.module.__dict__ 94 mod_globals = temp_module.module.__dict__
95 _run_code(code, mod_globals, init_globals, 95 _run_code(code, mod_globals, init_globals,
96 mod_name, mod_spec, pkg_name, script_name) 96 mod_name, mod_spec, pkg_name, script_name)
97 # Copy the globals of the temporary module, as they 97 # Copy the globals of the temporary module, as they
98 # may be cleared when the temporary module goes away 98 # may be cleared when the temporary module goes away
99 return mod_globals.copy() 99 return mod_globals.copy()
100 100
101 # Helper to get the loader, code and filename for a module 101 # Helper to get the loader, code and filename for a module
102 def _get_module_details(mod_name, error=ImportError): 102 def _get_module_details(mod_name, error=ImportError):
103 if mod_name.startswith("."):
104 raise error("Relative module names not supported")
105 pkg_name, _, _ = mod_name.rpartition(".")
106 if pkg_name:
107 # Try importing the parent to avoid catching initialization errors
108 try:
109 __import__(pkg_name)
110 except ImportError as err:
111 # If the parent or higher ancestor package is missing, this is an
112 # error that we may want to catch. But do not allow other errors
113 # to be caught.
114 if err.name is None or (err.name != pkg_name and
115 not pkg_name.startswith(err.name + ".")):
116 raise
117 raise error(format(err))
118
103 try: 119 try:
104 spec = importlib.util.find_spec(mod_name) 120 spec = importlib.util.find_spec(mod_name)
105 except (ImportError, AttributeError, TypeError, ValueError) as ex: 121 except (ImportError, AttributeError, TypeError, ValueError) as ex:
106 # This hack fixes an impedance mismatch between pkgutil and 122 # This hack fixes an impedance mismatch between pkgutil and
107 # importlib, where the latter raises other errors for cases where 123 # importlib, where the latter raises other errors for cases where
108 # pkgutil previously raised ImportError 124 # pkgutil previously raised ImportError
109 msg = "Error while finding spec for {!r} ({}: {})" 125 msg = "Error while finding spec for {!r} ({}: {})"
110 raise error(msg.format(mod_name, type(ex), ex)) from ex 126 raise error(msg.format(mod_name, type(ex).__name__, ex)) from ex
111 if spec is None: 127 if spec is None:
112 raise error("No module named %s" % mod_name) 128 raise error("No module named %s" % mod_name)
113 if spec.submodule_search_locations is not None: 129 if spec.submodule_search_locations is not None:
114 if mod_name == "__main__" or mod_name.endswith(".__main__"): 130 if mod_name == "__main__" or mod_name.endswith(".__main__"):
115 raise error("Cannot use package as __main__ module") 131 raise error("Cannot use package as __main__ module")
116 __import__(mod_name) # Do not catch exceptions initializing package
117 try: 132 try:
118 pkg_main_name = mod_name + ".__main__" 133 pkg_main_name = mod_name + ".__main__"
119 return _get_module_details(pkg_main_name) 134 return _get_module_details(pkg_main_name, error)
120 except ImportError as e: 135 except error as e:
121 raise error(("%s; %r is a package and cannot " + 136 raise error(("%s; %r is a package and cannot " +
122 "be directly executed") %(e, mod_name)) 137 "be directly executed") %(e, mod_name))
123 loader = spec.loader 138 loader = spec.loader
124 if loader is None: 139 if loader is None:
125 raise error("%r is a namespace package and cannot be executed" 140 raise error("%r is a namespace package and cannot be executed"
126 % mod_name) 141 % mod_name)
127 try: 142 try:
128 code = loader.get_code(mod_name) 143 code = loader.get_code(mod_name)
129 except ImportError as e: 144 except ImportError as e:
130 raise error(format(e)) from e 145 raise error(format(e)) from e
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 pass 275 pass
261 276
262 277
263 if __name__ == "__main__": 278 if __name__ == "__main__":
264 # Run the module specified as the next command line argument 279 # Run the module specified as the next command line argument
265 if len(sys.argv) < 2: 280 if len(sys.argv) < 2:
266 print("No module specified for execution", file=sys.stderr) 281 print("No module specified for execution", file=sys.stderr)
267 else: 282 else:
268 del sys.argv[0] # Make the requested module sys.argv[0] 283 del sys.argv[0] # Make the requested module sys.argv[0]
269 _run_module_as_main(sys.argv[0]) 284 _run_module_as_main(sys.argv[0])
OLDNEW
« no previous file with comments | « Doc/using/cmdline.rst ('k') | Lib/test/test_cmd_line_script.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+