[Patches] [ python-Patches-1438387 ] PEP 0328 (relative import) implementation
SourceForge.net
noreply at sourceforge.net
Mon Feb 27 21:47:37 CET 2006
Patches item #1438387, was opened at 2006-02-24 23:28
Message generated for change (Comment added) made by twouters
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1438387&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Thomas Wouters (twouters)
Assigned to: Nobody/Anonymous (nobody)
Summary: PEP 0328 (relative import) implementation
Initial Comment:
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.
----------------------------------------------------------------------
>Comment By: Thomas Wouters (twouters)
Date: 2006-02-27 21:47
Message:
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.
----------------------------------------------------------------------
Comment By: Thomas Wouters (twouters)
Date: 2006-02-25 19:33
Message:
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.
----------------------------------------------------------------------
Comment By: Thomas Wouters (twouters)
Date: 2006-02-24 23:58
Message:
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.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1438387&group_id=5470
More information about the Patches
mailing list