Skip to content

Commit

Permalink
fix: 3.11.0b4 has 0-numbered lines. Fixes #1419
Browse files Browse the repository at this point in the history
CPython added these lines in
python/cpython@1bfe83a
  • Loading branch information
nedbat committed Jul 12, 2022
1 parent 297b70e commit 0d64498
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

- Updated for a small change in Python 3.11.0 beta 4: modules now start with a
line with line number 0, which is ignored. This line cannnot be executed, so
coverage totals were thrown off. This line is now ignored by coverage.py,
but this also means that truly empty modules (like ``__init__.py``) have no
lines in them, rather than one phantom line. Fixes `issue 1419`_.

- Internal debugging data added to sys.modules is now an actual module, to
avoid confusing code that examines everything in sys.modules. Thanks,
Yilei Yang (`pull 1399`_).

.. _pull 1399: https://github.com/nedbat/coveragepy/pull/1399
.. _issue 1419: https://github.com/nedbat/coveragepy/issues/1419


.. _changes_6-4-1:
Expand Down
3 changes: 3 additions & 0 deletions coverage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class PYBEHAVIOR:
# Some words are keywords in some places, identifiers in other places.
soft_keywords = (PYVERSION >= (3, 10))

# Modules start with a line numbered zero. This means empty modules have
# only a 0-number line, which is ignored, giving a truly empty module.
empty_is_empty = (PYVERSION >= (3, 11, 0, 'beta', 4))

# Coverage.py specifics.

Expand Down
2 changes: 1 addition & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def _line_numbers(self):
"""
if hasattr(self.code, "co_lines"):
for _, _, line in self.code.co_lines():
if line is not None:
if line:
yield line
else:
# Adapted from dis.py in the standard library.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def test_what_is_the_sound_of_no_lines_clapping(self):
if env.JYTHON:
# Jython reports no lines for an empty file.
arcz_missing=".1 1." # pragma: only jython
elif env.PYBEHAVIOR.empty_is_empty:
arcz_missing=".1 1."
else:
# Other Pythons report one line.
arcz_missing=""
Expand Down
36 changes: 25 additions & 11 deletions tests/test_lcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tests.coveragetest import CoverageTest

import coverage
from coverage import env


class LcovTest(CoverageTest):
Expand Down Expand Up @@ -252,7 +253,8 @@ def test_empty_init_files(self):
line. It will also note the lack of branches, and the checksum for
the line.
Although there are no lines found, it will note one line as hit.
Although there are no lines found, it will note one line as hit in
old Pythons, and no lines hit in newer Pythons.
"""

self.make_file("__init__.py", "")
Expand All @@ -261,15 +263,27 @@ def test_empty_init_files(self):
self.start_import_stop(cov, "__init__")
cov.lcov_report()
self.assert_exists("coverage.lcov")
expected_result = textwrap.dedent("""\
TN:
SF:__init__.py
DA:1,1,1B2M2Y8AsgTpgAmY7PhCfg
LF:0
LH:1
BRF:0
BRH:0
end_of_record
""")
# Newer Pythons have truly empty empty files.
if env.PYBEHAVIOR.empty_is_empty:
expected_result = textwrap.dedent("""\
TN:
SF:__init__.py
LF:0
LH:0
BRF:0
BRH:0
end_of_record
""")
else:
expected_result = textwrap.dedent("""\
TN:
SF:__init__.py
DA:1,1,1B2M2Y8AsgTpgAmY7PhCfg
LF:0
LH:1
BRF:0
BRH:0
end_of_record
""")
actual_result = self.get_lcov_report_content()
assert actual_result == expected_result

0 comments on commit 0d64498

Please sign in to comment.