This is a complete implementation of PEP-328, relative imports,
including the future statement to enable absolute imports by default.
It's currently lacking tests and documentation. A couple of caveats:
- It's implemented by extending the stack-arguments of the
IMPORT_NAME opcode, but it doesn't up the bytecode magic. You need
to 'make clean' or 'make distclean' to remove existing .pyc/.pyo files,
or you'll get random crashes.
- It's implemented by adding a 5th (optional) argument to __import__,
to the end, for 'relative depth'. This new argument, 'lvl', can be -1, 0 or
a positive number. -1 signals 'non-absolute non-dotted import', or
exactly the same as 'import module' before. 0 signals 'absolute non-
dotted import', which means 'import module' with absolute-imports
enabled (by using 'from __future__ import absolute_import'.) A positive
number is the number of dots in 'import ...module'; the amount of
relativeness of the import.
- __import__'s 5th value defaults to -1 for now. It should obviously
change to 0 when absolute imports are the absolute truth (in 2.7 or
so.)
- The 5th argument to __import__ is only passed when it's not -1, so
'old' __import__ replacements still work as long as code doesn't use
'import ..module' or 'from __future__ import absolute_import'.
- I haven't tested it extensively with frozen or zipped modules or
custom importhooks and what not. The testsuite shows no failures for
me (on MacOS and Linux) but none of the test modules use
'import ..test' or 'from __future__ import absolute_import'.
This patch was brought to you by Lufthansa "babycarrier" Airlines and
10 hours stuffed in a too small chair surrounded by crying, whiney
babies and kids.
|