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

Delta Between Two Patch Sets: Lib/runpy.py

Issue 14285: Traceback wrong on ImportError while executing a package
Left Patch Set: Created 3 years, 9 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/using/cmdline.rst ('k') | Lib/test/test_cmd_line_script.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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")
103 pkg_name, _, _ = mod_name.rpartition(".") 105 pkg_name, _, _ = mod_name.rpartition(".")
104 if pkg_name and not mod_name.startswith("."): 106 if pkg_name:
105 # Try importing the parent to avoid catching initialization errors 107 # Try importing the parent to avoid catching initialization errors
106 try: 108 try:
107 __import__(pkg_name) 109 __import__(pkg_name)
108 except ImportError as err: 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.
109 if err.name is None or (err.name != pkg_name and 114 if err.name is None or (err.name != pkg_name and
110 not pkg_name.startswith(err.name + ".")): 115 not pkg_name.startswith(err.name + ".")):
111 raise 116 raise
112 raise error(format(err)) 117 raise error(format(err))
113 118
114 try: 119 try:
115 spec = importlib.util.find_spec(mod_name) 120 spec = importlib.util.find_spec(mod_name)
116 except (ImportError, AttributeError, TypeError, ValueError) as ex: 121 except (ImportError, AttributeError, TypeError, ValueError) as ex:
117 # This hack fixes an impedance mismatch between pkgutil and 122 # This hack fixes an impedance mismatch between pkgutil and
118 # importlib, where the latter raises other errors for cases where 123 # importlib, where the latter raises other errors for cases where
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 pass 275 pass
271 276
272 277
273 if __name__ == "__main__": 278 if __name__ == "__main__":
274 # Run the module specified as the next command line argument 279 # Run the module specified as the next command line argument
275 if len(sys.argv) < 2: 280 if len(sys.argv) < 2:
276 print("No module specified for execution", file=sys.stderr) 281 print("No module specified for execution", file=sys.stderr)
277 else: 282 else:
278 del sys.argv[0] # Make the requested module sys.argv[0] 283 del sys.argv[0] # Make the requested module sys.argv[0]
279 _run_module_as_main(sys.argv[0]) 284 _run_module_as_main(sys.argv[0])
LEFTRIGHT

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