Skip to content

Commit

Permalink
Avoid crashing on invalid python executables (#12812)
Browse files Browse the repository at this point in the history
When an invalid python executable is passed to mypy, show an error
message instead of crashing.
  • Loading branch information
pranavrajpal committed May 19, 2022
1 parent 7f4f5b8 commit 6c2690e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions mypy/modulefinder.py
Expand Up @@ -12,6 +12,8 @@
import sys
from enum import Enum, unique

from mypy.errors import CompileError

if sys.version_info >= (3, 11):
import tomllib
else:
Expand Down Expand Up @@ -649,9 +651,15 @@ def get_site_packages_dirs(python_executable: Optional[str]) -> Tuple[List[str],
else:
# Use subprocess to get the package directory of given Python
# executable
site_packages = ast.literal_eval(
subprocess.check_output([python_executable, pyinfo.__file__, 'getsitepackages'],
stderr=subprocess.PIPE).decode())
try:
site_packages = ast.literal_eval(
subprocess.check_output([python_executable, pyinfo.__file__, 'getsitepackages'],
stderr=subprocess.PIPE).decode())
except OSError as err:
reason = os.strerror(err.errno)
raise CompileError(
[f"mypy: Invalid python executable '{python_executable}': {reason}"]
) from err
return expand_site_packages(site_packages)


Expand Down

0 comments on commit 6c2690e

Please sign in to comment.