Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PYTHONSAFEPATH #1700

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ upgrading your version of coverage.py.
Unreleased
----------

Nothing yet.
- Fix: the PYTHONSAFEPATH environment variable new in Python 3.11 is properly
supported, closing `issue 1696`_. Thanks, `Philipp A. <pull 1700_>`_.


.. scriv-start-here
Expand Down Expand Up @@ -281,6 +282,8 @@ Version 7.3.3 — 2023-12-14

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

.. _issue 1696: https://github.com/nedbat/coveragepy/issues/1696
.. _pull 1700: https://github.com/nedbat/coveragepy/pull/1700
.. _issue 684: https://github.com/nedbat/coveragepy/issues/684
.. _pull 1705: https://github.com/nedbat/coveragepy/pull/1705
.. _issue 1709: https://github.com/nedbat/coveragepy/issues/1709
Expand Down
5 changes: 4 additions & 1 deletion coverage/execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def prepare(self) -> None:
This needs to happen before any importing, and without importing anything.
"""
path0: str | None
if self.as_module:
if env.PYVERSION >= (3, 11) and os.getenv('PYTHONSAFEPATH'):
# See https://docs.python.org/3/using/cmdline.html#cmdoption-P
path0 = None
elif self.as_module:
path0 = os.getcwd()
elif os.path.isdir(self.arg0):
# Running a directory means running the __main__.py file in that
Expand Down
12 changes: 11 additions & 1 deletion tests/test_execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import py_compile
import re
import sys

from pathlib import Path
from typing import Any, Iterator

import pytest
Expand All @@ -23,6 +23,7 @@
from coverage.files import python_reported_file

from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
from tests.helpers import change_dir

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

Expand Down Expand Up @@ -306,6 +307,15 @@ def test_pkg1_init(self) -> None:
assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
assert err == ""

def test_pythonpath(self, tmp_path: Path) -> None:
self.set_environ("PYTHONSAFEPATH", "1")
with change_dir(tmp_path):
run_python_module(["process_test.try_execfile"])
out, err = self.stdouterr()
mod_globs = json.loads(out)
assert tmp_path not in mod_globs["path"]
assert err == ""

def test_no_such_module(self) -> None:
with pytest.raises(NoSource, match="No module named '?i_dont_exist'?"):
run_python_module(["i_dont_exist"])
Expand Down
19 changes: 19 additions & 0 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,25 @@ def test_coverage_zip_is_like_python(self) -> None:
actual = self.run_command(f"python {cov_main} run run_me.py")
self.assert_tryexecfile_output(expected, actual)

def test_pythonsafepath(self) -> None:
with open(TRY_EXECFILE) as f:
self.make_file("run_me.py", f.read())
self.set_environ("PYTHONSAFEPATH", "1")
expected = self.run_command("python run_me.py")
actual = self.run_command("coverage run run_me.py")
self.assert_tryexecfile_output(expected, actual)

@pytest.mark.skipif(env.PYVERSION < (3, 11), reason="PYTHONSAFEPATH is new in 3.11")
def test_pythonsafepath_dashm(self) -> None:
with open(TRY_EXECFILE) as f:
self.make_file("with_main/__main__.py", f.read())

self.set_environ("PYTHONSAFEPATH", "1")
expected = self.run_command("python -m with_main")
actual = self.run_command("coverage run -m with_main")
assert re.search("No module named '?with_main'?", actual)
assert re.search("No module named '?with_main'?", expected)

def test_coverage_custom_script(self) -> None:
# https://github.com/nedbat/coveragepy/issues/678
# If sys.path[0] isn't the Python default, then coverage.py won't
Expand Down
Loading