Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid crashing on invalid python executables #12812

Merged
merged 1 commit into from
May 19, 2022

Conversation

pranavrajpal
Copy link
Contributor

Description

When an invalid python executable is passed to mypy, show an error message instead of crashing with a FileNotFoundError or some other exception.

The implementation of the error raises a CompileError instead of using the normal error handling from mypy.errors.Errors, similar to what we do for nonexistent files being passed to mypy:

mypy/mypy/build.py

Lines 2058 to 2061 in 8e7e817

raise CompileError([
"mypy: can't read file '{}': {}".format(
self.path, os.strerror(ioerr.errno))],
module_with_blocker=self.id) from ioerr

I used that approach because the relevant code didn't have access to an Errors instance and there probably isn't much reason to support continuing to check code when the python_executable is incorrect.

Test Plan

I couldn't reproduce the crash in the test suite (I'm guessing that's because we don't try looking for site-packages in the test suite, but I haven't confirmed that), so I tested this manually by running mypy empty.py --python-executable nonexistent, where empty.py is an empty file.

On master (8e7e817):

Traceback (most recent call last):
  File "/home/pranav/.local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/build.py", line 181, in build
    result = _build(
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/build.py", line 213, in _build
    search_paths = compute_search_paths(sources, options, data_dir, alt_lib_path)
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/modulefinder.py", line 788, in compute_search_paths
    egg_dirs, site_packages = get_site_packages_dirs(options.python_executable)
  File "/home/pranav/.local/lib/python3.8/site-packages/mypy/modulefinder.py", line 653, in get_site_packages_dirs
    subprocess.check_output([python_executable, pyinfo.__file__, 'getsitepackages'],
  File "/usr/lib/python3.8/subprocess.py", line 415, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/lib/python3.8/subprocess.py", line 493, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent'

With this PR:

mypy: Invalid python executable 'nonexistent': No such file or directory

When an invalid python executable is passed to mypy, show an error
message instead of crashing.
@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is much more friendlier than showing a traceback.

@JukkaL JukkaL merged commit 6c2690e into python:master May 19, 2022
@pranavrajpal pranavrajpal deleted the invalid-python-executable branch May 19, 2022 15:57
JukkaL pushed a commit that referenced this pull request May 20, 2022
When an invalid python executable is passed to mypy, show an error
message instead of crashing.
@JukkaL JukkaL mentioned this pull request May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants