diff --git a/coverage/execfile.py b/coverage/execfile.py index 02c5b9aa6..953db2ead 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -90,7 +90,7 @@ def prepare(self) -> None: This needs to happen before any importing, and without importing anything. """ path0: Optional[str] - if os.environ.get('PYTHONSAFEPATH', ''): + if env.PYVERSION >= (3, 11) and os.environ.get('PYTHONSAFEPATH', ''): # See https://docs.python.org/3/using/cmdline.html#cmdoption-P path0 = None elif self.as_module: diff --git a/tests/test_execfile.py b/tests/test_execfile.py index 116f915ac..0d575fcce 100644 --- a/tests/test_execfile.py +++ b/tests/test_execfile.py @@ -13,7 +13,7 @@ import py_compile import re import sys -from unittest import mock +from pathlib import Path from typing import Any, Iterator import pytest @@ -307,9 +307,9 @@ def test_pkg1_init(self) -> None: assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n" assert err == "" - def test_pythonpath(self, tmp_path) -> None: - env = {"PYTHONSAFEPATH": "1"} - with mock.patch.dict(os.environ, env), change_dir(tmp_path): + 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)