[Patches] [Patch #101135] 'import x as y' and 'from x import y as z'

noreply@sourceforge.net noreply@sourceforge.net
Wed, 9 Aug 2000 13:23:05 -0700


Patch #101135 has been updated. 

Project: 
Category: core (C code)
Status: Open
Summary: 'import x as y' and 'from x import y as z'

Follow-Ups:

Date: 2000-Aug-09 13:23
By: twouters

Comment:
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