Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_many_calls(self):
node = None
start = time.time()
for i in range(10000):
new_node = Source.executing(inspect.currentframe()).node
if node is None:
node = new_node
else:
self.assertIs(node, new_node)
self.assertLess(time.time() - start, 1)
if not inst.opname.startswith((
'BINARY_', 'UNARY_', 'LOAD_ATTR', 'LOAD_METHOD', 'LOOKUP_METHOD',
'SLICE+', 'COMPARE_OP', 'CALL_', 'IS_OP', 'CONTAINS_OP',
)):
continue
frame = C()
frame.f_lasti = inst.offset
frame.f_code = code
frame.f_globals = globals()
frame.f_lineno = lineno
source = Source.for_frame(frame)
node = None
try:
try:
node = Source.executing(frame).node
except Exception:
if inst.opname.startswith(('COMPARE_OP', 'CALL_')):
continue
if isinstance(only(source.statements_at_line(lineno)), (ast.AugAssign, ast.Import)):
continue
raise
except Exception:
print(source.text, lineno, inst, node and ast.dump(node), code, file=sys.stderr, sep='\n')
raise
nodes[node].append((inst, frame.__dict__))
yield [inst.opname, node_string(source, node)]
for const in code.co_consts:
if isinstance(const, type(code)):
def get_node(self, typ):
frame = inspect.currentframe().f_back.f_back
Source.lazycache(frame)
node = Source.executing(frame).node
assert isinstance(node, typ), (node, typ)
return node
def highlight_stack_frame(frame):
executing = Source.executing(frame)
node = executing.node
source = executing.source
if node:
source.asttokens()
start = node.first_token.start[0]
end = node.last_token.end[0]
else:
start = end = frame.f_lineno
highlighted = '\n'.join(highlight_ranges(source, [frame]).splitlines()[start - 1:end])
return highlight_python_and_ranges(highlighted)