Skip to content

Commit 7025854

Browse files
committed
cuda_core: lazily import frozen enum explanation tables
Keep cuda_utils.pyx unchanged while moving the large 13.1.1 explanation tables into frozen-only modules that are imported only for older bindings. Use loader-aware selection in enum_explanations_helpers.py and add tests that prove docstring-capable bindings skip the frozen-module imports. Made-with: Cursor
1 parent c4b5862 commit 7025854

6 files changed

Lines changed: 954 additions & 886 deletions

cuda_core/cuda/core/_utils/driver_cu_result_explanations.py

Lines changed: 7 additions & 348 deletions
Large diffs are not rendered by default.

cuda_core/cuda/core/_utils/driver_cu_result_explanations_frozen.py

Lines changed: 350 additions & 0 deletions
Large diffs are not rendered by default.

cuda_core/cuda/core/_utils/enum_explanations_helpers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
import importlib.metadata
2121
import re
22+
from collections.abc import Callable
2223
from typing import Any
2324

2425
_MIN_12X_BINDING_VERSION_FOR_ENUM_DOCSTRINGS = (12, 9, 6)
2526
_MIN_13X_BINDING_VERSION_FOR_ENUM_DOCSTRINGS = (13, 2, 0)
2627
_RST_INLINE_ROLE_RE = re.compile(r":(?:[a-z]+:)?[a-z]+:`([^`]+)`")
2728
_WORDWRAP_HYPHEN_AFTER_RE = re.compile(r"(?<=[0-9A-Za-z_])- (?=[0-9A-Za-z_])")
2829
_WORDWRAP_HYPHEN_BEFORE_RE = re.compile(r"(?<=[0-9A-Za-z_]) -(?=[0-9A-Za-z_])")
30+
_ExplanationTable = dict[int, str | tuple[str, ...]]
31+
_ExplanationTableLoader = Callable[[], _ExplanationTable]
2932

3033

3134
# ``version.pyx`` cannot be reused here (circular import via ``cuda_utils``).
@@ -111,14 +114,17 @@ def get(self, code: int, default: str | None = None) -> str | None:
111114

112115

113116
def get_best_available_explanations(
114-
enum_type: Any, fallback: dict[int, str | tuple[str, ...]]
115-
) -> DocstringBackedExplanations | dict[int, str | tuple[str, ...]]:
117+
enum_type: Any,
118+
fallback: _ExplanationTable | _ExplanationTableLoader,
119+
) -> DocstringBackedExplanations | _ExplanationTable:
116120
"""Pick one explanation source per bindings version.
117121
118122
Use enum-member ``__doc__`` only for bindings versions known to expose
119123
usable per-member text (12.9.6+ in the 12.x backport line, 13.2.0+ in the
120124
13.x mainline). Otherwise keep using the frozen 13.1.1 fallback tables.
121125
"""
122126
if not _binding_version_has_usable_enum_docstrings(_binding_version()):
127+
if callable(fallback):
128+
return fallback()
123129
return fallback
124130
return DocstringBackedExplanations(enum_type)

0 commit comments

Comments
 (0)