Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rtree/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def load() -> ctypes.CDLL:
except importlib.metadata.PackageNotFoundError:
pass

# Fallback: search relative to package directory
# (works when importlib.metadata is unavailable, e.g. Bazel, some venvs)
libs_dir = _cwd.parent / "rtree.libs"
if libs_dir.exists():
for so_file in libs_dir.glob("libspatialindex*.so"):
_candidates.insert(1, so_file)
break

for cand in _candidates:
if cand.is_dir():
# if our candidate is a directory use best guess
Expand Down
17 changes: 17 additions & 0 deletions tests/test_finder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ctypes import CDLL
from pathlib import Path
from unittest import mock

from rtree import finder

Expand All @@ -9,6 +10,22 @@ def test_load():
assert isinstance(lib, CDLL)


def test_load_without_importlib_metadata():
"""Test that library loading works when importlib.metadata.files() is unavailable.

This can happen in sandboxed environments like Bazel where package metadata
isn't properly exposed.
"""
# Reload finder module with mocked importlib.metadata.files returning None
with mock.patch("importlib.metadata.files", return_value=None):
# Need to reload the module to re-run the load logic
import importlib

importlib.reload(finder)
lib = finder.load()
assert isinstance(lib, CDLL)


def test_get_include():
incl = finder.get_include()
assert isinstance(incl, str)
Expand Down
Loading