Skip to content

Commit 903cc26

Browse files
committed
fix: symlink native library for importlib-based loading
The generated Python bindings look for the .so next to isomdl_uniffi.py via os.path.dirname(__file__), but the wheel places it in a subdirectory. Create a symlink (or copy) so the bindings can find the native library. Signed-off-by: Adam Burdett <burdettadam@gmail.com>
1 parent f8087db commit 903cc26

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

python/isomdl_uniffi/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,31 @@
2525
# - isomdl_uniffi/isomdl_uniffi.py (the Python bindings)
2626
# - isomdl_uniffi/isomdl_uniffi/ (directory containing the .so)
2727
# Python prefers packages over modules, so we must explicitly load the .py file.
28+
# Additionally, the generated bindings look for the .so next to the .py file,
29+
# but it's in the subdirectory. We symlink it before loading.
2830

2931
_module_dir = os.path.dirname(os.path.abspath(__file__))
3032
_bindings_path = os.path.join(_module_dir, "isomdl_uniffi.py")
3133

34+
# Ensure the native library is findable next to isomdl_uniffi.py
35+
# The .so is in isomdl_uniffi/isomdl_uniffi/ but the .py expects it in isomdl_uniffi/
36+
_lib_extensions = {
37+
"darwin": "libisomdl_uniffi.dylib",
38+
"win32": "isomdl_uniffi.dll",
39+
}
40+
_lib_name = _lib_extensions.get(sys.platform, "libisomdl_uniffi.so")
41+
_lib_nested = os.path.join(_module_dir, "isomdl_uniffi", _lib_name)
42+
_lib_target = os.path.join(_module_dir, _lib_name)
43+
44+
if os.path.exists(_lib_nested) and not os.path.exists(_lib_target):
45+
try:
46+
os.symlink(_lib_nested, _lib_target)
47+
except OSError:
48+
# Fallback: copy the file if symlinks are not supported
49+
import shutil
50+
51+
shutil.copy2(_lib_nested, _lib_target)
52+
3253
if os.path.exists(_bindings_path):
3354
try:
3455
spec = importlib.util.spec_from_file_location(

0 commit comments

Comments
 (0)