From 606e496dd6e2ace298532da200169124c26ae0f2 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Thu, 27 Jan 2022 11:57:43 +0200 Subject: bpo-40280: Use presence of msvcrt module to detect Windows (GH-30930) --- Lib/subprocess.py | 9 +++++++-- .../NEWS.d/next/Library/2022-01-16-14-07-14.bpo-40280.LtFHfF.rst | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 358f49a..ad08339 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -65,10 +65,15 @@ __all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "getstatusoutput", # NOTE: We intentionally exclude list2cmdline as it is # considered an internal implementation detail. issue10838. -_mswindows = sys.platform == "win32" +# use presence of msvcrt to detect Windows-like platforms (see bpo-8110) +try: + import msvcrt +except ModuleNotFoundError: + _mswindows = False +else: + _mswindows = True if _mswindows: - import msvcrt import _winapi from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, diff --git a/Misc/NEWS.d/next/Library/2022-01-16-14-07-14.bpo-40280.LtFHfF.rst b/Misc/NEWS.d/next/Library/2022-01-16-14-07-14.bpo-40280.LtFHfF.rst index b7bd7ab..f5d7676 100644 --- a/Misc/NEWS.d/next/Library/2022-01-16-14-07-14.bpo-40280.LtFHfF.rst +++ b/Misc/NEWS.d/next/Library/2022-01-16-14-07-14.bpo-40280.LtFHfF.rst @@ -1,4 +1,4 @@ :mod:`subprocess` now imports Windows-specific imports when -``sys.platform == "win32"`` and POSIX-specific imports on all other +``msvcrt`` module is available, and POSIX-specific imports on all other platforms. This gives a clean exception when ``_posixsubprocess`` is not -available (e.g. Emscripten browser target) and it's slightly faster, too. +available (e.g. Emscripten browser target). -- cgit v0.12