Skip to content

Try to avoid invoking a new process to determine the Windows version in the platform module #129294

Description

@tomergert

Feature or enhancement

Proposal:

In Lib/platform.py, there's a code aimed to determine the Windows version python runs in.

    for cmd in ('ver', 'command /c ver', 'cmd /c ver'):
        try:
            info = subprocess.check_output(cmd,
                                           stdin=subprocess.DEVNULL,
                                           stderr=subprocess.DEVNULL,
                                           text=True,
                                           encoding="locale",
                                           shell=True)

As much as I understand, one shouldn't invoke whole new process just to question what OS version he runs at.

so just before invoking cmd.exe, we will try to question winapi directly.

# Load ntdll.dll and call RtlGetVersion
    ntdll = ctypes.WinDLL("ntdll")
    rtl_get_version = ntdll.RtlGetVersion
    rtl_get_version(ctypes.byref(os_version))

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. zooba commented on Jan 27, 2025

    @zooba
    Member

    This is already fallback code for if WMI doesn't work, and GetVersionEx is already provided by sys.getwindowsversion.

    I don't think there's anything to change here.

  2. tomergert commented on Jan 28, 2025

    @tomergert
    Author

    This is already fallback code for if WMI doesn't work, and GetVersionEx is already provided by sys.getwindowsversion.

    I don't think there's anything to change here.

    Thank you,. I addressed your comment in the PR
    #129295 (comment)

  3. zware commented on Jul 18, 2026

    @zware
    Member

    In agreement with Steve, I'm going to close this issue. We may be at the point that we can remove the subprocess-based fallback entirely, but being the fallback fallback I'm not sure it's worth a change. Either way it's not a pressing concern, or a performance issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions