Kaydet (Commit) 761bb113 authored tarafından Nick Coghlan's avatar Nick Coghlan

Close #15230: runpy.run_path now sets __package__ correctly. Also refactored the…

Close #15230: runpy.run_path now sets __package__ correctly. Also refactored the runpy tests to use a more systematic approach
üst 2bb30218
...@@ -68,6 +68,7 @@ def _run_code(code, run_globals, init_globals=None, ...@@ -68,6 +68,7 @@ def _run_code(code, run_globals, init_globals=None,
run_globals.update(__name__ = mod_name, run_globals.update(__name__ = mod_name,
__file__ = mod_fname, __file__ = mod_fname,
__cached__ = None, __cached__ = None,
__doc__ = None,
__loader__ = mod_loader, __loader__ = mod_loader,
__package__ = pkg_name) __package__ = pkg_name)
exec(code, run_globals) exec(code, run_globals)
...@@ -242,12 +243,14 @@ def run_path(path_name, init_globals=None, run_name=None): ...@@ -242,12 +243,14 @@ def run_path(path_name, init_globals=None, run_name=None):
""" """
if run_name is None: if run_name is None:
run_name = "<run_path>" run_name = "<run_path>"
pkg_name = run_name.rpartition(".")[0]
importer = _get_importer(path_name) importer = _get_importer(path_name)
if isinstance(importer, imp.NullImporter): if isinstance(importer, imp.NullImporter):
# Not a valid sys.path entry, so run the code directly # Not a valid sys.path entry, so run the code directly
# execfile() doesn't help as we want to allow compiled files # execfile() doesn't help as we want to allow compiled files
code = _get_code_from_file(path_name) code = _get_code_from_file(path_name)
return _run_module_code(code, init_globals, run_name, path_name) return _run_module_code(code, init_globals, run_name, path_name,
pkg_name=pkg_name)
else: else:
# Importer is defined for path, so add it to # Importer is defined for path, so add it to
# the start of sys.path # the start of sys.path
...@@ -266,7 +269,6 @@ def run_path(path_name, init_globals=None, run_name=None): ...@@ -266,7 +269,6 @@ def run_path(path_name, init_globals=None, run_name=None):
mod_name, loader, code, fname = _get_main_module_details() mod_name, loader, code, fname = _get_main_module_details()
finally: finally:
sys.modules[main_name] = saved_main sys.modules[main_name] = saved_main
pkg_name = ""
with _TempModule(run_name) as temp_module, \ with _TempModule(run_name) as temp_module, \
_ModifiedArgv0(path_name): _ModifiedArgv0(path_name):
mod_globals = temp_module.module.__dict__ mod_globals = temp_module.module.__dict__
......
This diff is collapsed.
...@@ -87,6 +87,9 @@ Core and Builtins ...@@ -87,6 +87,9 @@ Core and Builtins
Library Library
------- -------
- Issue #15230: runpy.run_path now correctly sets __package__ as described
in the documentation
- Issue #14990: Correctly fail with SyntaxError on invalid encoding - Issue #14990: Correctly fail with SyntaxError on invalid encoding
declaration. declaration.
...@@ -341,6 +344,8 @@ Extension Modules ...@@ -341,6 +344,8 @@ Extension Modules
Tests Tests
----- -----
- Issue #15230: Adopted a more systematic approach in the runpy tests
- Issue #15300: Ensure the temporary test working directories are in the same - Issue #15300: Ensure the temporary test working directories are in the same
parent folder when running tests in multiprocess mode from a Python build. parent folder when running tests in multiprocess mode from a Python build.
Patch by Chris Jerdonek. Patch by Chris Jerdonek.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment