Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 test_executing_methods(self):
frame = inspect.currentframe()
executing = Source.executing(frame)
self.assertEqual(executing.code_qualname(), 'TestStuff.test_executing_methods')
text = 'Source.executing(frame)'
self.assertEqual(executing.text(), text)
start, end = executing.text_range()
self.assertEqual(executing.source.text[start:end], text)
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)
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)
def check(source, encoding, exception=None, matches=True):
encoded = source.encode(encoding)
if exception:
with self.assertRaises(exception):
Source.decode_source(encoded)
else:
decoded = Source.decode_source(encoded)
if matches:
self.assertEqual(decoded, source)
else:
self.assertNotEqual(decoded, source)
if time.time() - self.start_time > 45 * 60:
# Avoid travis time limit of 50 minutes
raise TimeOut
lineno = linestarts.get(inst.offset, lineno)
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__))
def check(source, encoding, exception=None, matches=True):
encoded = source.encode(encoding)
if exception:
with self.assertRaises(exception):
Source.decode_source(encoded)
else:
decoded = Source.decode_source(encoded)
if matches:
self.assertEqual(decoded, source)
else:
self.assertNotEqual(decoded, source)
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
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 assert_qualname(self, func, qn, check_actual_qualname=True):
qualname = Source.for_filename(__file__).code_qualname(func.__code__)
self.assertEqual(qn, qualname)
if PY3 and check_actual_qualname:
self.assertEqual(qn, func.__qualname__)
self.assertTrue(qn.endswith(func.__name__))