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 highlight_ranges(source, frames):
text = source.text
ranges = set()
for frame in frames:
executing = Source.executing(frame)
if executing.node:
text_range = executing.text_range()
ranges.add(text_range)
positions = []
for start, end in ranges:
positions.append((start, open_sentinel))
positions.append((end, close_sentinel))
while True:
start = text.find('\n', start + 1, end)
if start == -1:
break
positions.append((start, close_sentinel))
positions.append((start + 1, open_sentinel))