Message102733
I'm using Python 3.1.2 64-bit on Windows.
I've found that even if "absolute_import" is imported from __future__, 2to3 will convert imports to be treated as relative.
To demonstrate this behavior, I created a small package "abs_imp_test" (attached).
abs_imp_test.__init__ is 0 bytes.
abs_imp_test.string is a one-line file.
foo = 'bar'
abs_imp_test.main contains 4 lines:
from __future__ import absolute_import
import string
assert not hasattr(string, 'foo'), 'fail'
print("success")
Put abs_imp_test package somewhere in the python path (just having it relative to current directory works).
Note that the code is designed to be future-proof (using the future directive), so will run under Python 2.6 and Python 3.1 without errors.
> python26\python -c "from abs_imp_test import main"
success
> python31\python -c "from abs_imp_test import main"
success
However, if I run 2to3 on main, it converts "import string" to "from . import string" which changes the fundamental meaning of the import and breaks the test.
> 2to3 abs_import_test
...
RefactoringTool: Files that were modified:
RefactoringTool: abs_imp_test\main.py
> python -c "from abs_imp_test import main"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "abs_imp_test\main.py", line 4, in <module>
assert not hasattr(string, 'foo'), "fail"
AssertionError: fail
Is it possible that if the absolute_import future directive is present that the imports not be modified for relativity? |
|
Date |
User |
Action |
Args |
2010-04-09 17:09:50 | jaraco | set | recipients:
+ jaraco |
2010-04-09 17:09:50 | jaraco | set | messageid: <1270832990.09.0.0431237155361.issue8358@psf.upfronthosting.co.za> |
2010-04-09 17:09:47 | jaraco | link | issue8358 messages |
2010-04-09 17:09:46 | jaraco | create | |
|