Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Decode path before setting the module globals
runpy.run_path did not decode path-like objects to string, and instead stored
them directly in the runned module globals, potentially as non-str objects.
  • Loading branch information
kamilturek committed Aug 4, 2023
commit 4c2a7f0fedfc8133bc90a772213785ac359673bf
3 changes: 2 additions & 1 deletion Lib/runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def _get_main_module_details(error=ImportError):
def _get_code_from_file(run_name, fname):
# Check for a compiled file first
from pkgutil import read_code
decoded_path = os.path.abspath(os.fsdecode(fname))
decoded_path = os.path.abspath(fname)
with io.open_code(decoded_path) as f:
code = read_code(f)
if code is None:
Expand Down Expand Up @@ -279,6 +279,7 @@ def run_path(path_name, init_globals=None, run_name=None):
pkg_name = run_name.rpartition(".")[0]
from pkgutil import get_importer
importer = get_importer(path_name)
path_name = os.fsdecode(path_name)
if isinstance(importer, type(None)):
# Not a valid sys.path entry, so run the code directly
# execfile() doesn't help as we want to allow compiled files
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,10 @@ def test_basic_script_with_path_object(self):
mod_name = 'script'
script_name = pathlib.Path(self._make_test_script(script_dir,
mod_name))
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)
self._check_script(script_name, "<run_path>",
os.fsdecode(script_name),
os.fsdecode(script_name),
expect_spec=False)

def test_basic_script_no_suffix(self):
with temp_dir() as script_dir:
Expand Down