classification
Title: Blank lines in .pth file cause a duplicate sys.path entry
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Ido Michael, Malcolm Smith, ammar2, taleinat
Priority: normal Keywords: patch

Created on 2018-05-29 23:03 by Malcolm Smith, last changed 2020-09-19 19:14 by taleinat. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20679 merged Ido Michael, 2020-06-06 14:05
Messages (8)
msg318137 - (view) Author: Malcolm Smith (Malcolm Smith) Date: 2018-05-29 23:03
The `site` module documentation says that in .pth files, "Blank lines and lines beginning with # are skipped.". However, the implementation does not actually skip blank lines. It then joins the empty string to the site-packages directory, resulting in the site-packages directory being added to sys.path a second time.

Example:

$ python -c 'import sys; print(sys.path)'
['', '/home/smith/.virtualenvs/foo/lib/python36.zip', '/home/smith/.virtualenvs/foo/lib/python3.6', '/home/smith/.virtualenvs/foo/lib/python3.6/lib-dynload', '/usr/lib/python3.6', '/home/smith/.virtualenvs/foo/lib/python3.6/site-packages']
$ echo > /home/smith/.virtualenvs/foo/lib/python3.6/site-packages/test.pth
$ python -c 'import sys; print(sys.path)'
['', '/home/smith/.virtualenvs/foo/lib/python36.zip', '/home/smith/.virtualenvs/foo/lib/python3.6', '/home/smith/.virtualenvs/foo/lib/python3.6/lib-dynload', '/usr/lib/python3.6', '/home/smith/.virtualenvs/foo/lib/python3.6/site-packages', '/home/smith/.virtualenvs/foo/lib/python3.6/site-packages']

A patch fixing this is attached to issue 29326, but was ignored when that issue turned out to be caused by something else.
msg318201 - (view) Author: Ammar Askar (ammar2) * (Python triager) Date: 2018-05-30 17:08
If this is actually an issue, and not just a documentation lapse I can create a pull request from my original patch.
msg364185 - (view) Author: Malcolm Smith (Malcolm Smith) Date: 2020-03-14 15:42
It's definitely a bug and not a documentation lapse. There's no reason why blank lines should have that effect, and no indication in the code that this was intentional.
msg364235 - (view) Author: Ido Michael (Ido Michael) * Date: 2020-03-15 14:49
I can take this and have a PR
msg370824 - (view) Author: Ido Michael (Ido Michael) * Date: 2020-06-06 14:17
Created a PR: GH-20679 

I did see some of the changes are already in the code for example, test_underpth_nosite_file.test_sity.py already had pth_lines filter out: ''.

I think it's because this issue relevant only for 3.6?
Or maybe this issue was solved already and this bug report is redundant
msg377181 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-19 18:52
This is certainly a bug fix, but since it may break backwards-compatibility in delicate ways, I'm not going to backport it to earlier versions. Expect this to land in 3.10.
msg377183 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-19 19:13
New changeset 0c71a66b53f6ea262aa5a60784c8c625ae0bb98c by idomic in branch 'master':
bpo-33689: Blank lines in .pth file cause a duplicate sys.path entry (GH-20679)
https://github.com/python/cpython/commit/0c71a66b53f6ea262aa5a60784c8c625ae0bb98c
msg377184 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2020-09-19 19:14
Thanks for bringing this up again Malcolm, for the original Patch Ammar, and for the work on the PR Ido!
History
Date User Action Args
2020-09-19 19:14:49taleinatsetstatus: open -> closed
versions: + Python 3.10, - Python 3.6
messages: + msg377184

resolution: fixed
stage: patch review -> resolved
2020-09-19 19:13:36taleinatsetmessages: + msg377183
2020-09-19 18:52:43taleinatsetmessages: + msg377181
2020-06-06 14:17:11Ido Michaelsetmessages: + msg370824
2020-06-06 14:05:06Ido Michaelsetkeywords: + patch
stage: patch review
pull_requests: + pull_request19893
2020-06-06 13:49:38Ido Michaelsetnosy: + taleinat
2020-03-15 14:49:37Ido Michaelsetnosy: + Ido Michael
messages: + msg364235
2020-03-14 15:42:22Malcolm Smithsetmessages: + msg364185
2018-05-30 17:08:38ammar2setnosy: + ammar2
messages: + msg318201
2018-05-29 23:03:14Malcolm Smithcreate