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

Fixes #16669 #17275

Merged
merged 2 commits into from
May 21, 2024
Merged

Fixes #16669 #17275

merged 2 commits into from
May 21, 2024

Conversation

alexlshon
Copy link
Contributor

This Pull Request Fixes #16669 . This pull request makes a one line change to mypy/pyinfo.py to force the stdout of the python process to use utf-8.

One can replicate this error in Windows using Python3.8 just by calling the mypy/pyinfo.py module using a slightly modified code of the get_search_dirs function where the python executable doesn't match the value of sys.executable. The only modification made to this code from get_search_dirs is the adding of a non-ascii-path to the env parameter to subprocess.

    python_executable: str = r"<your_path_to_python_executable>\python.exe"
    non_ascii_path = r"<any_non_ascii_path>"
    env = {**dict(os.environ), "PYTHONSAFEPATH": "1"}
    env.update({"PYTHONPATH": non_ascii_path}) 
 # all code below is the same as get_search_dirs
    try:
        sys_path, site_packages = ast.literal_eval(
            subprocess.check_output(
                [python_executable, pyinfo.__file__, "getsearchdirs"],
                env=env,
                stderr=subprocess.PIPE,
            ).decode(encoding="utf-8")
        )
    except subprocess.CalledProcessError as err:
        print(err.stderr)
        print(err.stdout)
        raise
    except OSError as err:
        reason = os.strerror(err.errno)
        raise CompileError(
             [f"mypy: Invalid python executable '{python_executable}': {reason}"]
        ) from err

Running this code with out the fix returns this error in windows
image

Running pyinfo.py after forcing stdout to use utf-8 returns the expected value for sys_path and site_packages

mypy/pyinfo.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Awesome, thank you!

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@hauntsaninja hauntsaninja merged commit 2892ed4 into python:master May 21, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when get_search_dirs called with sys.path containing non-ascii characters on Windows
2 participants