Skip to content

Commit

Permalink
fix: isolate user code from coverage.py internal code flags. #1524
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 10, 2023
1 parent 8fef6f0 commit 880a64a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

Nothing yet.
- Fix: On Python 3.7, a file with type annotations but no ``from __future__
import annotations`` would be missing statements in the coverage report. This
is now fixed, closing `issue 1524`_.

.. _issue 1524: https://github.com/nedbat/coveragepy/issues/1524


.. _changes_7-0-4:
Expand Down
2 changes: 1 addition & 1 deletion coverage/execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def make_code_from_py(filename):
except (OSError, NoSource) as exc:
raise NoSource(f"No file to run: '{filename}'") from exc

return compile(source, filename, "exec")
return compile(source, filename, "exec", dont_inherit=True)


def make_code_from_pyc(filename):
Expand Down
2 changes: 1 addition & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def __init__(
else:
assert filename is not None
try:
self.code = compile(text, filename, "exec")
self.code = compile(text, filename, "exec", dont_inherit=True)
except SyntaxError as synerr:
raise NotPython(
"Couldn't parse '%s' as Python source: '%s' at line %d" % (
Expand Down
2 changes: 1 addition & 1 deletion lab/genpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def show_a_bunch():
source = PythonSpinner.generate_python(maker.make_body("def"))
try:
print("-"*80, "\n", source, sep="")
compile(source, "<string>", "exec")
compile(source, "<string>", "exec", dont_inherit=True)
except Exception as ex:
print(f"Oops: {ex}\n{source}")
if len(source) > len(longest):
Expand Down
2 changes: 1 addition & 1 deletion lab/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def all_code_objects(code):
def disassemble(pyparser):
"""Disassemble code, for ad-hoc experimenting."""

code = compile(pyparser.text, "", "exec")
code = compile(pyparser.text, "", "exec", dont_inherit=True)
for code_obj in all_code_objects(code):
if pyparser.text:
srclines = pyparser.text.splitlines()
Expand Down
2 changes: 1 addition & 1 deletion lab/show_pyc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def show_py_file(fname):
show_py_text(text, fname=fname)

def show_py_text(text, fname="<string>"):
code = compile(text, fname, "exec")
code = compile(text, fname, "exec", dont_inherit=True)
show_code(code)

CO_FLAGS = [
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def better_set_verbosity(v):
# Keep pylint happy.
__version__ = __url__ = version_info = ""
# Execute the code in version.py.
exec(compile(version_file.read(), cov_ver_py, 'exec'))
exec(compile(version_file.read(), cov_ver_py, 'exec', dont_inherit=True))

with open("README.rst") as readme:
readme_text = readme.read()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def cmd_executes(
code = textwrap.dedent(code)
expected = self.model_object()
globs = {n: getattr(expected, n) for n in self.MOCK_GLOBALS}
code_obj = compile(code, "<code>", "exec")
code_obj = compile(code, "<code>", "exec", dont_inherit=True)
eval(code_obj, globs, {}) # pylint: disable=eval-used

# Many of our functions take a lot of arguments, and cmdline.py
Expand Down
16 changes: 16 additions & 0 deletions tests/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,22 @@ def missing(x, y):
assert self.get_report(cov, output_format="total", precision=2) == "78.57\n"
assert self.get_report(cov, output_format="total", precision=4) == "78.5714\n"

def test_bug_1524(self) -> None:
self.make_file("bug1524.py", """\
class Mine:
@property
def thing(self) -> int:
return 17
print(Mine().thing)
""")
cov = coverage.Coverage()
self.start_import_stop(cov, "bug1524")
assert self.stdout() == "17\n"
report = self.get_report(cov)
report_lines = report.splitlines()
assert report_lines[2] == "bug1524.py 5 0 100%"


class ReportingReturnValueTest(CoverageTest):
"""Tests of reporting functions returning values."""
Expand Down

0 comments on commit 880a64a

Please sign in to comment.