Skip to content

bpo-20443: _PyConfig_Read() gets the absolute path of run_filename#14053

Merged
vstinner merged 2 commits intopython:masterfrom
vstinner:run_filename_abspath
Jun 25, 2019
Merged

bpo-20443: _PyConfig_Read() gets the absolute path of run_filename#14053
vstinner merged 2 commits intopython:masterfrom
vstinner:run_filename_abspath

Conversation

@vstinner
Copy link
Copy Markdown
Member

@vstinner vstinner commented Jun 13, 2019

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 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

https://bugs.python.org/issue20443

@vstinner
Copy link
Copy Markdown
Member Author

@1st1: This change covers your example described in https://bugs.python.org/issue20443 Would you mind to have a look?

@vstinner vstinner requested review from brettcannon and ncoghlan June 17, 2019 21:06
@brettcannon brettcannon added the type-feature A feature request or enhancement label Jun 21, 2019
@vstinner
Copy link
Copy Markdown
Member Author

@ncoghlan: Does this change seem reasonable to you?

Effect of my PR 14053 using script.py:

import sys
print(f"{__file__=}")
print(f"{sys.argv=}")
print(f"{sys.path[0]=}")

Before:

$ ./python script.py 
__file__=script.py
sys.argv=['script.py']
sys.path[0]=/home/vstinner/prog/python/master

With this change:

$ ./python script.py 
__file__=/home/vstinner/prog/python/master/script.py
sys.argv=['/home/vstinner/prog/python/master/script.py']
sys.path[0]=/home/vstinner/prog/python/master

vstinner added 2 commits June 25, 2019 02:46
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 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
@vstinner vstinner merged commit 3939c32 into python:master Jun 25, 2019
@vstinner vstinner deleted the run_filename_abspath branch June 25, 2019 13:02
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
…ythonGH-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.
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…ythonGH-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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants