msg49600 - (view) |
Author: Thomas Wouters (twouters) *  |
Date: 2006-02-24 22:28 |
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. |
|
|
msg49601 - (view) |
Author: Thomas Wouters (twouters) *  |
Date: 2006-02-24 22:58 |
Logged In: YES user_id=34209 I just noticed I didn't quite implement the right thing. 'from ..module import something' works as expected, but 'from . import module' is not implemented, and the 'import .module' syntax Guido rejected actually *is* implemented. I guess I'll have to fix that ;) On the upside, it seems this patch Just Works(tm) with zipimports, so I have reasonable faith that it's going to work across the board. |
|
|
msg49602 - (view) |
Author: Thomas Wouters (twouters) *  |
Date: 2006-02-25 18:33 |
Logged In: YES user_id=34209 A slightly modified patch that implements all of PEP-328 correctly. It does so by, at AST-generation time, changing 'from . import module' into 'import .module', and disallowing 'module .module' directly. I'm not sure if the AST should do that or not, but that's the easiest way to add it. |
|
|
msg49603 - (view) |
Author: Thomas Wouters (twouters) *  |
Date: 2006-02-27 20:47 |
Logged In: YES user_id=34209 And now a full, proper, complete implementation, with tests and everything. It should be just about done, barring small bugfixes, documentation, and parsermodule and compilerpackage support. |
|
|
msg49604 - (view) |
Author: Neal Norwitz (nnorwitz) *  |
Date: 2006-02-28 22:25 |
Logged In: YES user_id=33168 Thomas checked this in. |
|
|