def visit_For(self, node):
if isinstance(node.target, _ast.Name):
target = node.target.id
if isinstance(node.iter, _ast.Call) and isinstance(node.iter.func, _ast.Name):
if node.iter.func.id == "range":
return self.for_range(node, target)
else:
raise SyntaxError("GPUMAP: Only for ... in range(...) is supported!")
elif isinstance(node.iter, _ast.Name):
if node.iter.id in self.local_vars:
var_type = self.local_vars[node.iter.id]
if isinstance(var_type, str) and var_type.startswith("List<"):
list_type = var_type[var_type.find("<") + 1: var_type.rfind(">")]
return self.for_list(node, list_type, target)
elif isinstance(var_type, str) and var_type.startswith("List_Ptr<"):
list_type = var_type[var_type.find("<") + 1: var_type.rfind(">")]
return self.for_list(node, list_type, target, ptr=True)
else:
raise SyntaxError("GPUMAP: cannot iterate over a non-list type")
else:
raise SyntaxError("GPUMAP: no such variable found: " + node.iter.id)
else:
raise SyntaxError("GPUMAP: Only one variable can be assigned in a for loop!")
python类Call()的实例源码
def __repr__(self):
'''Generate representation of object for printing / interactive use
Most likely only interested in non-default properties, so we return
the string version of _context.
Example string returned:
<Context {'node': <_ast.Call object at 0x110252510>, 'function': None,
'name': 'socket', 'imports': set(['socket']), 'module': None,
'filename': 'examples/binding.py',
'call': <_ast.Call object at 0x110252510>, 'lineno': 3,
'import_aliases': {}, 'qualname': 'socket.socket'}>
:return: A string representation of the object
'''
return "<Context %s>" % self._context
def NAME(self, node):
"""
Handle occurrence of Name (which can be a load/store/delete access.)
"""
# Locate the name in locals / function / globals scopes.
if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
self.handleNodeLoad(node)
if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
and isinstance(node.parent, ast.Call)):
# we are doing locals() call in current scope
self.scope.usesLocals = True
elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
self.handleNodeStore(node)
elif isinstance(node.ctx, ast.Del):
self.handleNodeDelete(node)
else:
# must be a Param context -- this only happens for names in function
# arguments, but these aren't dispatched through here
raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
def NAME(self, node):
"""
Handle occurrence of Name (which can be a load/store/delete access.)
"""
# Locate the name in locals / function / globals scopes.
if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
self.handleNodeLoad(node)
if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
and isinstance(node.parent, ast.Call)):
# we are doing locals() call in current scope
self.scope.usesLocals = True
elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
self.handleNodeStore(node)
elif isinstance(node.ctx, ast.Del):
self.handleNodeDelete(node)
else:
# must be a Param context -- this only happens for names in function
# arguments, but these aren't dispatched through here
raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
def NAME(self, node):
"""
Handle occurrence of Name (which can be a load/store/delete access.)
"""
# Locate the name in locals / function / globals scopes.
if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
self.handleNodeLoad(node)
if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
and isinstance(node.parent, ast.Call)):
# we are doing locals() call in current scope
self.scope.usesLocals = True
elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
self.handleNodeStore(node)
elif isinstance(node.ctx, ast.Del):
self.handleNodeDelete(node)
else:
# must be a Param context -- this only happens for names in function
# arguments, but these aren't dispatched through here
raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
def parse_grpexpr(grp_ctx, tree, row_ctx, maker):
if isinstance(tree, _ast.Call) and tree.func.id in FUNC_MAP:
if tree.keywords:
raise Exception('Keyword arguments are not supported in %r' % (tree.func.id,))
return FUNC_MAP[tree.func.id](
maker, list(parse_expr(row_ctx, arg) for arg in tree.args)
)
else:
return parse_expr(grp_ctx, tree, partial(parse_grpexpr, maker=maker, row_ctx=row_ctx))
def BUILD_CLASS(self, instr):
call_func = self.ast_stack.pop()
assert isinstance(call_func, _ast.Call)
func = call_func.func
assert isinstance(func, _ast.FunctionDef)
code = func.body
pop_assignment(code, '__module__')
doc = pop_doc(code)
ret = code.pop()
assert isinstance(ret, _ast.Return) and ret.value == 'LOAD_LOCALS'
bases = self.ast_stack.pop()
assert isinstance(bases, _ast.Tuple)
bases = bases.elts
name = self.ast_stack.pop()
class_ = _ast.ClassDef(name=name, bases=bases, body=code, decorator_list=[],
lineno=instr.lineno, col_offset=0)
self.ast_stack.append(class_)
def CALL_FUNCTION(self, instr):
nkwargs = instr.oparg >> 8
nargs = (~(nkwargs << 8)) & instr.oparg
args = []
keywords = []
for _ in range(nkwargs):
expr = self.ast_stack.pop()
name = self.ast_stack.pop()
keyword = _ast.keyword(arg=name.s, value=expr, lineno=instr.lineno)
keywords.insert(0, keyword)
for _ in range(nargs):
arg = self.ast_stack.pop()
args.insert(0, arg)
if len(args) == 1 and isinstance(args[0], (_ast.FunctionDef, _ast.ClassDef)):
function = args[0]
if function.decorator_list is None:
function.decorator_list = []
node = self.ast_stack.pop()
function.decorator_list.insert(0, node)
self.ast_stack.append(function)
return
node = self.ast_stack.pop()
callfunc = _ast.Call(func=node, args=args, keywords=keywords, starargs=None, kwargs=None,
lineno=instr.lineno, col_offset=0)
self.ast_stack.append(callfunc)
def _(tree, **kwargs):
options = ''
if isinstance(tree[0], _ast.Expr) and isinstance(tree[0].value, _ast.Call):
# test for Settings()
func = tree[0].value.func.id
if func == 'Settings':
keywords = (parse(kwarg) for kwarg in tree[0].value.keywords)
for key, value in keywords:
if value[0] == '"':
options += '[{}:{}]\n'.format(key, value[1:-1])
else:
options += '[{}:{}]\n'.format(key, value)
tree = tree[1:]
return options + '\n'.join(parse(expr) for expr in transform(tree))
def _(item, body=None, top=False):
if top and len(item.args) != 0:
if isinstance(item.args[0], _ast.Call):
_transform(item.args[0], body=body, top=top)
if not isinstance(item.args[0], (_ast.Name, _ast.Num)):
syntax = ast.parse('tmpcall{} = b'.format(randint(0, 2**32))).body[0]
syntax.value = item.args[0]
item.args[0] = syntax.targets[0]
body.extend((syntax, item))
return True
return False