Skip to content

fix statements finding for multiple debug calls in the same block #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions devtools/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,23 +317,19 @@ def _statement_range(call_frame: 'FrameType', func_name: str) -> 'Tuple[int, int
first_line = None
last_line = None

for instr in instructions:
for instr in instructions: # pragma: no branch
if instr.starts_line:
if instr.opname in {'LOAD_GLOBAL', 'LOAD_NAME'} and instr.argval == func_name:
first_line = instr.starts_line
break
elif instr.opname == 'LOAD_GLOBAL' and instr.argval == 'debug':
if next(instructions).argval == func_name:
first_line = instr.starts_line
break
if instr.offset == call_frame.f_lasti:
break

if first_line is None:
raise IntrospectionError('error parsing code, unable to find "{}" function statement'.format(func_name))

for instr in instructions: # pragma: no branch
if instr.offset == call_frame.f_lasti:
break

for instr in instructions:
if instr.starts_line:
last_line = instr.starts_line - 1
Expand Down
25 changes: 25 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,28 @@ def __getattr__(self, item):
" b: <tests.test_main.test_pretty_error.<locals>.BadPretty object at 0x000> (BadPretty)\n"
" !!! error pretty printing value: RuntimeError('this is an error')"
)


@pytest.mark.skipif(sys.version_info >= (3, 8), reason='different between 3.7 and 3.8')
def test_multiple_debugs_37():
debug.format([i * 2 for i in range(2)])
debug.format([i * 2 for i in range(2)])
v = debug.format([i * 2 for i in range(2)])
s = re.sub(r':\d{2,}', ':<line no>', str(v))
assert s == (
'tests/test_main.py:<line no> test_multiple_debugs_37\n'
' [i * 2 for i in range(2)]: [0, 2] (list) len=2'
)


@pytest.mark.skipif(sys.version_info < (3, 8), reason='different between 3.7 and 3.8')
def test_multiple_debugs_38():
debug.format([i * 2 for i in range(2)])
debug.format([i * 2 for i in range(2)])
v = debug.format([i * 2 for i in range(2)])
s = re.sub(r':\d{2,}', ':<line no>', str(v))
# FIXME there's an extraneous bracket here, due to some error building code from the ast
assert s == (
'tests/test_main.py:<line no> test_multiple_debugs_38\n'
' ([i * 2 for i in range(2)]: [0, 2] (list) len=2'
)