From 0a89b4ee1a1f1424e358f7a17bed08b8cba6b4e3 Mon Sep 17 00:00:00 2001 From: Aviad Date: Thu, 12 Dec 2024 16:36:53 +0200 Subject: [PATCH 1/2] fixed missing coverage for one-line lambdas in 3.11 --- src/slipcover/slipcover.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/slipcover/slipcover.py b/src/slipcover/slipcover.py index 59a2b7a..d1007c9 100644 --- a/src/slipcover/slipcover.py +++ b/src/slipcover/slipcover.py @@ -34,8 +34,10 @@ def counter_total(self: Counter) -> int: _op_RETURN_GENERATOR = dis.opmap["RETURN_GENERATOR"] def findlinestarts(co: types.CodeType): - for off, line in dis.findlinestarts(co): - if line and co.co_code[off] not in (_op_RESUME, _op_RETURN_GENERATOR): + last_line = None + for off, _, line in co.co_lines(): + if line and line != last_line and co.co_code[off] not in (_op_RESUME, _op_RETURN_GENERATOR): + last_line = line yield off, line else: From 9c6ffbb1ee1872544d77d5abded5366568217a9e Mon Sep 17 00:00:00 2001 From: Aviad Date: Thu, 12 Dec 2024 21:39:02 +0200 Subject: [PATCH 2/2] added oneline lambda test --- tests/test_coverage.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_coverage.py b/tests/test_coverage.py index 49cee40..d736098 100644 --- a/tests/test_coverage.py +++ b/tests/test_coverage.py @@ -91,6 +91,35 @@ def foo(n): assert [] == cov['missing_lines'] +def test_oneline_lambda(): + sci = sc.Slipcover() + + base_line = current_line() + def foo(n): + n = ( + lambda: n + )() + + unused = \ + lambda: 24 + return n + + X = foo(42) + + sci.instrument(foo) + dis.dis(foo) + + assert X == foo(42) + + cov = sci.get_coverage() + assert {simple_current_file()} == cov['files'].keys() + + cov = cov['files'][simple_current_file()] + assert [2, 3, 6, 8] == [l-base_line for l in cov['executed_lines']] + assert [7] == cov['missing_lines'] + + + def test_exception(): sci = sc.Slipcover()