|
1 | | -import ctypes |
2 | | -from ctypes import wintypes |
3 | | - |
4 | | -kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) |
5 | | - |
6 | | -IsWow64Process2 = kernel32.IsWow64Process2 |
7 | | -IsWow64Process2.argtypes = [ |
8 | | - wintypes.HANDLE, |
9 | | - ctypes.POINTER(wintypes.USHORT), |
10 | | - ctypes.POINTER(wintypes.USHORT), |
11 | | -] |
12 | | -IsWow64Process2.restype = wintypes.BOOL |
13 | | - |
14 | | -IMAGE_FILE_MACHINE_UNKNOWN = 0x0000 |
15 | | -IMAGE_FILE_MACHINE_AMD64 = 0x8664 |
16 | | -IMAGE_FILE_MACHINE_ARM64 = 0xAA64 |
17 | | -IMAGE_FILE_MACHINE_I386 = 0x014c |
18 | | - |
19 | | -MACHINE = { |
20 | | - IMAGE_FILE_MACHINE_UNKNOWN: 'unknown', # native, no WOW64 |
21 | | - IMAGE_FILE_MACHINE_AMD64: 'amd64', |
22 | | - IMAGE_FILE_MACHINE_ARM64: 'arm64', |
23 | | - IMAGE_FILE_MACHINE_I386: 'x86', |
24 | | -} |
25 | | - |
26 | | -def platform(): |
27 | | - handle = kernel32.GetCurrentProcess() |
28 | | - |
29 | | - process = wintypes.USHORT() |
30 | | - native = wintypes.USHORT() |
31 | | - |
32 | | - r = IsWow64Process2(handle, ctypes.byref(process), ctypes.byref(native)) |
33 | | - if not r: |
34 | | - raise ctypes.WinError(ctypes.get_last_error()) |
35 | | - |
36 | | - return native.value, process.value, MACHINE[native.value], MACHINE[process.value] |
37 | | - |
38 | | - |
39 | | -def pp(): |
40 | | - import subprocess |
41 | | - |
42 | | - result = subprocess.run( |
43 | | - ["powershell", "-NoProfile", "-Command", "(Get-CimInstance Win32_Processor).Architecture"], |
44 | | - capture_output=True, text=True, check=True) |
45 | | - |
46 | | - return result |
| 1 | +import platform |
| 2 | +import sysconfig |
| 3 | + |
| 4 | + |
| 5 | +def _windows_interpreter_arch() -> str: |
| 6 | + """Return the Windows architecture for which the Python interpreter has been compiled.""" |
| 7 | + match sysconfig.get_platform(): |
| 8 | + case 'win32': |
| 9 | + return 'x86' |
| 10 | + case 'win-amd64': |
| 11 | + return 'amd64' |
| 12 | + case 'win-arm64': |
| 13 | + return 'arm64' |
| 14 | + raise ValueError |
| 15 | + |
| 16 | + |
| 17 | +def detect(): |
| 18 | + arch = _windows_interpreter_arch() |
| 19 | + nativearch = platform.machine().lower() |
| 20 | + cross = arch != nativearch |
| 21 | + print(f'arch={arch} native={nativearch} cross={cross}') |
0 commit comments