Skip to content

Commit 2384e27

Browse files
committed
fix: prevent crash when platform.architecture() is not allowed (#1120)
1 parent 66bab65 commit 2384e27

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/openai/_base_client.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -1836,8 +1836,12 @@ def __str__(self) -> str:
18361836

18371837

18381838
def get_platform() -> Platform:
1839-
system = platform.system().lower()
1840-
platform_name = platform.platform().lower()
1839+
try:
1840+
system = platform.system().lower()
1841+
platform_name = platform.platform().lower()
1842+
except Exception:
1843+
return "Unknown"
1844+
18411845
if "iphone" in platform_name or "ipad" in platform_name:
18421846
# Tested using Python3IDE on an iPhone 11 and Pythonista on an iPad 7
18431847
# system is Darwin and platform_name is a string like:
@@ -1880,8 +1884,8 @@ def platform_headers(version: str) -> Dict[str, str]:
18801884
"X-Stainless-Package-Version": version,
18811885
"X-Stainless-OS": str(get_platform()),
18821886
"X-Stainless-Arch": str(get_architecture()),
1883-
"X-Stainless-Runtime": platform.python_implementation(),
1884-
"X-Stainless-Runtime-Version": platform.python_version(),
1887+
"X-Stainless-Runtime": get_python_runtime(),
1888+
"X-Stainless-Runtime-Version": get_python_version(),
18851889
}
18861890

18871891

@@ -1897,9 +1901,27 @@ def __str__(self) -> str:
18971901
Arch = Union[OtherArch, Literal["x32", "x64", "arm", "arm64", "unknown"]]
18981902

18991903

1904+
def get_python_runtime() -> str:
1905+
try:
1906+
return platform.python_implementation()
1907+
except Exception:
1908+
return "unknown"
1909+
1910+
1911+
def get_python_version() -> str:
1912+
try:
1913+
return platform.python_version()
1914+
except Exception:
1915+
return "unknown"
1916+
1917+
19001918
def get_architecture() -> Arch:
1901-
python_bitness, _ = platform.architecture()
1902-
machine = platform.machine().lower()
1919+
try:
1920+
python_bitness, _ = platform.architecture()
1921+
machine = platform.machine().lower()
1922+
except Exception:
1923+
return "unknown"
1924+
19031925
if machine in ("arm64", "aarch64"):
19041926
return "arm64"
19051927

0 commit comments

Comments
 (0)