Skip to content

Commit 6db3d96

Browse files
pranavrajpalJukkaL
authored andcommitted
Avoid crashing on invalid python executables (#12812)
When an invalid python executable is passed to mypy, show an error message instead of crashing.
1 parent cb2c07b commit 6db3d96

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

mypy/modulefinder.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import sys
1313
from enum import Enum, unique
1414

15+
from mypy.errors import CompileError
16+
1517
if sys.version_info >= (3, 11):
1618
import tomllib
1719
else:
@@ -649,9 +651,15 @@ def get_site_packages_dirs(python_executable: Optional[str]) -> Tuple[List[str],
649651
else:
650652
# Use subprocess to get the package directory of given Python
651653
# executable
652-
site_packages = ast.literal_eval(
653-
subprocess.check_output([python_executable, pyinfo.__file__, 'getsitepackages'],
654-
stderr=subprocess.PIPE).decode())
654+
try:
655+
site_packages = ast.literal_eval(
656+
subprocess.check_output([python_executable, pyinfo.__file__, 'getsitepackages'],
657+
stderr=subprocess.PIPE).decode())
658+
except OSError as err:
659+
reason = os.strerror(err.errno)
660+
raise CompileError(
661+
[f"mypy: Invalid python executable '{python_executable}': {reason}"]
662+
) from err
655663
return expand_site_packages(site_packages)
656664

657665

0 commit comments

Comments
 (0)