Skip to content

Commit 7c2844c

Browse files
flying-sheepnedbat
authored andcommitted
fix: support PYTHONSAFEPATH #1696
Fix make test mostly work changes from code review changes from code review Fix lint error
1 parent ba685fb commit 7c2844c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

coverage/execfile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ def prepare(self) -> None:
8989
This needs to happen before any importing, and without importing anything.
9090
"""
9191
path0: str | None
92-
if self.as_module:
92+
if env.PYVERSION >= (3, 11) and os.environ.get('PYTHONSAFEPATH', ''):
93+
# See https://docs.python.org/3/using/cmdline.html#cmdoption-P
94+
path0 = None
95+
elif self.as_module:
9396
path0 = os.getcwd()
9497
elif os.path.isdir(self.arg0):
9598
# Running a directory means running the __main__.py file in that

tests/test_execfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import re
1515
import sys
1616

17+
from pathlib import Path
1718
from typing import Any
1819
from collections.abc import Iterator
1920

@@ -24,6 +25,7 @@
2425
from coverage.files import python_reported_file
2526

2627
from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
28+
from tests.helpers import change_dir
2729

2830
TRY_EXECFILE = os.path.join(TESTS_DIR, "modules/process_test/try_execfile.py")
2931

@@ -307,6 +309,15 @@ def test_pkg1_init(self) -> None:
307309
assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
308310
assert err == ""
309311

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+
310321
def test_no_such_module(self) -> None:
311322
with pytest.raises(NoSource, match="No module named '?i_dont_exist'?"):
312323
run_python_module(["i_dont_exist"])

tests/test_process.py

Lines changed: 19 additions & 0 deletions
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("No module named '?with_main'?", actual)
827+
assert re.search("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)