Skip to content

Commit 0dafc47

Browse files
eevelweezeleevelhauntsaninjaAlexWaygood
authored
15110: set env var for subprocess, check version in script (#15122)
Fixes #15110 - Add PYTHONSAFEPATH="true" to call to subprocess in get_search_dirs() - Check sys.version_info before modifying path in pyinfo.py To test, import mypy.modulefinder.get_search_dirs & run: - get_search_dirs() with no args - on Python 3.11, call get_search_dirs with an earlier version as python_executable - on Python < 3.11, call get_search_dirs with Python 3.11 as python_executable Expected: - consistent results, no exceptions Co-authored-by: eevel <eevel@weezel3.weezelnet> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent e4217f2 commit 0dafc47

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

mypy/modulefinder.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,19 @@ def get_search_dirs(python_executable: str | None) -> tuple[list[str], list[str]
751751
else:
752752
# Use subprocess to get the package directory of given Python
753753
# executable
754+
env = {**dict(os.environ), "PYTHONSAFEPATH": "1"}
754755
try:
755756
sys_path, site_packages = ast.literal_eval(
756757
subprocess.check_output(
757-
[python_executable, pyinfo.__file__, "getsearchdirs"], stderr=subprocess.PIPE
758+
[python_executable, pyinfo.__file__, "getsearchdirs"],
759+
env=env,
760+
stderr=subprocess.PIPE,
758761
).decode()
759762
)
763+
except subprocess.CalledProcessError as err:
764+
print(err.stderr)
765+
print(err.stdout)
766+
raise
760767
except OSError as err:
761768
reason = os.strerror(err.errno)
762769
raise CompileError(

mypy/pyinfo.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
if __name__ == "__main__":
1313
# HACK: We don't want to pick up mypy.types as the top-level types
1414
# module. This could happen if this file is run as a script.
15-
# This workaround fixes it.
16-
old_sys_path = sys.path
17-
sys.path = sys.path[1:]
18-
import types # noqa: F401
15+
# This workaround fixes this for Python versions before 3.11.
16+
if sys.version_info < (3, 11):
17+
old_sys_path = sys.path
18+
sys.path = sys.path[1:]
19+
import types # noqa: F401
1920

20-
sys.path = old_sys_path
21+
sys.path = old_sys_path
2122

2223
import os
2324
import site

0 commit comments

Comments
 (0)