[Python-Dev] [Patch #101135] 'import x as y' and 'from x import y as z' (fwd)

Thomas Wouters thomas@xs4all.net
Wed, 9 Aug 2000 22:27:50 +0200


For those of you not on the patches list, here's the summary of the patch I
just uploaded to SF. In short, it adds "import x as y" and "from module
import x as y", in the way Tim proposed this morning. (Probably late last
night for most of you.)

----- Forwarded message from noreply@sourceforge.net -----

This patch adds the oft-proposed 'import as' syntax, to both 'import module'
and 'from module import ...', but without making 'as' a reserved word (by
using the technique Tim Peters proposed on python-dev.)

'import spam as egg' is a very simple patch to compile.c, which doesn't need
changes to the VM, but 'from spam import dog as meat' needs a new bytecode,
which this patch calls 'FROM_IMPORT_AS'. The bytecode loads an object from a
module onto the stack, so a STORE_NAME can store it later. This can't be
done by the normal FROM_IMPORT opcode, because it needs to take the special
case of '*' into account. Also, because it uses 'STORE_NAME', it's now
possible to mix 'import' and 'global', like so:

global X
from foo import X as X

The patch still generates the old code for

from foo import X

(without 'as') mostly to save on bytecode size, and for the 'compatibility'
with mixing 'global' and 'from .. import'... I'm not sure what's the best
thing to do.

The patch doesn't include a test suite or documentation, yet.

-------------------------------------------------------
For more info, visit:

http://sourceforge.net/patch/?func=detailpatch&patch_id=101135&group_id=5470

----- End forwarded message -----

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!