Skip to content

Commit 4c59f00

Browse files
committed
finish #1700
1 parent 3f5846f commit 4c59f00

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

CHANGES.rst

+5
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ Unreleased
3333
reduce the overhead coverage.py imposes on your test suite. Set the
3434
environment variable ``COVERAGE_CORE=sysmon`` to try it out.
3535

36+
- Fix: the PYTHONSAFEPATH environment variable new in Python 3.11 is properly
37+
supported, closing `issue 1696`_. Thanks, `Philipp A. <pull 1700_>`_.
38+
3639
- The :class:`Coverage constructor<.Coverage>` now has a ``plugins`` parameter
3740
for passing in plugin objects directly, thanks to `Alex Gaynor <pull
3841
1919_>`_.
3942

4043
- Confirmed support for PyPy 3.11. Thanks Michał Górny.
4144

45+
.. _issue 1696: https://github.com/nedbat/coveragepy/issues/1696
46+
.. _pull 1700: https://github.com/nedbat/coveragepy/pull/1700
4247
.. _pull 1919: https://github.com/nedbat/coveragepy/pull/1919
4348

4449

coverage/execfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def prepare(self) -> None:
8989
This needs to happen before any importing, and without importing anything.
9090
"""
9191
path0: str | None
92-
if env.PYVERSION >= (3, 11) and os.environ.get('PYTHONSAFEPATH', ''):
92+
if env.PYVERSION >= (3, 11) and os.getenv("PYTHONSAFEPATH"):
9393
# See https://docs.python.org/3/using/cmdline.html#cmdoption-P
9494
path0 = None
9595
elif self.as_module:

tests/test_execfile.py

-9
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,6 @@ def test_pkg1_init(self) -> None:
309309
assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
310310
assert err == ""
311311

312-
def test_pythonpath(self, tmp_path: Path) -> None:
313-
self.set_environ("PYTHONSAFEPATH", "1")
314-
with change_dir(tmp_path):
315-
run_python_module(["process_test.try_execfile"])
316-
out, err = self.stdouterr()
317-
mod_globs = json.loads(out)
318-
assert tmp_path not in mod_globs["path"]
319-
assert err == ""
320-
321312
def test_no_such_module(self) -> None:
322313
with pytest.raises(NoSource, match="No module named '?i_dont_exist'?"):
323314
run_python_module(["i_dont_exist"])

tests/test_process.py

+19
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,25 @@ def test_coverage_zip_is_like_python(self) -> None:
807807
actual = self.run_command(f"python {cov_main} run run_me.py")
808808
self.assert_tryexecfile_output(expected, actual)
809809

810+
def test_pythonsafepath(self) -> None:
811+
with open(TRY_EXECFILE) as f:
812+
self.make_file("run_me.py", f.read())
813+
self.set_environ("PYTHONSAFEPATH", "1")
814+
expected = self.run_command("python run_me.py")
815+
actual = self.run_command("coverage run run_me.py")
816+
self.assert_tryexecfile_output(expected, actual)
817+
818+
@pytest.mark.skipif(env.PYVERSION < (3, 11), reason="PYTHONSAFEPATH is new in 3.11")
819+
def test_pythonsafepath_dashm(self) -> None:
820+
with open(TRY_EXECFILE) as f:
821+
self.make_file("with_main/__main__.py", f.read())
822+
823+
self.set_environ("PYTHONSAFEPATH", "1")
824+
expected = self.run_command("python -m with_main")
825+
actual = self.run_command("coverage run -m with_main")
826+
assert re.search(f"No module named '?with_main'?", actual)
827+
assert re.search(f"No module named '?with_main'?", expected)
828+
810829
def test_coverage_custom_script(self) -> None:
811830
# https://github.com/nedbat/coveragepy/issues/678
812831
# If sys.path[0] isn't the Python default, then coverage.py won't

0 commit comments

Comments
 (0)