File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -90,7 +90,10 @@ def prepare(self) -> None:
90
90
This needs to happen before any importing, and without importing anything.
91
91
"""
92
92
path0 : Optional [str ]
93
- if self .as_module :
93
+ if os .environ .get ('PYTHONSAFEPATH' , '' ):
94
+ # See https://docs.python.org/3/using/cmdline.html#cmdoption-P
95
+ path0 = None
96
+ elif self .as_module :
94
97
path0 = os .getcwd ()
95
98
elif os .path .isdir (self .arg0 ):
96
99
# Running a directory means running the __main__.py file in that
Original file line number Diff line number Diff line change 13
13
import py_compile
14
14
import re
15
15
import sys
16
-
16
+ from unittest import mock
17
17
from typing import Any , Iterator
18
18
19
19
import pytest
@@ -306,6 +306,14 @@ def test_pkg1_init(self) -> None:
306
306
assert out == "pkg1.__init__: pkg1\n pkg1.__init__: __main__\n "
307
307
assert err == ""
308
308
309
+ def test_pythonpath (self ) -> None :
310
+ with mock .patch .dict (os .environ , {"PYTHONSAFEPATH" : "1" }):
311
+ run_python_module ([TRY_EXECFILE ])
312
+ out , err = self .stdouterr ()
313
+ mod_globs = json .loads (out )
314
+ assert os .cwd () not in mod_globs ["path" ]
315
+ assert err == ""
316
+
309
317
def test_no_such_module (self ) -> None :
310
318
with pytest .raises (NoSource , match = "No module named '?i_dont_exist'?" ):
311
319
run_python_module (["i_dont_exist" ])
You can’t perform that action at this time.
0 commit comments