Skip to content

Commit dbcfe75

Browse files
authored
Revert "fix(sysinfo): report host architecture instead of Python process arch (fixes #58)" (#468)
Reverts #463
1 parent f535844 commit dbcfe75

2 files changed

Lines changed: 46 additions & 110 deletions

File tree

src/winml/modelkit/commands/sys.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import json
2727
import logging
28-
import os
2928
import platform
3029
import sys
3130
from typing import Any
@@ -54,31 +53,26 @@ def _get_python_info() -> dict[str, Any]:
5453

5554
def _get_platform_info() -> dict[str, Any]:
5655
"""Gather OS and platform information."""
56+
system = platform.system()
5757
release = platform.release()
5858

59-
# platform.release() may incorrectly report '10' for Windows 11 on some
60-
# Python versions; OS class queries the build number for accurate detection.
61-
try:
62-
if OS.get().is_windows_11():
63-
release = "11"
64-
except Exception:
65-
# Fallback to platform.release() if OS detection fails
66-
pass
67-
68-
# platform.machine() reports the running Python process architecture.
69-
# On ARM64 Windows running an x64 Python under WOW64 emulation,
70-
# PROCESSOR_ARCHITEW6432 holds the true host arch; otherwise
71-
# PROCESSOR_ARCHITECTURE matches the host.
72-
machine = (
73-
os.environ.get("PROCESSOR_ARCHITEW6432")
74-
or os.environ.get("PROCESSOR_ARCHITECTURE")
75-
or platform.machine()
76-
)
59+
# For Windows, use OS class for accurate Windows 11 detection
60+
# platform.release() may incorrectly report '10' on some Python versions
61+
if system == "Windows":
62+
try:
63+
os_info = OS.get()
64+
# Only override if it's actually Windows 11
65+
# Otherwise keep the original platform.release() value
66+
if os_info.is_windows_11():
67+
release = "11"
68+
except Exception:
69+
# Fallback to platform.release() if OS detection fails
70+
pass
7771

7872
return {
79-
"system": platform.system(),
73+
"system": system,
8074
"release": release,
81-
"machine": machine,
75+
"machine": platform.machine(),
8276
"processor": platform.processor() or "Unknown",
8377
}
8478

tests/unit/sysinfo/test_sysinfo.py

Lines changed: 31 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from __future__ import annotations
1111

12-
import os
1312
from unittest.mock import MagicMock, patch
1413

1514

@@ -129,108 +128,51 @@ def test_windows_detection_fallback_on_exception(
129128
assert result["release"] == "10"
130129
assert result["machine"] == "AMD64"
131130

132-
@patch("winml.modelkit.commands.sys.OS")
133-
@patch("winml.modelkit.commands.sys.platform")
134-
def test_processor_unknown_fallback(
135-
self, mock_platform: MagicMock, mock_os_class: MagicMock
136-
) -> None:
137-
"""Test processor defaults to 'Unknown' when empty."""
138-
from winml.modelkit.commands.sys import _get_platform_info
139-
140-
mock_platform.system.return_value = "Windows"
141-
mock_platform.release.return_value = "10"
142-
mock_platform.machine.return_value = "AMD64"
143-
mock_platform.processor.return_value = "" # Empty string
144-
145-
mock_os_instance = MagicMock()
146-
mock_os_instance.is_windows_11.return_value = False
147-
mock_os_class.get.return_value = mock_os_instance
148-
149-
with patch.dict(os.environ, {"PROCESSOR_ARCHITECTURE": "AMD64"}, clear=True):
150-
result = _get_platform_info()
151-
152-
assert result["processor"] == "Unknown"
153-
154-
155-
class TestWindowsHostArchitecture:
156-
"""Test that Windows machine architecture reports the host, not the Python process arch.
157-
158-
Regression coverage for https://github.com/microsoft/WinML-ModelKit/issues/58:
159-
`platform.machine()` returns the running Python process architecture, which is
160-
incorrect on ARM64 Windows when an x64 Python runs under WOW64 emulation.
161-
"""
162-
163-
@patch("winml.modelkit.commands.sys.OS")
164131
@patch("winml.modelkit.commands.sys.platform")
165-
def test_native_arm64_python_reports_arm64(
166-
self, mock_platform: MagicMock, mock_os_class: MagicMock
167-
) -> None:
168-
"""Native ARM64 Python on ARM64 Windows must report ARM64."""
132+
def test_non_windows_platform(self, mock_platform: MagicMock) -> None:
133+
"""Test non-Windows platforms pass through unchanged."""
169134
from winml.modelkit.commands.sys import _get_platform_info
170135

171-
mock_platform.system.return_value = "Windows"
172-
mock_platform.release.return_value = "10"
173-
mock_platform.machine.return_value = "ARM64"
174-
mock_platform.processor.return_value = "ARMv8 (64-bit)"
175-
176-
mock_os_instance = MagicMock()
177-
mock_os_instance.is_windows_11.return_value = True
178-
mock_os_class.get.return_value = mock_os_instance
136+
# Setup mocks for Linux
137+
mock_platform.system.return_value = "Linux"
138+
mock_platform.release.return_value = "5.15.0"
139+
mock_platform.machine.return_value = "x86_64"
140+
mock_platform.processor.return_value = "x86_64"
179141

180-
with patch.dict(os.environ, {"PROCESSOR_ARCHITECTURE": "ARM64"}, clear=True):
181-
result = _get_platform_info()
142+
result = _get_platform_info()
182143

183-
assert result["machine"] == "ARM64"
144+
assert result["system"] == "Linux"
145+
assert result["release"] == "5.15.0"
146+
assert result["machine"] == "x86_64"
184147

185-
@patch("winml.modelkit.commands.sys.OS")
186148
@patch("winml.modelkit.commands.sys.platform")
187-
def test_x64_emulated_python_on_arm64_reports_arm64(
188-
self, mock_platform: MagicMock, mock_os_class: MagicMock
189-
) -> None:
190-
"""x64-emulated Python on ARM64 Windows must report ARM64, not AMD64.
191-
192-
Under WOW64 emulation, PROCESSOR_ARCHITECTURE reflects the emulated
193-
process arch (AMD64) and PROCESSOR_ARCHITEW6432 holds the true host
194-
arch (ARM64). The host arch is what users want to see.
195-
"""
149+
def test_macos_platform(self, mock_platform: MagicMock) -> None:
150+
"""Test macOS platforms pass through unchanged."""
196151
from winml.modelkit.commands.sys import _get_platform_info
197152

198-
mock_platform.system.return_value = "Windows"
199-
mock_platform.release.return_value = "11"
200-
mock_platform.machine.return_value = "AMD64"
201-
mock_platform.processor.return_value = "Intel64 Family 6"
153+
# Setup mocks for macOS
154+
mock_platform.system.return_value = "Darwin"
155+
mock_platform.release.return_value = "21.6.0"
156+
mock_platform.machine.return_value = "arm64"
157+
mock_platform.processor.return_value = "arm"
202158

203-
mock_os_instance = MagicMock()
204-
mock_os_instance.is_windows_11.return_value = True
205-
mock_os_class.get.return_value = mock_os_instance
206-
207-
with patch.dict(
208-
os.environ,
209-
{"PROCESSOR_ARCHITECTURE": "AMD64", "PROCESSOR_ARCHITEW6432": "ARM64"},
210-
clear=True,
211-
):
212-
result = _get_platform_info()
159+
result = _get_platform_info()
213160

214-
assert result["machine"] == "ARM64"
161+
assert result["system"] == "Darwin"
162+
assert result["release"] == "21.6.0"
163+
assert result["machine"] == "arm64"
215164

216-
@patch("winml.modelkit.commands.sys.OS")
217165
@patch("winml.modelkit.commands.sys.platform")
218-
def test_native_amd64_python_reports_amd64(
219-
self, mock_platform: MagicMock, mock_os_class: MagicMock
220-
) -> None:
221-
"""Native AMD64 Python on AMD64 Windows must still report AMD64."""
166+
def test_processor_unknown_fallback(self, mock_platform: MagicMock) -> None:
167+
"""Test processor defaults to 'Unknown' when empty."""
222168
from winml.modelkit.commands.sys import _get_platform_info
223169

224-
mock_platform.system.return_value = "Windows"
225-
mock_platform.release.return_value = "11"
226-
mock_platform.machine.return_value = "AMD64"
227-
mock_platform.processor.return_value = "Intel64 Family 6"
228-
229-
mock_os_instance = MagicMock()
230-
mock_os_instance.is_windows_11.return_value = True
231-
mock_os_class.get.return_value = mock_os_instance
170+
# Setup mocks
171+
mock_platform.system.return_value = "Linux"
172+
mock_platform.release.return_value = "5.15.0"
173+
mock_platform.machine.return_value = "x86_64"
174+
mock_platform.processor.return_value = "" # Empty string
232175

233-
with patch.dict(os.environ, {"PROCESSOR_ARCHITECTURE": "AMD64"}, clear=True):
234-
result = _get_platform_info()
176+
result = _get_platform_info()
235177

236-
assert result["machine"] == "AMD64"
178+
assert result["processor"] == "Unknown"

0 commit comments

Comments
 (0)