bpo-20443: _PyConfig_Read() gets the absolute path of run_filename (GH-14053)
Python now gets the absolute path of the script filename specified on
the command line (ex: "python3 script.py"): the __file__ attribute of
the __main__ module, sys.argv[0] and sys.path[0] become an absolute
path, rather than a relative path.
* Add _Py_isabs() and _Py_abspath() functions.
* _PyConfig_Read() now tries to get the absolute path of
run_filename, but keeps the relative path if _Py_abspath() fails.
* Reimplement os._getfullpathname() using _Py_abspath().
* Use _Py_isabs() in getpath.c.
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index d138ca0..4677e60 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -218,6 +218,18 @@
script_name = _make_test_script(script_dir, 'script')
self._check_script(script_name, script_name, script_name,
script_dir, None,
+ importlib.machinery.SourceFileLoader,
+ expected_cwd=script_dir)
+
+ def test_script_abspath(self):
+ # pass the script using the relative path, expect the absolute path
+ # in __file__ and sys.argv[0]
+ with support.temp_cwd() as script_dir:
+ self.assertTrue(os.path.isabs(script_dir), script_dir)
+
+ script_name = _make_test_script(script_dir, 'script')
+ self._check_script(os.path.basename(script_name), script_name, script_name,
+ script_dir, None,
importlib.machinery.SourceFileLoader)
def test_script_compiled(self):
@@ -542,7 +554,7 @@
# Issue #16218
source = 'print(ascii(__file__))\n'
- script_name = _make_test_script(os.curdir, name, source)
+ script_name = _make_test_script(os.getcwd(), name, source)
self.addCleanup(support.unlink, script_name)
rc, stdout, stderr = assert_python_ok(script_name)
self.assertEqual(
diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index 1bc8d3a..b897489 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -805,9 +805,10 @@
preconfig = {
'allocator': PYMEM_ALLOCATOR_DEBUG,
}
+ script_abspath = os.path.abspath('script.py')
config = {
- 'argv': ['script.py'],
- 'run_filename': 'script.py',
+ 'argv': [script_abspath],
+ 'run_filename': script_abspath,
'dev_mode': 1,
'faulthandler': 1,
'warnoptions': ['default'],
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 86c2f22..be848b2 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -926,27 +926,26 @@
return stderr
# tracemalloc disabled
+ filename = os.path.abspath(support.TESTFN)
stderr = run('-Wd', support.TESTFN)
- expected = textwrap.dedent('''
- {fname}:5: ResourceWarning: unclosed file <...>
+ expected = textwrap.dedent(f'''
+ {filename}:5: ResourceWarning: unclosed file <...>
f = None
ResourceWarning: Enable tracemalloc to get the object allocation traceback
- ''')
- expected = expected.format(fname=support.TESTFN).strip()
+ ''').strip()
self.assertEqual(stderr, expected)
# tracemalloc enabled
stderr = run('-Wd', '-X', 'tracemalloc=2', support.TESTFN)
- expected = textwrap.dedent('''
- {fname}:5: ResourceWarning: unclosed file <...>
+ expected = textwrap.dedent(f'''
+ {filename}:5: ResourceWarning: unclosed file <...>
f = None
Object allocated at (most recent call last):
- File "{fname}", lineno 7
+ File "{filename}", lineno 7
func()
- File "{fname}", lineno 3
+ File "{filename}", lineno 3
f = open(__file__)
- ''')
- expected = expected.format(fname=support.TESTFN).strip()
+ ''').strip()
self.assertEqual(stderr, expected)