Skip to content

bpo-39517: fix runpy.run_path() when using pathlike objects #18699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions Lib/runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import importlib.util
import io
import types
import os
from pkgutil import read_code, get_importer

__all__ = [
Expand Down Expand Up @@ -229,11 +230,12 @@ def _get_main_module_details(error=ImportError):

def _get_code_from_file(run_name, fname):
# Check for a compiled file first
with io.open_code(fname) as f:
decoded_path = os.path.abspath(os.fsdecode(fname))
with io.open_code(decoded_path) as f:
code = read_code(f)
if code is None:
# That didn't work, so try it as normal source code
with io.open_code(fname) as f:
with io.open_code(decoded_path) as f:
code = compile(f.read(), fname, 'exec')
return code, fname

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import importlib, importlib.machinery, importlib.util
import py_compile
import warnings
import pathlib
from test.support import (
forget, make_legacy_pyc, unload, verbose, no_tracing,
create_empty_file, temp_dir)
Expand Down Expand Up @@ -652,6 +653,14 @@ def test_basic_script(self):
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)

def test_basic_script_with_path_object(self):
with temp_dir() as script_dir:
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)

def test_basic_script_no_suffix(self):
with temp_dir() as script_dir:
mod_name = 'script'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix runpy.run_path() when using pathlike objects