Skip to content

Commit 99435fa

Browse files
committed
Fix architecture detection to use Python interpreter bitness
Replace platform.machine() with struct.calcsize('P') for x86/x64 detection. platform.machine() returns OS architecture (e.g., 'AMD64' for 64-bit Windows), which incorrectly causes 32-bit Python on 64-bit Windows to load 64-bit DLLs. Changes: - Use struct.calcsize('P') * 8 to detect Python interpreter bitness (32 vs 64-bit) - Keep platform.machine() only for ARM64 architecture detection - Fix regression introduced in 1.4.0 that broke 32-bit Python support - Add debug logging for Python interpreter bitness Fixes architecture mismatch errors when using 32-bit Python on 64-bit Windows.
1 parent b4f7a00 commit 99435fa

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

libloader/libloader.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import platform
5+
import struct
56

67
# Set up logger
78
logger = logging.getLogger(__name__)
@@ -72,11 +73,15 @@ def find_library_path(libname, x86_path=".", x64_path=".", arm64_path=None):
7273
libname_with_prefix = f"{prefix}{libname}"
7374
logger.debug("Library name with prefix: %s", libname_with_prefix)
7475

75-
# Get actual machine architecture
76+
# Detect Python interpreter bitness (not OS architecture)
77+
pointer_bits = struct.calcsize("P") * 8
78+
logger.debug("Detected Python interpreter bitness: %d-bit", pointer_bits)
79+
80+
# Get machine architecture for ARM64 detection
7681
machine = platform.machine().lower()
7782
logger.debug("Detected machine architecture: %s", machine)
7883

79-
# Map machine architecture to the appropriate path
84+
# Map architecture to the appropriate path
8085
if machine in ("arm64", "aarch64"):
8186
# ARM64 architecture (M1+ Macs, ARM servers, modern Raspberry Pi)
8287
if arm64_path is None:
@@ -86,14 +91,14 @@ def find_library_path(libname, x86_path=".", x64_path=".", arm64_path=None):
8691
else:
8792
path = os.path.join(arm64_path, libname_with_prefix)
8893
logger.debug("Using ARM64 path: %s", arm64_path)
89-
elif machine in ("x86_64", "amd64", "x64"):
90-
# 64-bit x86 architecture (Intel/AMD)
94+
elif pointer_bits == 64:
95+
# 64-bit Python interpreter (x86_64)
9196
path = os.path.join(x64_path, libname_with_prefix)
92-
logger.debug("Using x86_64 path: %s", x64_path)
97+
logger.debug("Using x64 path (64-bit Python): %s", x64_path)
9398
else:
94-
# 32-bit x86 or other architectures
99+
# 32-bit Python interpreter (x86)
95100
path = os.path.join(x86_path, libname_with_prefix)
96-
logger.debug("Using x86 path for architecture '%s': %s", machine, x86_path)
101+
logger.debug("Using x86 path (%d-bit Python): %s", pointer_bits, x86_path)
97102

98103
ext = get_library_extension()
99104
logger.debug("Using library extension: %s", ext)

0 commit comments

Comments
 (0)