def visitGeneratorExp(self, n, varchecks, *args): generators, varchecks = self.handleComprehensions( n.generators, n.lineno, n.col_offset, varchecks, *args) elt = self.dispatch(n.elt, varchecks, *args) return ast.GeneratorExp(elt=elt, generators=generators)
def visit_ListComp(self, node): self.generic_visit(node) if node in self.potential_iterator: self.update = True return ast.GeneratorExp(node.elt, node.generators) else: return node
def For(var_in, body): gen = [ast.comprehension(Store(n.id), s, []) for (n, s) in var_in] ## Nested for-loops are elided together. if isinstance(body, ast.GeneratorExp): gen.extend(body.generators) body = body.elt return ast.GeneratorExp(body, gen)
def visit_DictComp(self, node): result = self.block.alloc_temp() elt = ast.Tuple(elts=[node.key, node.value], context=ast.Load) with self.visit(ast.GeneratorExp(elt, node.generators)) as gen: self.writer.write_checked_call2( result, 'πg.DictType.Call(πF, πg.Args{{{}}}, nil)', gen.expr) return result
def visit_GeneratorExp(self, node: GeneratorExp, *args, **kwargs) -> C.GeneratorExp: elt = self.visit(node.elt, *args, **kwargs) generators = self.visit(node.generators, *args, **kwargs) return C.GeneratorExp( elt=elt, generators=generators, )
def visit_For(self, node: ast.For) -> ast.For: inner_for: ast.For = node.body[0] new_target = ast.Tuple(elts=[node.target, inner_for.target]) def create_comprehension(for_node: ast.For) -> ast.comprehension: return ast.comprehension(target=for_node.target, iter=for_node.iter, ifs=[]) gen_exp = ast.GeneratorExp( elt=new_target, generators=[ create_comprehension(node), create_comprehension(inner_for) ], ) new_for = ast.For(target=new_target, iter=gen_exp, body=inner_for.body, orelse=node.orelse) new_for = ast.fix_missing_locations(new_for) return new_for
def as_ast(dct): """See https://docs.python.org/2/library/ast.html""" if dct['ast_type'] == "Module": return ast.Module(dct["body"]) elif dct['ast_type'] == "Interactive": return ast.Interactive(dct["body"]) elif dct['ast_type'] == "Expression": return ast.Expression(dct["body"]) elif dct['ast_type'] == "Suite": return ast.Suite(dct["body"]) elif dct['ast_type'] == "FunctionDef": return ast.FunctionDef(dct["name"], dct["args"], dct["body"], dct["decorator_list"]) elif dct['ast_type'] == "ClassDef": return ast.ClassDef(dct["name"], dct["bases"], dct["body"], dct["decorator_list"]) elif dct['ast_type'] == "Return": return ast.Return(dct["value"]) elif dct['ast_type'] == "Delete": return ast.Delete(dct["targets"]) elif dct['ast_type'] == "Assign": return ast.Assign(dct["targets"], dct["value"]) elif dct['ast_type'] == "AugAssign": return ast.AugAssign(dct["target"], dct["op"], dct["value"]) elif dct['ast_type'] == "Print": return ast.Print(dct["dest"], dct["values"], dct["nl"]) elif dct['ast_type'] == "For": return ast.For(dct["target"], dct["iter"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "While": return ast.While(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "If": return ast.If(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "With": return ast.With(dct["context_expr"], dct["optional_vars"], dct["body"]) elif dct['ast_type'] == "Raise": return ast.Raise(dct["type"], dct["inst"], dct["tback"]) elif dct['ast_type'] == "TryExcept": return ast.TryExcept(dct["body"], dct["handlers"], dct["orelse"]) elif dct['ast_type'] == "TryFinally": return ast.TryFinally(dct["body"], dct["finalbody"]) elif dct['ast_type'] == "Assert": return ast.Assert(dct["test"], dct["msg"]) elif dct['ast_type'] == "Import": return ast.Import(dct["names"]) elif dct['ast_type'] == "ImportFrom": return ast.ImportFrom(dct["module"], dct["names"], dct["level"]) elif dct['ast_type'] == "Exec": return ast.Exec(dct["body"], dct["globals"], dct["locals"]) elif dct['ast_type'] == "Global": return ast.Global(dct["names"]) elif dct['ast_type'] == "Expr": return ast.Expr(dct["value"]) elif dct['ast_type'] == "Pass": return ast.Pass() elif dct['ast_type'] == "Break": return ast.Break() elif dct['ast_type'] == "Continue": return ast.Continue() elif dct['ast_type'] == "BoolOp": return ast.BoolOp(dct["op"], dct["values"]) elif dct['ast_type'] == "BinOp": return ast.BinOp(dct["left"], dct["op"], dct["right"]) elif dct['ast_type'] == "UnaryOp": return ast.UnaryOp(dct["op"], dct["operand"]) elif dct['ast_type'] == "Lambda": return ast.Lambda(dct["args"], dct["body"]) elif dct['ast_type'] == "IfExp": return ast.IfExp(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "Dict": return ast.Dict(dct["keys"], dct["values"]) elif dct['ast_type'] == "Set": return ast.Set(dct["elts"]) elif dct['ast_type'] == "ListComp": return ast.ListComp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "SetComp": return ast.SetComp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "DictComp": return ast.DictComp(dct["key"], dct["value"], dct["generators"]) elif dct['ast_type'] == "GeneratorExp": return ast.GeneratorExp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "Yield": return ast.Yield(dct["value"]) elif dct['ast_type'] == "Compare": return ast.Compare(dct["left"], dct["ops"], dct["comparators"]) elif dct['ast_type'] == "Call": return ast.Call(dct["func"], dct["args"], dct["keywords"], dct["starargs"], dct["kwargs"]) elif dct['ast_type'] == "Repr": return ast.Repr(dct["value"]) elif dct['ast_type'] == "Num": return ast.Num(dct["n"]) elif dct['ast_type'] == "Str": # Converting to ASCII return ast.Str(dct["s"].encode('ascii', 'ignore')) elif dct['ast_type'] == "Attribute": return ast.Attribute(dct["value"], dct["attr"], dct["ctx"]) elif dct['ast_type'] == "Subscript": return ast.Subscript(dct["value"], dct["slice"], dct["ctx"]) elif dct['ast_type'] == "Name": return ast.Name(dct["id"], dct["ctx"]) elif dct['ast_type'] == "List": return ast.List(dct["elts"], dct["ctx"]) elif dct['ast_type'] == "Tuple": return ast.Tuple(dct["elts"], dct["ctx"]) elif dct['ast_type'] == "Load": return ast.Load() elif dct['ast_type'] == "Store": return ast.Store() elif dct['ast_type'] == "Del": return ast.Del() elif dct['ast_type'] == "AugLoad": return ast.AugLoad() elif dct['ast_type'] == "AugStore": return ast.AugStore() elif dct['ast_type'] == "Param": return ast.Param() elif dct['ast_type'] == "Ellipsis": return ast.Ellipsis() elif dct['ast_type'] == "Slice": return ast.Slice(dct["lower"], dct["upper"], dct["step"]) elif dct['ast_type'] == "ExtSlice": return ast.ExtSlice(dct["dims"]) elif dct['ast_type'] == "Index": return ast.Index(dct["value"]) elif dct['ast_type'] == "And": return ast.And() elif dct['ast_type'] == "Or": return ast.Or() elif dct['ast_type'] == "Add": return ast.Add() elif dct['ast_type'] == "Sub": return ast.Sub() elif dct['ast_type'] == "Mult": return ast.Mult() elif dct['ast_type'] == "Div": return ast.Div() elif dct['ast_type'] == "Mod": return ast.Mod() elif dct['ast_type'] == "Pow": return ast.Pow() elif dct['ast_type'] == "LShift": return ast.LShift() elif dct['ast_type'] == "RShift": return ast.RShift() elif dct['ast_type'] == "BitOr": return ast.BitOr() elif dct['ast_type'] == "BitXor": return ast.BitXor() elif dct['ast_type'] == "BitAnd": return ast.BitAnd() elif dct['ast_type'] == "FloorDiv": return ast.FloorDiv() elif dct['ast_type'] == "Invert": return ast.Invert() elif dct['ast_type'] == "Not": return ast.Not() elif dct['ast_type'] == "UAdd": return ast.UAdd() elif dct['ast_type'] == "USub": return ast.USub() elif dct['ast_type'] == "Eq": return ast.Eq() elif dct['ast_type'] == "NotEq": return ast.NotEq() elif dct['ast_type'] == "Lt": return ast.Lt() elif dct['ast_type'] == "LtE": return ast.LtE() elif dct['ast_type'] == "Gt": return ast.Gt() elif dct['ast_type'] == "GtE": return ast.GtE() elif dct['ast_type'] == "Is": return ast.Is() elif dct['ast_type'] == "IsNot": return ast.IsNot() elif dct['ast_type'] == "In": return ast.In() elif dct['ast_type'] == "NotIn": return ast.NotIn() elif dct['ast_type'] == "comprehension": return ast.comprehension(dct["target"], dct["iter"], dct["ifs"]) elif dct['ast_type'] == "ExceptHandler": return ast.ExceptHandler(dct["type"], dct["name"], dct["body"]) elif dct['ast_type'] == "arguments": return ast.arguments(dct["args"], dct["vararg"], dct["kwarg"], dct["defaults"]) elif dct['ast_type'] == "keyword": return ast.keyword(dct["arg"], dct["value"]) elif dct['ast_type'] == "alias": return ast.alias(dct["name"], dct["asname"]) else: return dct
def visit_ListComp(self, node): result = self.block.alloc_temp() with self.visit(ast.GeneratorExp(node.elt, node.generators)) as gen: self.writer.write_checked_call2( result, 'πg.ListType.Call(πF, πg.Args{{{}}}, nil)', gen.expr) return result
def make_base_class_dict_test_stmts( self, bases: List[TAst], instance_fields: Set[str] ) -> stmt: slots_stmt = self.make_slots_stmt(instance_fields) # if there are non-names in the bases of the class, give up and just create slots if not all(isinstance(b, ast.Name) for b in bases): return slots_stmt # if __dict__ is not added to instance fields (no loose slots), just slotify if "__dict__" not in instance_fields: return slots_stmt # generate code that decide whether __dict__ should be included # if any('__dict__' in getattr(_t, '__dict__', ()) for b in <bases> for _t in b.mro()): # __slots__ = <slots without __dict__> # else: # __slots__ = <slots with __dict__> names = [lineinfo(ast.Name(cast(ast.Name, n).id, ast.Load())) for n in bases] condition = lineinfo( ast.Call( lineinfo(ast.Name("any", ast.Load())), [ lineinfo( ast.GeneratorExp( lineinfo( ast.Compare( lineinfo(Constant("__dict__")), [lineinfo(ast.In())], [ lineinfo( ast.Call( lineinfo( ast.Name("getattr", ast.Load()) ), [ lineinfo( ast.Name("_t", ast.Load()) ), lineinfo(Constant("__dict__")), lineinfo(ast.Tuple([], ast.Load())), ], [], ) ) ], ) ), [ lineinfo( ast.comprehension( lineinfo(ast.Name("b", ast.Store())), lineinfo(ast.List(names, ast.Load())), [], 0, ) ), lineinfo( ast.comprehension( lineinfo(ast.Name("_t", ast.Store())), lineinfo( ast.Call( lineinfo( ast.Attribute( lineinfo( ast.Name("b", ast.Load()) ), "mro", ast.Load(), ) ), [], [], ) ), [], 0, ) ), ], ) ) ], [], ) ) slots_stmt_without_dict = self.make_slots_stmt(instance_fields - {"__dict__"}) return lineinfo(ast.If(condition, [slots_stmt_without_dict], [slots_stmt]))
def test_operators(self): boolop0 = ast.BoolOp() boolop1 = ast.BoolOp(ast.And(), [ ast.Name('True', ast.Load()), ast.Name('False', ast.Load()), ast.Name('a',ast.Load())]) boolop2 = ast.BoolOp(ast.And(), [ ast.Name('True', ast.Load()), ast.Name('False', ast.Load()), ast.Name('a',ast.Load())], 0, 0) binop0 = ast.BinOp() binop1 = ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3)) binop2 = ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3), 0, 0) unaryop0 = ast.UnaryOp() unaryop1 = ast.UnaryOp(ast.Not(), ast.Name('True',ast.Load())) unaryop2 = ast.UnaryOp(ast.Not(), ast.Name('True',ast.Load()), 0, 0) lambda0 = ast.Lambda() lambda1 = ast.Lambda(ast.arguments([ast.Name('x', ast.Param())], None, None, []), ast.Name('x', ast.Load())) ifexp0 = ast.IfExp() ifexp1 = ast.IfExp(ast.Name('True',ast.Load()), ast.Num(1), ast.Num(0)) ifexp2 = ast.IfExp(ast.Name('True',ast.Load()), ast.Num(1), ast.Num(0), 0, 0) dict0 = ast.Dict() dict1 = ast.Dict([ast.Num(1), ast.Num(2)], [ast.Str('a'), ast.Str('b')]) dict2 = ast.Dict([ast.Num(1), ast.Num(2)], [ast.Str('a'), ast.Str('b')], 0, 0) set0 = ast.Set() set1 = ast.Set([ast.Num(1), ast.Num(2)]) set2 = ast.Set([ast.Num(1), ast.Num(2)], 0, 0) lc0 = ast.ListComp() lc1 = ast.ListComp( ast.Name('x',ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])]) lc2 = ast.ListComp( ast.Name('x',ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])], 0, 0) setcomp0 = ast.SetComp() setcomp1 = ast.SetComp(ast.Name('x', ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Str('abracadabra'), [ast.Compare(ast.Name('x', ast.Load()), [ast.NotIn()], [ast.Str('abc')])])]) comprehension0 = ast.comprehension() comprehension1 = ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), []) # "{i : chr(65+i) for i in (1,2)}") dictcomp0 = ast.DictComp() dictcomp1 = ast.DictComp(ast.Name('i', ast.Load()), ast.Call(ast.Name('chr', ast.Load()), [ast.BinOp(ast.Num(65), ast.Add(), ast.Name('i', ast.Load()))], [], None, None), [ast.comprehension(ast.Name('i', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(n=2)], ast.Load()), [])]) dictcomp2 = ast.DictComp(ast.Name('i', ast.Load()), ast.Call(ast.Name('chr', ast.Load()), [ast.BinOp(ast.Num(65), ast.Add(), ast.Name('i', ast.Load()))], [], None, None), [ast.comprehension(ast.Name('i', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(n=2)], ast.Load()), [])],0,0) # (x for x in (1,2)) genexp0 = ast.GeneratorExp() genexp1 = ast.GeneratorExp(ast.Name('x', ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])]) genexp2 = ast.GeneratorExp(ast.Name('x', ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])],0,0) # yield 2 yield0 = ast.Yield() yield1 = ast.Yield(ast.Num(2)) yield2 = ast.Yield(ast.Num(2),0,0) yield20 = ast.Yield(lineno=0, col_offset=0) # a>0 compare0 = ast.Compare() compare1 = ast.Compare(ast.Name('a', ast.Load()), [ast.Gt()], [ast.Num(0)]) compare2 = ast.Compare(ast.Name('a', ast.Load()), [ast.Gt()], [ast.Num(0)],0,0) # chr(65) call0 = ast.Call() call1 = ast.Call(ast.Name('chr', ast.Load()), [ast.Num(65)], [], None, None) call2 = ast.Call(ast.Name('chr', ast.Load()), [ast.Num(65)], [], None, None, 0, 0) call20 = ast.Call(ast.Name('f', ast.Load()), [ast.Num(0)], []) call21 = ast.Call(ast.Name('f', ast.Load()), [ast.Num(0)], [], lineno=0, col_offset=0) # 0 num0 = ast.Num() num1 = ast.Num(0) num2 = ast.Num(0,0,0) # "foo" str0 = ast.Str() str1 = ast.Str("foo") str2 = ast.Str("foo",0,0) # TODO: come back repr0 = ast.Repr() repr1 = ast.Repr(ast.Num(0)) repr2 = ast.Repr(ast.Num(0),0,0) # foo.bar attr0 = ast.Attribute() attr1 = ast.Attribute(ast.Name('foo', ast.Load()), 'bar', ast.Load()) attr2 = ast.Attribute(ast.Name('foo', ast.Load()), 'bar', ast.Load(), 0,0) # a[1:2] subscript0 = ast.Subscript() subscript1 = ast.Subscript(ast.Name('a', ast.Load()), ast.Slice(ast.Num(1), ast.Num(2)), ast.Load()) subscript2 = ast.Subscript(ast.Name('a', ast.Load()), ast.ExtSlice([ast.Num(1), ast.Num(2)]), ast.Load(), 0, 0) # name name0 = ast.Name() name1 = ast.Name("name", ast.Load()) name2 = ast.Name("name", ast.Load(),0,0) # [1,2] list0 = ast.List() list1 = ast.List([ast.Num(1), ast.Num(2)], ast.Load()) list2 = ast.List([ast.Num(1), ast.Num(2)], ast.Load(),0,0) # (1,2) tuple0 = ast.Tuple() tuple1 = ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()) tuple2 = ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load(), 0, 0)
class Patterns: """ Stores the pattern nodes / templates to be used extracting information from the Python ast. Patterns are a 1-to-1 mapping from context and Python ast node to GTScript ast node. Context is encoded in the field types and all understood sementic is encoded in the structure. """ Symbol = ast.Name(id=Capture("name")) IterationOrder = ast.withitem( context_expr=ast.Call(func=ast.Name(id="computation"), args=[ast.Name(id=Capture("order"))])) Constant = ast.Constant(value=Capture("value")) Interval = ast.withitem(context_expr=ast.Call( func=ast.Name(id="interval"), args=[Capture("start"), Capture("stop")])) # TODO(tehrengruber): this needs to be a function, since the uid must be generated each time LocationSpecification = ast.withitem( context_expr=ast.Call(func=ast.Name(id="location"), args=[ast.Name(id=Capture("location_type")) ]), optional_vars=Capture( "name", default=ast.Name(id=UIDGenerator.get_unique_id( prefix="location"))), ) SubscriptSingle = ast.Subscript( value=Capture("value"), slice=ast.Index(value=ast.Name(id=Capture("index")))) SubscriptMultiple = ast.Subscript( value=Capture("value"), slice=ast.Index(value=ast.Tuple(elts=Capture("indices")))) BinaryOp = ast.BinOp(op=Capture("op"), left=Capture("left"), right=Capture("right")) Call = ast.Call(args=Capture("args"), func=ast.Name(id=Capture("func"))) LocationComprehension = ast.comprehension(target=Capture("target"), iter=Capture("iterator")) Generator = ast.GeneratorExp(generators=Capture("generators"), elt=Capture("elt")) Assign = ast.Assign(targets=[Capture("target")], value=Capture("value")) Stencil = ast.With(items=Capture("iteration_spec"), body=Capture("body")) Pass = ast.Pass() Argument = ast.arg(arg=Capture("name"), annotation=Capture("type_")) Computation = ast.FunctionDef( args=ast.arguments(args=Capture("arguments")), body=Capture("stencils"), name=Capture("name"), )
class Patterns: """ Stores the pattern nodes / templates to be used extracting information from the Python ast. Patterns are a 1-to-1 mapping from context and Python ast node to GTScript ast node. Context is encoded in the field types and all understood sementic is encoded in the structure. """ SymbolName = ast.Name(id=Capture(0)) SymbolRef = ast.Name(id=Capture("name")) IterationOrder = ast.withitem( context_expr=ast.Call(func=ast.Name(id="computation"), args=[ast.Name(id=Capture("order"))])) Constant = ast.Constant(value=Capture("value")) Interval = ast.withitem(context_expr=ast.Call( func=ast.Name(id="interval"), args=[Capture("start"), Capture("stop")])) LocationSpecification = ast.withitem( context_expr=ast.Call(func=ast.Name(id="location"), args=[Capture("location_type")]), optional_vars=Capture( "name", default=lambda: ast.Name(id=UIDGenerator.sequential_id( prefix="location"))), ) Subscript = ast.Subscript( value=Capture("value"), slice=ast.Index( Capture("indices", transformer=SubscriptTransformer)), ) BinaryOp = ast.BinOp(op=Capture("op"), left=Capture("left"), right=Capture("right")) Call = ast.Call(args=Capture("args"), func=Capture("func", expected_type=ast.Name)) SubscriptCall = ast.Call(args=Capture("args"), func=Capture("func", expected_type=ast.Subscript)) List_ = ast.List(elts=Capture("elts")) LocationComprehension = ast.comprehension(target=Capture("target"), iter=Capture("iterable")) Generator = ast.GeneratorExp(generators=Capture("generators"), elt=Capture("elt")) Assign = ast.Assign(targets=[Capture("target")], value=Capture("value")) Stencil = ast.With(items=Capture("iteration_spec"), body=Capture("body")) Pass = ast.Pass() Argument = ast.arg(arg=Capture("name", transformer=StrToSymbolTransformer), annotation=Capture("type_")) Computation = ast.FunctionDef( # args=ast.arguments(args=Capture("arguments")), # noqa E800 body=Capture("stencils"), name=Capture("name"), ) UnaryOp = ast.UnaryOp(op=Capture("op"), operand=Capture("operand"))
def visitGeneratorExp(self, n, *args): generators = self.reduce(n.generators, *args) elt = self.dispatch(n.elt, *args) return ast.GeneratorExp(elt=elt, generators=generators)
def test_empty_init(self): # Jython 2.5.0 did not allow empty constructors for many ast node types # but CPython ast nodes do allow this. For the moment, I don't see a # reason to allow construction of the super types (like ast.AST and # ast.stmt) as well as the op types that are implemented as enums in # Jython (like boolop), but I've left them in but commented out for # now. We may need them in the future since CPython allows this, but # it may fall under implementation detail. #ast.AST() ast.Add() ast.And() ast.Assert() ast.Assign() ast.Attribute() ast.AugAssign() ast.AugLoad() ast.AugStore() ast.BinOp() ast.BitAnd() ast.BitOr() ast.BitXor() ast.BoolOp() ast.Break() ast.Call() ast.ClassDef() ast.Compare() ast.Continue() ast.Del() ast.Delete() ast.Dict() ast.Div() ast.Ellipsis() ast.Eq() ast.Exec() ast.Expr() ast.Expression() ast.ExtSlice() ast.FloorDiv() ast.For() ast.FunctionDef() ast.GeneratorExp() ast.Global() ast.Gt() ast.GtE() ast.If() ast.IfExp() ast.Import() ast.ImportFrom() ast.In() ast.Index() ast.Interactive() ast.Invert() ast.Is() ast.IsNot() ast.LShift() ast.Lambda() ast.List() ast.ListComp() ast.Load() ast.Lt() ast.LtE() ast.Mod() ast.Module() ast.Mult() ast.Name() ast.Not() ast.NotEq() ast.NotIn() ast.Num() ast.Or() ast.Param() ast.Pass() ast.Pow() ast.Print() ast.RShift() ast.Raise() ast.Repr() ast.Return() ast.Slice() ast.Store() ast.Str() ast.Sub() ast.Subscript() ast.Suite() ast.TryExcept() ast.TryFinally() ast.Tuple() ast.UAdd() ast.USub() ast.UnaryOp() ast.While() ast.With() ast.Yield() ast.alias() ast.arguments() #ast.boolop() #ast.cmpop() ast.comprehension() #ast.excepthandler() #ast.expr() #ast.expr_context() ast.keyword()
def visit_Call(self, call_node): func_node = call_node.func if isinstance(func_node, ast.Name) and func_node.id == "isinstance": # original code was: # assert isinstance(obj, cls_or_tuple) # # generated code is: # # @contexts_assertion_var1 = obj # @contexts_assertion_var2 = cls_or_tuple # @contexts_assertion_var3 = obj.__class__ # @contexts_assertion_var4 = (tuple(@x.__name__ for @x in @contexts_assertion_var2) # if isinstance(@contexts_assertion_var2, tuple) # else @contexts_assertion_var2.__name__) # assert isinstance(@contexts_assertion_var1, @contexts_assertion_var2), 'Asserted isinstance({0}, {1}) but found it to be a {2}'.format(@contexts_assertion_var1, repr(@contexts_assertion_var4).replace("'", ""), @contexts_assertion_var3) if sys.version_info < (3, 6): tupleAstArgs = ast.comprehension( ast.Name('@x', ast.Store()), self.load('@contexts_assertion_var2'), []) else: tupleAstArgs = ast.comprehension( ast.Name('@x', ast.Store()), self.load('@contexts_assertion_var2'), [], False) return [ self.assign('@contexts_assertion_var1', call_node.args[0]), self.assign('@contexts_assertion_var2', call_node.args[1]), self.assign( '@contexts_assertion_var3', self.clsname( self.getattr(self.load('@contexts_assertion_var1'), '__class__'))), self.assign( '@contexts_assertion_var4', ast.IfExp( ast.Call(func=self.load('isinstance'), args=[ self.load('@contexts_assertion_var2'), self.load('tuple'), ], keywords=[]), ast.Call(func=self.load('tuple'), args=[ ast.GeneratorExp( self.clsname(self.load('@x')), [ tupleAstArgs, ]), ], keywords=[]), self.clsname(self.load('@contexts_assertion_var2')))), ast.Assert( ast.Call(func=ast.Name(id='isinstance', ctx=ast.Load()), args=[ self.load('@contexts_assertion_var1'), self.load('@contexts_assertion_var2'), ], keywords=[]), self.format( 'Asserted isinstance({0}, {1}) but found it to be a {2}', [ self.repr(self.load('@contexts_assertion_var1')), ast.Call(func=self.getattr( self.repr( self.load('@contexts_assertion_var4')), 'replace'), args=[ast.Str("'"), ast.Str("")], keywords=[]), self.load('@contexts_assertion_var3'), ])) ] if isinstance(func_node, ast.Name) and func_node.id == "all": # original code was: # # assert all(iterable) # # generated code is: # # @contexts_assertion_var1 = iterable # for @contexts_assertion_var_ix, @contexts_assertion_var_elem in enumerate(@contexts_assertion_var1): # assert x, "Not all elements of {} were truthy. First falsy element: {} at position {}".format(@contexts_assertion_var1, @contexts_assertion_var_ix, @contexts_assertion_var_elem) return [ self.assign('@contexts_assertion_var1', call_node.args[0]), ast.For( ast.Tuple([ ast.Name("@contexts_assertion_var_ix", ast.Store()), ast.Name("@contexts_assertion_var_elem", ast.Store()) ], ast.Store()), ast.Call(func=self.load("enumerate"), args=[self.load('@contexts_assertion_var1')], keywords=[]), [ ast.Assert( self.load('@contexts_assertion_var_elem'), self.format( "Not all elements of {0} were truthy. First falsy element: {1} at position {2}", [ self.repr( self.load('@contexts_assertion_var1')), self.repr( self.load( '@contexts_assertion_var_elem')), self.load('@contexts_assertion_var_ix'), ])) ], []) ]
def GeneratorExp(draw, expression) -> ast.GeneratorExp: return ast.GeneratorExp(elt=draw(expression), generators=draw( lists(comprehension(expression), min_size=1, max_size=3)))