Skip to content

Commit 66d3d34

Browse files
committed
finish nedbat#1700
1 parent 28a62e7 commit 66d3d34

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23+
- Fix: the PYTHONSAFEPATH environment variable new in Python 3.11 is properly
24+
supported, closing `issue 1696`_. Thanks, `Philipp A. <pull 1700_>`_.
25+
2326
- Added new :ref:`debug options <cmd_run_debug>`:
2427

2528
- ``pytest`` writes the pytest test name into the debug output.
2629

2730
- ``dataop2`` writes the full data being added to CoverageData objects.
2831

32+
.. _issue 1696: https://github.com/nedbat/coveragepy/issues/1696
33+
.. _pull 1700: https://github.com/nedbat/coveragepy/pull/1700
2934

3035
.. scriv-start-here
3136

coverage/execfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def prepare(self) -> None:
9090
This needs to happen before any importing, and without importing anything.
9191
"""
9292
path0: Optional[str]
93-
if env.PYVERSION >= (3, 11) and os.environ.get('PYTHONSAFEPATH', ''):
93+
if env.PYVERSION >= (3, 11) and os.getenv("PYTHONSAFEPATH"):
9494
# See https://docs.python.org/3/using/cmdline.html#cmdoption-P
9595
path0 = None
9696
elif self.as_module:

tests/test_execfile.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,6 @@ def test_pkg1_init(self) -> None:
307307
assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
308308
assert err == ""
309309

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

tests/test_process.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,25 @@ def test_coverage_zip_is_like_python(self) -> None:
786786
actual = self.run_command(f"python {cov_main} run run_me.py")
787787
self.assert_tryexecfile_output(expected, actual)
788788

789+
def test_pythonsafepath(self) -> None:
790+
with open(TRY_EXECFILE) as f:
791+
self.make_file("run_me.py", f.read())
792+
self.set_environ("PYTHONSAFEPATH", "1")
793+
expected = self.run_command("python run_me.py")
794+
actual = self.run_command("coverage run run_me.py")
795+
self.assert_tryexecfile_output(expected, actual)
796+
797+
@pytest.mark.skipif(env.PYVERSION < (3, 11), reason="PYTHONSAFEPATH is new in 3.11")
798+
def test_pythonsafepath_dashm(self) -> None:
799+
with open(TRY_EXECFILE) as f:
800+
self.make_file("with_main/__main__.py", f.read())
801+
802+
self.set_environ("PYTHONSAFEPATH", "1")
803+
expected = self.run_command("python -m with_main")
804+
actual = self.run_command("coverage run -m with_main")
805+
assert re.search(f"No module named '?with_main'?", actual)
806+
assert re.search(f"No module named '?with_main'?", expected)
807+
789808
def test_coverage_custom_script(self) -> None:
790809
# https://github.com/nedbat/coveragepy/issues/678
791810
# If sys.path[0] isn't the Python default, then coverage.py won't

0 commit comments

Comments
 (0)