Skip to content

Commit f70c099

Browse files
committed
cuda_core: generalize enum doc cleanup regexes
Broaden the inline-role cleanup to accept generic RST roles and widen the word-wrap hyphen fix beyond lowercase-only cases. Keep the current 13.2.x output unchanged while expanding unit coverage for the newly supported forms. Made-with: Cursor
1 parent 849b1b9 commit f70c099

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎cuda_core/cuda/core/_utils/enum_explanations_helpers.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
_MIN_12X_BINDING_VERSION_FOR_ENUM_DOCSTRINGS = (12, 9, 6)
2525
_MIN_13X_BINDING_VERSION_FOR_ENUM_DOCSTRINGS = (13, 2, 0)
26+
_RST_INLINE_ROLE_RE = re.compile(r":(?:[a-z]+:)?[a-z]+:`([^`]+)`")
27+
_WORDWRAP_HYPHEN_AFTER_RE = re.compile(r"(?<=[0-9A-Za-z_])- (?=[0-9A-Za-z_])")
28+
_WORDWRAP_HYPHEN_BEFORE_RE = re.compile(r"(?<=[0-9A-Za-z_]) -(?=[0-9A-Za-z_])")
2629

2730

2831
# ``version.pyx`` cannot be reused here (circular import via ``cuda_utils``).
@@ -46,14 +49,14 @@ def _binding_version_has_usable_enum_docstrings(version: tuple[int, int, int]) -
4649
def _fix_hyphenation_wordwrap_spacing(s: str) -> str:
4750
"""Remove spaces around hyphens introduced by line wrapping in generated ``__doc__`` text.
4851
49-
This is a narrow workaround for wrapped forms such as ``non- linear`` that
50-
would otherwise look awkward in user-facing messages.
52+
This targets asymmetric wrap artifacts such as ``non- linear`` or
53+
``GPU- Direct`` while leaving intentional ``a - b`` separators alone.
5154
"""
5255
prev = None
5356
while prev != s:
5457
prev = s
55-
s = re.sub(r"([a-z])- ([a-z])", r"\1-\2", s)
56-
s = re.sub(r"([a-z]) -([a-z])", r"\1-\2", s)
58+
s = _WORDWRAP_HYPHEN_AFTER_RE.sub("-", s)
59+
s = _WORDWRAP_HYPHEN_BEFORE_RE.sub("-", s)
5760
return s
5861

5962

@@ -71,11 +74,7 @@ def clean_enum_member_docstring(doc: str | None) -> str | None:
7174
# Known codegen bug on cudaErrorIncompatibleDriverContext. Remove once fixed
7275
# in cuda-bindings code generation.
7376
s = s.replace("\n:py:obj:`~.Interactions`", ' "Interactions ')
74-
s = re.sub(
75-
r":(?:py:)?(?:obj|func|meth|class|mod|data|const|exc):`([^`]+)`",
76-
lambda m: re.sub(r"^~?\.", "", m.group(1)),
77-
s,
78-
)
77+
s = _RST_INLINE_ROLE_RE.sub(lambda m: re.sub(r"^~?\.", "", m.group(1)), s)
7978
s = re.sub(r"\*\*([^*]+)\*\*", r"\1", s)
8079
s = re.sub(r"\*([^*]+)\*", r"\1", s)
8180
s = re.sub(r"\s+", " ", s).strip()

‎cuda_core/tests/test_utils_enum_explanations_helpers.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,32 @@ def __call__(self, code):
3636
pytest.param(
3737
"see\n:py:obj:`~.cuInit()` or :py:obj:`cuCtxDestroy()`",
3838
"see cuInit() or cuCtxDestroy()",
39-
id="sphinx_py_obj_roles",
39+
id="rst_py_domain_role",
4040
),
4141
pytest.param(
4242
"x :py:func:`~.cudaMalloc()` y",
4343
"x cudaMalloc() y",
44-
id="sphinx_py_func_role",
44+
id="rst_py_role",
4545
),
46+
pytest.param(
47+
"x :c:func:`cuLaunchKernel` y",
48+
"x cuLaunchKernel y",
49+
id="rst_non_py_domain_role",
50+
),
51+
pytest.param("x :term:`device` y", "x device y", id="rst_role_without_domain"),
4652
pytest.param("**Note:** text", "Note: text", id="strip_bold"),
4753
pytest.param("*Note* text", "Note text", id="strip_italic"),
4854
pytest.param("[Deprecated]\n", "[Deprecated]", id="deprecated_line"),
4955
pytest.param("non- linear", "non-linear", id="hyphen_space_after"),
5056
pytest.param("word -word", "word-word", id="hyphen_space_before"),
57+
pytest.param("GPU- Direct", "GPU-Direct", id="hyphen_space_after_uppercase"),
58+
pytest.param("peer -GPU", "peer-GPU", id="hyphen_space_before_uppercase"),
59+
pytest.param("L2- cache", "L2-cache", id="hyphen_space_after_digit"),
60+
pytest.param(
61+
"Common causes are - a. bad access",
62+
"Common causes are - a. bad access",
63+
id="preserve_dash_separator",
64+
),
5165
pytest.param(
5266
'Please see\n:py:obj:`~.Interactions`with the CUDA Driver API" for more information.',
5367
'Please see "Interactions with the CUDA Driver API" for more information.',

0 commit comments

Comments
 (0)