Skip to content

Commit fc7ee93

Browse files
authored
fix statements finding for multiple debug calls in the same block (#61)
* fix sttatement finding for multiple debug calls in the same block * different for 3.8 and 3.7 * coverage
1 parent 8db5110 commit fc7ee93

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

devtools/debug.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,23 +317,19 @@ def _statement_range(call_frame: 'FrameType', func_name: str) -> 'Tuple[int, int
317317
first_line = None
318318
last_line = None
319319

320-
for instr in instructions:
320+
for instr in instructions: # pragma: no branch
321321
if instr.starts_line:
322322
if instr.opname in {'LOAD_GLOBAL', 'LOAD_NAME'} and instr.argval == func_name:
323323
first_line = instr.starts_line
324-
break
325324
elif instr.opname == 'LOAD_GLOBAL' and instr.argval == 'debug':
326325
if next(instructions).argval == func_name:
327326
first_line = instr.starts_line
328-
break
327+
if instr.offset == call_frame.f_lasti:
328+
break
329329

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

333-
for instr in instructions: # pragma: no branch
334-
if instr.offset == call_frame.f_lasti:
335-
break
336-
337333
for instr in instructions:
338334
if instr.starts_line:
339335
last_line = instr.starts_line - 1

tests/test_main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,28 @@ def __getattr__(self, item):
258258
" b: <tests.test_main.test_pretty_error.<locals>.BadPretty object at 0x000> (BadPretty)\n"
259259
" !!! error pretty printing value: RuntimeError('this is an error')"
260260
)
261+
262+
263+
@pytest.mark.skipif(sys.version_info >= (3, 8), reason='different between 3.7 and 3.8')
264+
def test_multiple_debugs_37():
265+
debug.format([i * 2 for i in range(2)])
266+
debug.format([i * 2 for i in range(2)])
267+
v = debug.format([i * 2 for i in range(2)])
268+
s = re.sub(r':\d{2,}', ':<line no>', str(v))
269+
assert s == (
270+
'tests/test_main.py:<line no> test_multiple_debugs_37\n'
271+
' [i * 2 for i in range(2)]: [0, 2] (list) len=2'
272+
)
273+
274+
275+
@pytest.mark.skipif(sys.version_info < (3, 8), reason='different between 3.7 and 3.8')
276+
def test_multiple_debugs_38():
277+
debug.format([i * 2 for i in range(2)])
278+
debug.format([i * 2 for i in range(2)])
279+
v = debug.format([i * 2 for i in range(2)])
280+
s = re.sub(r':\d{2,}', ':<line no>', str(v))
281+
# FIXME there's an extraneous bracket here, due to some error building code from the ast
282+
assert s == (
283+
'tests/test_main.py:<line no> test_multiple_debugs_38\n'
284+
' ([i * 2 for i in range(2)]: [0, 2] (list) len=2'
285+
)

0 commit comments

Comments
 (0)