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: importlib mixes up '.' and os.getcwd()
Type: Stage:
Components: Library (Lib) Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: Arfrever, brett.cannon, ncoghlan, pitrou, skrah
Priority: normal Keywords:

Created on 2012-02-18 20:38 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg153656 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-02-18 20:38
An empty sys.path string means the current directory in relative terms:

$ touch foo.py
$ python3 -c "import foo; print(foo.__file__)"
foo.py


But importlib uses os.getcwd() instead in PathFinder._path_importer_cache(). This impacts semantics of path searching, as well as the values of __file__ and __cached__ attributes.
msg153663 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-02-19 00:55
It's surprisingly difficult to get right because tests will fail if you keep the path relative in other situations (such as test_cmd_line_script). It was an absolute pain to get it to where it is now. If you can make it keep the relative path and pass the test then that's great, but relative file paths suck for compatibility and I don't think they are that useful to begin with.
msg153702 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-02-19 14:13
Purging __file__ of relative references isn't a problem - they're *supposed* to always be absolute. The import.c version just stuffs it up sometimes (mainly due to the way it handles the empty string in the path).

IOW, while the importlib behaviour *is* different from the old behaviour, it's really a long overdue fix rather than a defect.
(You can still induce weirdness by adding paths involving '.' or '..' to sys.path, but that's more a case of "don't do that". We add the empty string ourselves, so we should handle it more cleanly than we do in import.c)
msg153782 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-02-20 13:09
There was the following sporadic buildbot failure:


======================================================================
FAIL: test_package___file__ (test.test_imp.PEP3147Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/srv/buildbot/buildarea/3.x.bolen-ubuntu/build/Lib/test/test_imp.py", line 333, in test_package___file__
    os.sep.join(('.', 'pep3147', '__init__.py')))
AssertionError: 'pep3147/__init__.py' != './pep3147/__init__.py'
- pep3147/__init__.py
+ ./pep3147/__init__.py
? ++


I wonder if the test suite is playing games and sometimes using importlib :-/
msg153788 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-02-20 18:23
[...]

And I can't reproduce, even when using --randseed.
msg153790 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-02-20 18:28
The test suite does use importlib for some imports as finders from importlib for some packages stick around. I brought this up a way back but people thought it was actually a good idea to let importlib handle imports to stress test it.

Anyway, if it can't be reproduced then I don't think this is worth worrying about. As Nick pointed out, everything should be absolute anyway.
msg153913 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-02-21 23:15
It just happened again on the new FreeBSD-9.0 bot:

http://www.python.org/dev/buildbot/all/builders/AMD64%20FreeBSD%208.2%203.x/builds/1845/steps/test/logs/stdio

======================================================================
FAIL: test_package___file__ (test.test_imp.PEP3147Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/home/buildbot/buildarea/3.x.krah-freebsd/build/Lib/test/test_imp.py", line 333, in test_package___file__
    os.sep.join(('.', 'pep3147', '__init__.py')))
AssertionError: 'pep3147/__init__.py' != './pep3147/__init__.py'
- pep3147/__init__.py
+ ./pep3147/__init__.py
? ++
History
Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58260
2012-02-21 23:15:22skrahsetnosy: + skrah
messages: + msg153913
2012-02-20 18:29:12brett.cannonsetstatus: open -> closed
resolution: not a bug
2012-02-20 18:28:54brett.cannonsetmessages: + msg153790
2012-02-20 18:23:07pitrousetmessages: + msg153788
2012-02-20 13:09:44pitrousetmessages: + msg153782
2012-02-20 01:03:24Arfreversetnosy: + Arfrever
2012-02-19 14:13:22ncoghlansetmessages: + msg153702
2012-02-19 08:37:49eric.araujosetnosy: + ncoghlan
2012-02-19 00:55:27brett.cannonsetmessages: + msg153663
2012-02-18 20:38:46pitroucreate