From f44a442341b10b04754d0f31f90e056516349cca Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 16 Jan 2024 16:17:50 +0100 Subject: [PATCH 1/3] gh-114107: test.pythoninfo logs Windows Developer Mode Also, don't skip the whole collect_windows() if ctypes is missing. --- Lib/test/pythoninfo.py | 49 ++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index 6dfb7f37e406a5..4ef52bd041c36a 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -865,26 +865,25 @@ def collect_subprocess(info_add): def collect_windows(info_add): + # windows.RtlAreLongPathsEnabled: RtlAreLongPathsEnabled() try: import ctypes + if not hasattr(ctypes, 'WinDLL'): + raise ImportError except ImportError: - return - - if not hasattr(ctypes, 'WinDLL'): - return - - ntdll = ctypes.WinDLL('ntdll') - BOOLEAN = ctypes.c_ubyte - - try: - RtlAreLongPathsEnabled = ntdll.RtlAreLongPathsEnabled - except AttributeError: - res = '' + pass else: - RtlAreLongPathsEnabled.restype = BOOLEAN - RtlAreLongPathsEnabled.argtypes = () - res = bool(RtlAreLongPathsEnabled()) - info_add('windows.RtlAreLongPathsEnabled', res) + ntdll = ctypes.WinDLL('ntdll') + BOOLEAN = ctypes.c_ubyte + try: + RtlAreLongPathsEnabled = ntdll.RtlAreLongPathsEnabled + except AttributeError: + res = '' + else: + RtlAreLongPathsEnabled.restype = BOOLEAN + RtlAreLongPathsEnabled.argtypes = () + res = bool(RtlAreLongPathsEnabled()) + info_add('windows.RtlAreLongPathsEnabled', res) try: import _winapi @@ -893,6 +892,7 @@ def collect_windows(info_add): except (ImportError, AttributeError): pass + # windows.version_caption: "wmic os get Caption,Version /value" command import subprocess try: # When wmic.exe output is redirected to a pipe, @@ -919,6 +919,7 @@ def collect_windows(info_add): if line: info_add('windows.version', line) + # windows.ver: "ver" command try: proc = subprocess.Popen(["ver"], shell=True, stdout=subprocess.PIPE, @@ -937,6 +938,22 @@ def collect_windows(info_add): if line: info_add('windows.ver', line) + # windows.developer_mode: get AllowDevelopmentWithoutDevLicense registry + import winreg + try: + key = winreg.OpenKey( + winreg.HKEY_LOCAL_MACHINE, + r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock") + subkey = "AllowDevelopmentWithoutDevLicense" + try: + value, value_type = winreg.QueryValueEx(key, subkey) + finally: + winreg.CloseKey(key) + except OSError: + pass + else: + info_add('windows.developer_mode', "enabled" if value else "disabled") + def collect_fips(info_add): try: From f2c2c020da1b55a08bd670afb22bfd3821ec073f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 16 Jan 2024 16:24:40 +0100 Subject: [PATCH 2/3] Fix test.pythoninfo on Linux --- Lib/test/pythoninfo.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index 4ef52bd041c36a..7cbd95a3fd50c7 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -865,6 +865,10 @@ def collect_subprocess(info_add): def collect_windows(info_add): + if sys.platform != "win32": + # Code specific to Windows + return + # windows.RtlAreLongPathsEnabled: RtlAreLongPathsEnabled() try: import ctypes From c0ea1176af3e7607e8487ae7cd0e9647a31eec98 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 16 Jan 2024 16:31:09 +0100 Subject: [PATCH 3/3] log also ctypes.windll.shell32.IsUserAnAdmin() --- Lib/test/pythoninfo.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index 7cbd95a3fd50c7..814358746d6d8a 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -870,6 +870,7 @@ def collect_windows(info_add): return # windows.RtlAreLongPathsEnabled: RtlAreLongPathsEnabled() + # windows.is_admin: IsUserAnAdmin() try: import ctypes if not hasattr(ctypes, 'WinDLL'): @@ -889,6 +890,12 @@ def collect_windows(info_add): res = bool(RtlAreLongPathsEnabled()) info_add('windows.RtlAreLongPathsEnabled', res) + shell32 = ctypes.windll.shell32 + IsUserAnAdmin = shell32.IsUserAnAdmin + IsUserAnAdmin.restype = BOOLEAN + IsUserAnAdmin.argtypes = () + info_add('windows.is_admin', IsUserAnAdmin()) + try: import _winapi dll_path = _winapi.GetModuleFileName(sys.dllhandle)