Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys.path entries normalization in site.py doesn't follow POSIX symlink behaviour #90811

Open
jpoiret mannequin opened this issue Feb 5, 2022 · 1 comment
Open

sys.path entries normalization in site.py doesn't follow POSIX symlink behaviour #90811

jpoiret mannequin opened this issue Feb 5, 2022 · 1 comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jpoiret
Copy link
Mannequin

jpoiret mannequin commented Feb 5, 2022

BPO 46653
Nosy @jpoiret

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-02-05.22:20:58.875>
labels = ['type-bug', '3.8', '3.9', '3.10', '3.11', '3.7', 'library']
title = "sys.path entries normalization in site.py doesn't follow POSIX symlink behaviour"
updated_at = <Date 2022-02-05.22:20:58.875>
user = 'https://github.com/jpoiret'

bugs.python.org fields:

activity = <Date 2022-02-05.22:20:58.875>
actor = 'jpoiret'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2022-02-05.22:20:58.875>
creator = 'jpoiret'
dependencies = []
files = []
hgrepos = []
issue_num = 46653
keywords = []
message_count = 1.0
messages = ['412600']
nosy_count = 1.0
nosy_names = ['jpoiret']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue46653'
versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

@jpoiret
Copy link
Mannequin Author

jpoiret mannequin commented Feb 5, 2022

Whenever sys.prefix contains a symlink followed by a '..', the corresponding part of sys.path entries will not refer to the parent directory of the directory pointed to by the symlink, but rather to the directory in which the symlink is. Thus, it will be impossible to import standard Python modules installed at sys.prefix, among other things.

Here is an example:
Suppose you have installed Python with prefix /usr. Create a symlink /tmp/symlink pointing to /usr/lib, and launch PYTHONHOME=/tmp/symlink/.. python3. In that REPL, import warnings will fail to find the correct module, as evidenced by the value of sys.path containing entries such as /tmp/lib/python3.X/ instead of the expected /usr/lib/python3.X/.

This issue is caused by the makepath function (among others) in Lib/site.py using os.path.abspath instead of os.path.realpath, which does not follow POSIX as the documentation of os.path.normpath (used internally by abspath) suggests.

I propose replacing all four instances of abspath in Lib/site.py to realpath instead. This is a breaking change for users that relied on non-conforming symlink semantics (although that user-base might be quite small), but in my opinion Python should be expected to follow the behaviour of the platform it is running on.

This issue was raised while investigating a bug [1] in the relocatable packs feature of GNU Guix [2], which makes use of symlinks to achieve relocatability.

[1] https://issues.guix.gnu.org/53258
[2] https://guix.gnu.org/en/blog/2018/tarballs-the-ultimate-container-image-format/

@jpoiret jpoiret mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 5, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants