diff --git a/devtools/debug.py b/devtools/debug.py index d43ca54..a7b98b0 100644 --- a/devtools/debug.py +++ b/devtools/debug.py @@ -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 diff --git a/tests/test_main.py b/tests/test_main.py index 5dcd0a9..285d812 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -258,3 +258,28 @@ def __getattr__(self, item): " b: .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,}', ':', str(v)) + assert s == ( + 'tests/test_main.py: 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,}', ':', str(v)) + # FIXME there's an extraneous bracket here, due to some error building code from the ast + assert s == ( + 'tests/test_main.py: test_multiple_debugs_38\n' + ' ([i * 2 for i in range(2)]: [0, 2] (list) len=2' + )