Skip to content

Commit ff09ce2

Browse files
committed
Issue python#9936: Fixed executable lines' search in the trace module.
1 parent 5e83da3 commit ff09ce2

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

Lib/test/test_trace.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ def test_linear_methods(self):
188188
}
189189
self.assertEqual(tracer.results().counts, expected)
190190

191-
192191
class TestRunExecCounts(unittest.TestCase):
193192
"""A simple sanity test of line-counting, via runctx (exec)"""
194193
def setUp(self):
@@ -285,8 +284,9 @@ def tearDown(self):
285284
rmtree(TESTFN)
286285
unlink(TESTFN)
287286

288-
def _coverage(self, tracer):
289-
tracer.run('from test import test_pprint; test_pprint.test_main()')
287+
def _coverage(self, tracer,
288+
cmd='from test import test_pprint; test_pprint.test_main()'):
289+
tracer.run(cmd)
290290
r = tracer.results()
291291
r.write_results(show_missing=True, summary=True, coverdir=TESTFN)
292292

@@ -313,6 +313,25 @@ def test_coverage_ignore(self):
313313
files = os.listdir(TESTFN)
314314
self.assertEquals(files, [])
315315

316+
def test_issue9936(self):
317+
tracer = trace.Trace(trace=0, count=1)
318+
modname = 'test.tracedmodules.testmod'
319+
# Ensure that the module is executed in import
320+
if modname in sys.modules:
321+
del sys.modules[modname]
322+
cmd = ("import test.tracedmodules.testmod as t;"
323+
"t.func(0); t.func2();")
324+
with captured_stdout() as stdout:
325+
self._coverage(tracer, cmd)
326+
stdout.seek(0)
327+
stdout.readline()
328+
coverage = {}
329+
for line in stdout:
330+
lines, cov, module = line.split()[:3]
331+
coverage[module] = (int(lines), int(cov[:-1]))
332+
self.assertIn(modname, coverage)
333+
self.assertEqual(coverage[modname], (5, 100))
334+
316335

317336
def test_main():
318337
run_unittest(__name__)

Lib/test/tracedmodules/testmod.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
def func(x):
22
b = x + 1
33
return b + 2
4+
5+
def func2():
6+
"""Test function for issue 9936 """
7+
return (1,
8+
2,
9+
3)

Lib/trace.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import tokenize
6060
import inspect
6161
import gc
62-
62+
import dis
6363
import pickle
6464

6565
def usage(outfile):
@@ -376,13 +376,7 @@ def find_lines_from_code(code, strs):
376376
"""Return dict where keys are lines in the line number table."""
377377
linenos = {}
378378

379-
line_increments = code.co_lnotab[1::2]
380-
table_length = len(line_increments)
381-
docstring = False
382-
383-
lineno = code.co_firstlineno
384-
for li in line_increments:
385-
lineno += li
379+
for _, lineno in dis.findlinestarts(code):
386380
if lineno not in strs:
387381
linenos[lineno] = 1
388382

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Core and Builtins
6868
Library
6969
-------
7070

71+
- Issue #9936: Fixed executable lines' search in the trace module.
72+
7173
- Issue #9790: Rework imports necessary for samefile and sameopenfile
7274
in ntpath.
7375

0 commit comments

Comments
 (0)