-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Possible duplicate entries in sys.path if .pth files are used with zip's #70774
Comments
In site.py there is the internal function _init_pathinfo() This function builds a set of path entries from sys.path. This is used to avoid duplicate entries in sys.path. The fix is as simple as removing the os.path.isdir(...) line and fixing the indent. Also the docstring should be modified. Detected by using this function in a project reusing addsitedir(...) functionality to add another path with .pth processing. |
Could you provide a code example of your using addsitedir that results in duplicates? |
Here is a testcase to reproduce the issue:
This prints just a single instance of '/foo/bar': ['/local/mnt/workspace/python/tst', '/foo/bar', '/usr/local/lib/python36.zip', '/local/mnt/workspace/python/src/Lib', '/local/mnt/workspace/python/src/Lib/plat-linux', '/local/mnt/workspace/python/build/build/lib.linux-x86_64-3.6-pydebug'] Now if we explicitly set PYTHONPATH to include '/foo/bar'
and then run test.py here is the output: ['/local/mnt/workspace/python/tst', '/foo/bar', '/usr/local/lib/python36.zip', '/local/mnt/workspace/python/src/Lib', '/local/mnt/workspace/python/src/Lib/plat-linux', '/local/mnt/workspace/python/build/build/lib.linux-x86_64-3.6-pydebug', '/foo/bar'] We see that there are duplicate entries for '/foo/bar'. As Wolfgang rightly said the issue comes from the check for But since this is the first bug I am looking at I am not sure of the implications of removing this check. Can someone please confirm that what I see is indeed a failing test case, or is this the intended behavior? Thanks, |
Thanks for this, Mandeep. I don't think it is entirely the same issue that Wolfgang is describing. He's particularly concerned about the clash of .zip files from the sys.path with ones coming from .pth files for example. And while the proposed fix would resolve the issue, I don't feel it would be entirely correct. For the record, I'm not even sure sys.path is guaranteed to be contain no duplicates, at least I wasn't able to find a statement to that effect in the docs. In any case, I'm adding Brett here who was seemingly the last one to touch that bit of code. I'm also attaching a path with what I think is a more appropriate and smaller fix. |
Extended unit test for the issue and patch for site.py. |
I think a fix for 3.6 only is ok, because it changes behaviour. Should I add a test with a temporary created pth file? |
Unfortunately you can't simply remove that directory check because you don't want to blindly normalize case. If someone put some token value on sys.path for their use that was case-sensitive even on a case-insensitive OS then the proposed change would break those semantics (e.g. if someone put a URL in sys.path for a REST-based importer). The possibilities I see are:
|
And the code under discussion can be found at https://hg.python.org/cpython/file/default/Lib/site.py#l133 |
I still think my fix is more appropriate as it ensures that known_paths and sys.path stay connected somehow. |
Ok, I implemented point 3. Added a test case also for an URL path. I think duplicate detection is now improved and it should break nothing. |
New changeset bd1af1a97c2e by Brett Cannon in branch 'default': |
New changeset 09dc97edf454 by Brett Cannon in branch '3.5': New changeset 94d5c57ee835 by Brett Cannon in branch 'default': |
I simplified Wolfgang's patch by simply using os.path.exists(). That eliminated the one place where os.path.isdir() was in use that was too specific to directories where files were reasonable to expect. I also quickly tweaked the docs for the site module in 3.5 to not promise that files would work. If you think there is still an issue with keeping things tied together, SilentGhost, feel free to open another issue to track it. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: