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.
This commit is contained in:
Victor Stinner 2019-06-25 15:02:43 +02:00 committed by GitHub
parent 080b6b40fa
commit 3939c321c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 211 additions and 42 deletions

View file

@ -217,6 +217,18 @@ class CmdLineTest(unittest.TestCase):
with support.temp_dir() as script_dir:
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)
@ -542,7 +554,7 @@ class CmdLineTest(unittest.TestCase):
# 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(