Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# will find a code mismatch.
for stmt in stmts:
# use `ast.parse` instead of `ast.Module` for better portability
# python3.8 changes the signature of `ast.Module`
# Inspired by https://github.com/pallets/werkzeug/pull/1552/files
tree = ast.parse("")
tree.body = [stmt]
ast.copy_location(tree, stmt)
try:
node = NodeFinder(frame, stmts, tree, lasti).result
break
except Exception:
pass
else:
try:
node = NodeFinder(frame, stmts, source.tree, lasti).result
except Exception as e:
# These exceptions can be caused by the source code having changed
# so the cached Source doesn't match the running code
# (e.g. when using IPython %autoreload)
# Try again with a fresh Source object
if retry_cache and isinstance(e, (NotOneValueFound, AssertionError)):
return find(
source=cls.for_frame(frame, use_cache=False),
retry_cache=False,
)
if TESTING:
raise
if node:
new_stmts = {statement_containing_node(node)}
assert_(new_stmts <= stmts)
if source.tree:
stmts = source.statements_at_line(lineno)
if code.co_filename.startswith('':
# IPython separates each statement in a cell to be executed separately
# So NodeFinder should only compile one statement at a time or it
# will find a code mismatch.
for stmt in stmts:
# use `ast.parse` instead of `ast.Module` for better portability
# python3.8 changes the signature of `ast.Module`
# Inspired by https://github.com/pallets/werkzeug/pull/1552/files
tree = ast.parse("")
tree.body = [stmt]
ast.copy_location(tree, stmt)
try:
node = NodeFinder(frame, stmts, tree, lasti).result
break
except Exception:
pass
else:
try:
node = NodeFinder(frame, stmts, source.tree, lasti).result
except Exception as e:
# These exceptions can be caused by the source code having changed
# so the cached Source doesn't match the running code
# (e.g. when using IPython %autoreload)
# Try again with a fresh Source object
if retry_cache and isinstance(e, (NotOneValueFound, AssertionError)):
return find(
source=cls.for_frame(frame, use_cache=False),
retry_cache=False,
)