Skip to content

Commit 95ccd0b

Browse files
committed
Cache version info at import time
1 parent fe7e148 commit 95ccd0b

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

src_py/__init__.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@
5858
if _repo_build_pkg_dir.is_dir():
5959
__path__.append(str(_repo_build_pkg_dir))
6060

61+
from ._backend import get_capi_module, get_pybind_module # noqa: E402
62+
63+
64+
def _get_version_source() -> type:
65+
backend = os.getenv("LBUG_PYTHON_BACKEND", "auto").strip().lower()
66+
if backend != "capi":
67+
pybind_module = get_pybind_module()
68+
if pybind_module is not None:
69+
return pybind_module.Database
70+
return get_capi_module().Database
71+
72+
73+
_version_source = _get_version_source()
74+
# Resolve version info before restoring dlopen flags so a pybind import, when
75+
# selected, happens under RTLD_GLOBAL and its symbols remain visible to
76+
# subsequently loaded extensions such as json.
77+
version = __version__ = _version_source.get_version()
78+
storage_version = _version_source.get_storage_version()
79+
6180
from .async_connection import AsyncConnection # noqa: E402
6281
from .connection import Connection # noqa: E402
6382
from .database import Database # noqa: E402
@@ -67,13 +86,8 @@
6786

6887

6988
def __getattr__(name: str) -> str | int:
70-
if name in ("version", "__version__"):
71-
return Database.get_version()
72-
elif name == "storage_version":
73-
return Database.get_storage_version()
74-
else:
75-
msg = f"module {__name__!r} has no attribute {name!r}"
76-
raise AttributeError(msg)
89+
msg = f"module {__name__!r} has no attribute {name!r}"
90+
raise AttributeError(msg)
7791

7892

7993
# Restore the original dlopen flags

src_py/database.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def get_version() -> str:
179179
str
180180
The version of the database.
181181
"""
182-
return get_capi_module().Database.get_version()
182+
from . import __version__
183+
184+
return __version__
183185

184186
@staticmethod
185187
def get_storage_version() -> int:
@@ -191,7 +193,9 @@ def get_storage_version() -> int:
191193
int
192194
The storage version of the database.
193195
"""
194-
return get_capi_module().Database.get_storage_version()
196+
from . import storage_version
197+
198+
return storage_version
195199

196200
def __getstate__(self) -> dict[str, Any]:
197201
state = {

0 commit comments

Comments
 (0)