88from compiler import ast , parse , walk , syntax
99from compiler import pyassem , misc , future , symbols
1010from compiler .consts import SC_LOCAL , SC_GLOBAL , SC_FREE , SC_CELL
11- from compiler .consts import CO_VARARGS , CO_VARKEYWORDS , CO_NEWLOCALS ,\
12- CO_NESTED , CO_GENERATOR , CO_GENERATOR_ALLOWED , CO_FUTURE_DIVISION
11+ from compiler .consts import (CO_VARARGS , CO_VARKEYWORDS , CO_NEWLOCALS ,
12+ CO_NESTED , CO_GENERATOR , CO_GENERATOR_ALLOWED , CO_FUTURE_DIVISION ,
13+ CO_FUTURE_ABSIMPORT , CO_FUTURE_WITH_STATEMENT )
1314from compiler .pyassem import TupleArg
1415
1516# XXX The version-specific code can go, since this code only works with 2.x.
@@ -215,6 +216,10 @@ def __init__(self):
215216 self ._div_op = "BINARY_TRUE_DIVIDE"
216217 elif feature == "generators" :
217218 self .graph .setFlag (CO_GENERATOR_ALLOWED )
219+ elif feature == "absolute_import" :
220+ self .graph .setFlag (CO_FUTURE_ABSIMPORT )
221+ elif feature == "with_statement" :
222+ self .graph .setFlag (CO_FUTURE_WITH_STATEMENT )
218223
219224 def initClass (self ):
220225 """This method is called once for each class"""
@@ -543,6 +548,19 @@ def visitAnd(self, node):
543548 def visitOr (self , node ):
544549 self .visitTest (node , 'JUMP_IF_TRUE' )
545550
551+ def visitIfExp (self , node ):
552+ endblock = self .newBlock ()
553+ elseblock = self .newBlock ()
554+ self .visit (node .test )
555+ self .emit ('JUMP_IF_FALSE' , elseblock )
556+ self .emit ('POP_TOP' )
557+ self .visit (node .then )
558+ self .emit ('JUMP_FORWARD' , endblock )
559+ self .nextBlock (elseblock )
560+ self .emit ('POP_TOP' )
561+ self .visit (node .else_ )
562+ self .nextBlock (endblock )
563+
546564 def visitCompare (self , node ):
547565 self .visit (node .expr )
548566 cleanup = self .newBlock ()
@@ -875,8 +893,10 @@ def visitPass(self, node):
875893
876894 def visitImport (self , node ):
877895 self .set_lineno (node )
896+ level = 0 if "absolute_import" in self .futures else - 1
878897 for name , alias in node .names :
879898 if VERSION > 1 :
899+ self .emit ('LOAD_CONST' , level )
880900 self .emit ('LOAD_CONST' , None )
881901 self .emit ('IMPORT_NAME' , name )
882902 mod = name .split ("." )[0 ]
@@ -888,8 +908,12 @@ def visitImport(self, node):
888908
889909 def visitFrom (self , node ):
890910 self .set_lineno (node )
911+ level = node .level
912+ if level == 0 and "absolute_import" not in self .futures :
913+ level = - 1
891914 fromlist = map (lambda (name , alias ): name , node .names )
892915 if VERSION > 1 :
916+ self .emit ('LOAD_CONST' , level )
893917 self .emit ('LOAD_CONST' , tuple (fromlist ))
894918 self .emit ('IMPORT_NAME' , node .modname )
895919 for name , alias in node .names :
0 commit comments