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: ImpImporter.find_modules removes symlinks in paths
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, cryvate
Priority: normal Keywords: patch

Created on 2017-12-01 00:10 by cryvate, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 4672 open Cryvate, 2017-12-01 23:18
Messages (3)
msg307353 - (view) Author: Henk-Jaap Wagenaar (cryvate) * Date: 2017-12-01 00:10
ImpImporter.find_modules calls os.path.real on the path used:

https://github.com/python/cpython/blob/be6b74c0795b709c7a04e2187a7e32d08f5155f6/Lib/pkgutil.py#L181

This means if there is a submodule (foo.bar) which first appears on the path involving a symlink that

pkgutil.find_loader('foo').get_filename()

has the path with the symlinks removed whereas

import foo
foo.__file__

does have the symlinks. Note that is in the absence of any PEP302 import hooks.

This behaviour comes up in pytest test/plugin collection in virtual environments in Py2.7 on linux where they have a symlinked venv/local/lib before venv/lib on the path and creates problems.

There might very well be a good reason the path is made absolute here, however the test suite passes when it is reverted to path = [self.path].
msg307385 - (view) Author: Henk-Jaap Wagenaar (cryvate) * Date: 2017-12-01 12:45
Ignoring testing code, there is minimal use of os.path.realpath in the stdlib (https://github.com/python/cpython/search?utf8=%E2%9C%93&q=%22os.path.realpath%22&type=):

- Lib/platform.py: used before opening a file (works around a cygwin bug)
- Lib/unittest/loader.py: used to compare two paths for equality
- Lib/pkgutil.py: as outlined in this issue
- Lib/pydoc.py: used to determine the directory
- Lib/inspect.py: path and realpath added to caching dictionary

So the question that remains is whether __import__ does this on the hood? I get not due to the difference noted in the OP betweein importing and using pkgutil.
msg307411 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-12-01 20:52
Import itself I don't believe calls os.path.realpath(), so this is probably just something pkgutil happens to do.
History
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76369
2017-12-01 23:18:47Cryvatesetkeywords: + patch
stage: patch review
pull_requests: + pull_request4580
2017-12-01 20:52:39brett.cannonsetnosy: + brett.cannon
messages: + msg307411
2017-12-01 12:45:07cryvatesetmessages: + msg307385
2017-12-01 00:10:50cryvatecreate