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 9, 2023
1 parent 8fef6f0 commit bae06bf
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 7 deletions.
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

0 comments on commit bae06bf

Please sign in to comment.