fix debug statement hook to catch imports using a dundle method

This commit is contained in:
Anderson Medeiros 2025-03-16 19:43:30 -03:00
parent 31903eabdb
commit c5a939b530
2 changed files with 11 additions and 0 deletions

View file

@ -44,6 +44,11 @@ class DebugStatementParser(ast.NodeVisitor):
self.breakpoints.append(st)
def visit_Call(self, node: ast.Call) -> None:
if isinstance(node, ast.Call) and len(node.args):
if isinstance(node.args[0], ast.Constant) and node.args[0].value in DEBUG_STATEMENTS:
st = Debug(node.lineno, node.col_offset, node.args[0].value, 'imported')
self.breakpoints.append(st)
"""python3.7+ breakpoint()"""
if isinstance(node.func, ast.Name) and node.func.id == 'breakpoint':
st = Debug(node.lineno, node.col_offset, node.func.id, 'called')

View file

@ -26,6 +26,12 @@ def test_finds_debug_import_from_import():
assert visitor.breakpoints == [Debug(1, 0, 'pudb', 'imported')]
def test_finds_debug_import_when_using_dunder_import():
visitor = DebugStatementParser()
visitor.visit(ast.parse('__import__("pdb").set_trace()'))
assert visitor.breakpoints == [Debug(1, 0, 'pdb', 'imported')]
def test_finds_breakpoint():
visitor = DebugStatementParser()
visitor.visit(ast.parse('breakpoint()'))