[Python-checkins] bpo-39517: Allow runpy.run_path() to accept path-like objects (GH-18699)

Miss Islington (bot) webhook-mailer at python.org
Sun Mar 8 17:00:04 EDT 2020


https://github.com/python/cpython/commit/0687bdf5def13ddca09e854113c0eac0772afea0
commit: 0687bdf5def13ddca09e854113c0eac0772afea0
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-08T13:59:59-07:00
summary:

bpo-39517: Allow runpy.run_path() to accept path-like objects (GH-18699)

(cherry picked from commit 0911ea5c172264eaefa3efe4a1788159c160920d)

Co-authored-by: Maor Kleinberger <kmaork at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-02-29-11-20-50.bpo-39517.voQZb8.rst
M Lib/runpy.py
M Lib/test/test_runpy.py

diff --git a/Lib/runpy.py b/Lib/runpy.py
index 8adc91e32f319..0f54f3e71b6fe 100644
--- a/Lib/runpy.py
+++ b/Lib/runpy.py
@@ -15,6 +15,7 @@
 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 f003086111639..84834d39b1c21 100644
--- a/Lib/test/test_runpy.py
+++ b/Lib/test/test_runpy.py
@@ -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)
@@ -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'
diff --git a/Misc/NEWS.d/next/Library/2020-02-29-11-20-50.bpo-39517.voQZb8.rst b/Misc/NEWS.d/next/Library/2020-02-29-11-20-50.bpo-39517.voQZb8.rst
new file mode 100644
index 0000000000000..0cd628f43a8e4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-02-29-11-20-50.bpo-39517.voQZb8.rst
@@ -0,0 +1 @@
+Fix runpy.run_path() when using pathlike objects
\ No newline at end of file



More information about the Python-checkins mailing list