From 5a609c4b15e8d47af233237ea88b6ee7b28ff18b Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 29 May 2020 17:10:31 +0100 Subject: [PATCH 1/3] fix sttatement finding for multiple debug calls in the same block --- devtools/debug.py | 8 ++------ tests/test_main.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/devtools/debug.py b/devtools/debug.py index d43ca54..2230bc1 100644 --- a/devtools/debug.py +++ b/devtools/debug.py @@ -321,19 +321,15 @@ def _statement_range(call_frame: 'FrameType', func_name: str) -> 'Tuple[int, int 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..b01136c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -258,3 +258,15 @@ def __getattr__(self, item): " b: .BadPretty object at 0x000> (BadPretty)\n" " !!! error pretty printing value: RuntimeError('this is an error')" ) + + +def test_multiple_debugs(): + 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\n' + ' ([i * 2 for i in range(2)]: [0, 2] (list) len=2' + ) From dde45da87ff34a593f39039b2fcbf73548beef76 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 29 May 2020 17:14:51 +0100 Subject: [PATCH 2/3] different for 3.8 and 3.7 --- tests/test_main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index b01136c..285d812 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -260,13 +260,26 @@ def __getattr__(self, item): ) -def test_multiple_debugs(): +@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\n' + 'tests/test_main.py: test_multiple_debugs_38\n' ' ([i * 2 for i in range(2)]: [0, 2] (list) len=2' ) From e594f0f0ad5b38d1c55a8ab9fe61a020ce7910c2 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 29 May 2020 17:18:12 +0100 Subject: [PATCH 3/3] coverage --- devtools/debug.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devtools/debug.py b/devtools/debug.py index 2230bc1..a7b98b0 100644 --- a/devtools/debug.py +++ b/devtools/debug.py @@ -317,7 +317,7 @@ 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