From 77d20c6a1c5583827d8b4e50ac827afd5cf1e377 Mon Sep 17 00:00:00 2001 From: Ash Vardanian <1983160+ashvardanian@users.noreply.github.com> Date: Thu, 10 Oct 2024 03:14:19 +0000 Subject: [PATCH] Fix: Avoid `RTLD_GLOBAL` on Windows --- python/usearch/__init__.py | 23 ++++++++++++++++++----- python/usearch/index.py | 11 ----------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/python/usearch/__init__.py b/python/usearch/__init__.py index 00413ae3..e5f559e8 100644 --- a/python/usearch/__init__.py +++ b/python/usearch/__init__.py @@ -1,4 +1,6 @@ import os +import sys +import ctypes import platform import warnings import urllib.request @@ -6,15 +8,26 @@ from urllib.error import HTTPError #! Load SimSIMD before the USearch compiled module +#! We can't just use the `import simsimd` as on Linux and Windows (unlike MacOS), +#! the symbols are not automatically loaded into the global namespace. try: - import ctypes import simsimd - #! We can't just use the `import simsimd` as on Linux and Windows (unlike MacOS), - #! the symbols are not automatically loaded into the global namespace. - simsimd_lib = ctypes.CDLL(simsimd.__file__, mode=ctypes.RTLD_GLOBAL) + # Cross-platform check for Windows + if sys.platform == "win32": + # Add the directory where the `.dll` is located + dll_directory = os.path.dirname(simsimd.__file__) + os.add_dll_directory(dll_directory) + + # Load SimSIMD library using `ctypes` without `RTLD_GLOBAL` + simsimd_lib = ctypes.CDLL(simsimd.__file__) + + else: + # Non-Windows: Use `RTLD_GLOBAL` for Unix-based systems (Linux/macOS) + simsimd_lib = ctypes.CDLL(simsimd.__file__, mode=ctypes.RTLD_GLOBAL) + except ImportError: - pass # Well, the user doesn't want SimSIMD, I assume :) + pass # If the user doesn't want SimSIMD, we assume they know what they're doing from usearch.compiled import ( diff --git a/python/usearch/index.py b/python/usearch/index.py index 886c571f..fe344431 100644 --- a/python/usearch/index.py +++ b/python/usearch/index.py @@ -25,17 +25,6 @@ import numpy as np from tqdm import tqdm -#! Load SimSIMD before the USearch compiled module -try: - import ctypes - import simsimd - - #! We can't just use the `import simsimd` as on Linux and Windows (unlike MacOS), - #! the symbols are not automatically loaded into the global namespace. - simsimd_lib = ctypes.CDLL(simsimd.__file__, mode=ctypes.RTLD_GLOBAL) -except ImportError: - pass # Well, the user doesn't want SimSIMD, I assume :) - # Precompiled symbols that won't be exposed directly: from usearch.compiled import ( Index as _CompiledIndex,