Skip to content

Commit 0911ea5

Browse files
authored
bpo-39517: Allow runpy.run_path() to accept path-like objects (GH-18699)
1 parent 4ca060d commit 0911ea5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/runpy.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import importlib.util
1616
import io
1717
import types
18+
import os
1819
from pkgutil import read_code, get_importer
1920

2021
__all__ = [
@@ -229,11 +230,12 @@ def _get_main_module_details(error=ImportError):
229230

230231
def _get_code_from_file(run_name, fname):
231232
# Check for a compiled file first
232-
with io.open_code(fname) as f:
233+
decoded_path = os.path.abspath(os.fsdecode(fname))
234+
with io.open_code(decoded_path) as f:
233235
code = read_code(f)
234236
if code is None:
235237
# That didn't work, so try it as normal source code
236-
with io.open_code(fname) as f:
238+
with io.open_code(decoded_path) as f:
237239
code = compile(f.read(), fname, 'exec')
238240
return code, fname
239241

Lib/test/test_runpy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import importlib, importlib.machinery, importlib.util
99
import py_compile
1010
import warnings
11+
import pathlib
1112
from test.support import (
1213
forget, make_legacy_pyc, unload, verbose, no_tracing,
1314
create_empty_file, temp_dir)
@@ -652,6 +653,14 @@ def test_basic_script(self):
652653
self._check_script(script_name, "<run_path>", script_name,
653654
script_name, expect_spec=False)
654655

656+
def test_basic_script_with_path_object(self):
657+
with temp_dir() as script_dir:
658+
mod_name = 'script'
659+
script_name = pathlib.Path(self._make_test_script(script_dir,
660+
mod_name))
661+
self._check_script(script_name, "<run_path>", script_name,
662+
script_name, expect_spec=False)
663+
655664
def test_basic_script_no_suffix(self):
656665
with temp_dir() as script_dir:
657666
mod_name = 'script'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix runpy.run_path() when using pathlike objects

0 commit comments

Comments
 (0)