This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.executable generating canonical path
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: alvesjnr, eric.araujo, iritkatriel, ned.deily, petri.lehtinen, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2012-02-22 16:36 by alvesjnr, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
canonical_path.diff alvesjnr, 2012-02-22 16:36 review
sys_executable_realpath.patch vstinner, 2012-02-22 23:12 review
Messages (15)
msg153970 - (view) Author: Antonio Ribeiro (alvesjnr) Date: 2012-02-22 16:36
Hi all,

As it is my first time here, I'll try to explay step-by-step why I'm providing this path, and why I think that it is changing something that I believe that is not correct.

First of all, I was trying to run one individual test, as the dev guide says:

./python -m test -v test_sys

but, instead of run this test from the root directory of the project, I run it from ./Lib/test, so I did:

../../python -m test -v test_sys

and it returned an error. The reason is that my sys.executable was not an abspath. When I went to see why it is different, I get this value to sys.executable:

sys.executable -> '/home/antonio/Projects/cpython/Lib/test/../../python'

I'm not sure if I'm thinking write, but for me don't look nice to have this value as sys.executable, once the real path should be:

'/home/antonio/Projects/cpython/python'

So I'm providing a paatch that do exactly this.

Hope you enjoy
msg153993 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-02-22 21:49
This is unfortunately wrong since it will fail in the face of symlinks.
(if "foo" is a symlink to another directory, then "foo/../bar" is not the same thing as "bar")
msg154008 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-02-22 23:12
We can use realpath() if the function is available. Something similar is already done for sys.argv[0]. We should also use GetFullPathNameW() on Windows.
msg154009 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-02-22 23:13
See also the issue #13402.
msg154010 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-02-22 23:17
> We can use realpath() if the function is available. Something similar
> is already done for sys.argv[0]. We should also use GetFullPathNameW()
> on Windows.

GetFullPathName() is an abspath() equivalent. For realpath() you want
GetFinalPathNameByHandle(), aka. _nt.getfinalpathname().
msg154012 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-02-22 23:26
> GetFullPathName() is an abspath() equivalent. For realpath() you
> want GetFinalPathNameByHandle(), aka. _nt.getfinalpathname().

Ah? ntpath.realpath() uses nt._getfullpathname(). Is it a bug in ntpath?
msg154015 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-02-22 23:37
> Ah? ntpath.realpath() uses nt._getfullpathname(). Is it a bug in ntpath?

Probably:

# realpath is a no-op on systems without islink support
realpath = abspath
msg154019 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-02-22 23:48
>> Is it a bug in ntpath?
> Probably

Ok, I opened the issue #14094.
msg154108 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-24 04:36
I wonder if the doc for sys.executable should mention that .. segments may be present.
msg154448 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-02-27 09:32
Éric Araujo wrote:
> I wonder if the doc for sys.executable should mention that ..
> segments may be present.

Once this is fixed, it should say that .. segments may be present if
there's realpath() is not available.
msg400435 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-08-27 17:21
This is still unresolved:

iritkatriel@Irits-MBP Lib % pwd
/Users/iritkatriel/src/cpython-1/Lib
iritkatriel@Irits-MBP Lib % ../python.exe -m test -v test_sys

[snipped]

======================================================================
FAIL: test_executable (test.test_sys.SysModuleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/iritkatriel/src/cpython-1/Lib/test/test_sys.py", line 692, in test_executable
    self.assertEqual(os.path.abspath(sys.executable), sys.executable)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: '/Users/iritkatriel/src/cpython-1/python.exe' != '/Users/iritkatriel/src/cpython-1/Lib/../python.exe'
- /Users/iritkatriel/src/cpython-1/python.exe
+ /Users/iritkatriel/src/cpython-1/Lib/../python.exe
?                                  +++++++
msg400618 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-08-30 15:16
test_sys:

    def test_executable(self):
        # sys.executable should be absolute
        self.assertEqual(os.path.abspath(sys.executable), sys.executable)

IMO the test is wrong. It must test: test.assertTrue(os.path.isabs(sys.executable)).

The path is not required to be normalized.
msg400619 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-08-30 15:17
> The path is not required to be normalized.

I'm talking about the current implementation.

I'm not against making Python even better: attempt to normalize the path ;-)
msg400651 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2021-08-30 18:25
> I'm not against making Python even better: attempt to normalize the path ;-)

I would be very cautious about doing that. I'm pretty sure it would break some existing code.
msg400787 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-08-31 21:58
See also bpo-20443 "__code__. co_filename should always be an absolute path". I had a revert a change which affected sys.argv[0]:

commit a1a99b4bb7cbe2dbc55a1d92c3c509b4466d3c3b
Author: Victor Stinner <vstinner@python.org>
Date:   Mon Dec 9 17:34:02 2019 +0100

    bpo-20443: No longer make sys.argv[0] absolute for script (GH-17534)
    
    In Python 3.9.0a1, sys.argv[0] was made an asolute path if a filename
    was specified on the command line. Revert this change, since most
    users expect sys.argv to be unmodified.
History
Date User Action Args
2022-04-11 14:57:27adminsetgithub: 58296
2021-08-31 21:58:44vstinnersetmessages: + msg400787
2021-08-30 18:25:34ned.deilysetnosy: + ned.deily
messages: + msg400651
2021-08-30 15:17:50vstinnersetmessages: + msg400619
2021-08-30 15:16:42vstinnersetmessages: + msg400618
2021-08-27 17:21:17iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11
nosy: + iritkatriel

messages: + msg400435

components: + Library (Lib), - None
2012-02-27 09:32:49petri.lehtinensetmessages: + msg154448
2012-02-24 04:36:09eric.araujosetnosy: + eric.araujo, petri.lehtinen
messages: + msg154108
2012-02-22 23:48:37vstinnersetmessages: + msg154019
2012-02-22 23:37:46pitrousetmessages: + msg154015
2012-02-22 23:26:03vstinnersetmessages: + msg154012
2012-02-22 23:17:32pitrousetmessages: + msg154010
2012-02-22 23:13:13vstinnersetmessages: + msg154009
2012-02-22 23:12:08vstinnersetfiles: + sys_executable_realpath.patch
nosy: + vstinner
messages: + msg154008

2012-02-22 21:49:29pitrousetnosy: + pitrou
messages: + msg153993
2012-02-22 16:36:56alvesjnrcreate