Skip to content

Commit bd90b20

Browse files
committed
In 3.10, modules always have firstlineno==1
1 parent 13f15b0 commit bd90b20

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

coverage/env.py

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class PYBEHAVIOR(object):
9292
# PyPy has always omitted statements after return.
9393
omit_after_return = omit_after_jump or PYPY
9494

95+
# Modules used to have firstlineno equal to the line number of the first
96+
# real line of code. Now they always start at 1.
97+
module_firstline_1 = pep626
98+
9599
# Coverage.py specifics.
96100

97101
# Are we using the C-implemented trace function?

coverage/parser.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ def _raw_parse(self):
205205
if not empty:
206206
self.raw_statements.update(self.byte_parser._find_statements())
207207

208+
# The first line of modules can lie and say 1 always, even if the first
209+
# line of code is later. If so, map 1 to the actual first line of the
210+
# module.
211+
if env.PYBEHAVIOR.module_firstline_1 and self._multiline:
212+
self._multiline[1] = min(self.raw_statements)
213+
208214
def first_line(self, line):
209215
"""Return the first line number of the statement including `line`."""
210216
if line < 0:
@@ -620,7 +626,9 @@ def _line__List(self, node):
620626
return node.lineno
621627

622628
def _line__Module(self, node):
623-
if node.body:
629+
if env.PYBEHAVIOR.module_firstline_1:
630+
return 1
631+
elif node.body:
624632
return self.line_for_node(node.body[0])
625633
else:
626634
# Empty modules have no line number, they always start at 1.

tests/coveragetest.py

+5
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ def check_coverage(
219219
self.fail("None of the missing choices matched %r" % missing_formatted)
220220

221221
if arcs is not None:
222+
# print("Possible arcs:")
223+
# print(" expected:", arcs)
224+
# print(" actual:", analysis.arc_possibilities())
225+
# print("Executed:")
226+
# print(" actual:", sorted(set(analysis.arcs_executed())))
222227
with self.delayed_assertions():
223228
self.assert_equal_arcs(
224229
arcs, analysis.arc_possibilities(),

tests/test_arcs.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ def test_simple_sequence(self):
2525
b = 3
2626
""",
2727
arcz=".1 13 3.")
28+
line1 = 1 if env.PYBEHAVIOR.module_firstline_1 else 2
2829
self.check_coverage("""\
2930
3031
a = 2
3132
b = 3
3233
3334
c = 5
3435
""",
35-
arcz="-22 23 35 5-2")
36+
arcz="-{0}2 23 35 5-{0}".format(line1)
37+
)
3638

3739
def test_function_def(self):
3840
self.check_coverage("""\

0 commit comments

Comments
 (0)