Skip to content

Commit c77f552

Browse files
authored
gh-114107: test.pythoninfo logs Windows Developer Mode (#114121)
Also, don't skip the whole collect_windows() if ctypes is missing. Log also ctypes.windll.shell32.IsUserAnAdmin().
1 parent 867f59f commit c77f552

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

Lib/test/pythoninfo.py

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -865,26 +865,36 @@ def collect_subprocess(info_add):
865865

866866

867867
def collect_windows(info_add):
868-
try:
869-
import ctypes
870-
except ImportError:
871-
return
872-
873-
if not hasattr(ctypes, 'WinDLL'):
868+
if sys.platform != "win32":
869+
# Code specific to Windows
874870
return
875871

876-
ntdll = ctypes.WinDLL('ntdll')
877-
BOOLEAN = ctypes.c_ubyte
878-
872+
# windows.RtlAreLongPathsEnabled: RtlAreLongPathsEnabled()
873+
# windows.is_admin: IsUserAnAdmin()
879874
try:
880-
RtlAreLongPathsEnabled = ntdll.RtlAreLongPathsEnabled
881-
except AttributeError:
882-
res = '<function not available>'
875+
import ctypes
876+
if not hasattr(ctypes, 'WinDLL'):
877+
raise ImportError
878+
except ImportError:
879+
pass
883880
else:
884-
RtlAreLongPathsEnabled.restype = BOOLEAN
885-
RtlAreLongPathsEnabled.argtypes = ()
886-
res = bool(RtlAreLongPathsEnabled())
887-
info_add('windows.RtlAreLongPathsEnabled', res)
881+
ntdll = ctypes.WinDLL('ntdll')
882+
BOOLEAN = ctypes.c_ubyte
883+
try:
884+
RtlAreLongPathsEnabled = ntdll.RtlAreLongPathsEnabled
885+
except AttributeError:
886+
res = '<function not available>'
887+
else:
888+
RtlAreLongPathsEnabled.restype = BOOLEAN
889+
RtlAreLongPathsEnabled.argtypes = ()
890+
res = bool(RtlAreLongPathsEnabled())
891+
info_add('windows.RtlAreLongPathsEnabled', res)
892+
893+
shell32 = ctypes.windll.shell32
894+
IsUserAnAdmin = shell32.IsUserAnAdmin
895+
IsUserAnAdmin.restype = BOOLEAN
896+
IsUserAnAdmin.argtypes = ()
897+
info_add('windows.is_admin', IsUserAnAdmin())
888898

889899
try:
890900
import _winapi
@@ -893,6 +903,7 @@ def collect_windows(info_add):
893903
except (ImportError, AttributeError):
894904
pass
895905

906+
# windows.version_caption: "wmic os get Caption,Version /value" command
896907
import subprocess
897908
try:
898909
# When wmic.exe output is redirected to a pipe,
@@ -919,6 +930,7 @@ def collect_windows(info_add):
919930
if line:
920931
info_add('windows.version', line)
921932

933+
# windows.ver: "ver" command
922934
try:
923935
proc = subprocess.Popen(["ver"], shell=True,
924936
stdout=subprocess.PIPE,
@@ -937,6 +949,22 @@ def collect_windows(info_add):
937949
if line:
938950
info_add('windows.ver', line)
939951

952+
# windows.developer_mode: get AllowDevelopmentWithoutDevLicense registry
953+
import winreg
954+
try:
955+
key = winreg.OpenKey(
956+
winreg.HKEY_LOCAL_MACHINE,
957+
r"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock")
958+
subkey = "AllowDevelopmentWithoutDevLicense"
959+
try:
960+
value, value_type = winreg.QueryValueEx(key, subkey)
961+
finally:
962+
winreg.CloseKey(key)
963+
except OSError:
964+
pass
965+
else:
966+
info_add('windows.developer_mode', "enabled" if value else "disabled")
967+
940968

941969
def collect_fips(info_add):
942970
try:

0 commit comments

Comments
 (0)