summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMaor Kleinberger <kmaork@gmail.com>2020-03-08 20:43:17 (GMT)
committerGitHub <noreply@github.com>2020-03-08 20:43:17 (GMT)
commit0911ea5c172264eaefa3efe4a1788159c160920d (patch)
treefad7f723dadd5b83d61aef153478935690ee3c08 /Lib
parent4ca060d8ad7c6df1fd4df30f9a14f6aa35380c09 (diff)
downloadcpython-0911ea5c172264eaefa3efe4a1788159c160920d.zip
cpython-0911ea5c172264eaefa3efe4a1788159c160920d.tar.gz
cpython-0911ea5c172264eaefa3efe4a1788159c160920d.tar.bz2
bpo-39517: Allow runpy.run_path() to accept path-like objects (GH-18699)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/runpy.py6
-rw-r--r--Lib/test/test_runpy.py9
2 files changed, 13 insertions, 2 deletions
diff --git a/Lib/runpy.py b/Lib/runpy.py
index 8adc91e..0f54f3e 100644
--- a/Lib/runpy.py
+++ b/Lib/runpy.py
@@ -15,6 +15,7 @@ import importlib.machinery # importlib first so we can test #15386 via -m
import importlib.util
import io
import types
+import os
from pkgutil import read_code, get_importer
__all__ = [
@@ -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
diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py
index f003086..84834d3 100644
--- a/Lib/test/test_runpy.py
+++ b/Lib/test/test_runpy.py
@@ -8,6 +8,7 @@ import tempfile
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)
@@ -652,6 +653,14 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
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'