99
1010# -- Path setup --------------------------------------------------------------
1111
12+ import inspect
1213import os
1314import re
1415import sys
@@ -141,41 +142,34 @@ def _html_baseurl():
141142}
142143
143144
144- _CUDA_BINDING_DOCSTRING_PREFIXES = ("cuda.bindings.driver." , "cuda.bindings.runtime." )
145- _CUDA_BINDING_DOCSTRING_NAMES = {
146- "cuda.bindings.nvml.device_get_virtualization_mode" ,
147- "cuda.bindings.nvml.device_set_virtualization_mode" ,
148- }
149- _CUDA_BINDING_ROLE_SUFFIX = re .compile (r"(:py:obj:`[^`]+`)(\[[^\]]*\]|\d+)" )
150- _CUDA_BINDING_ROLE_POINTER_SUFFIX = re .compile (r"(:py:obj:`[^`]+`) \*(?=[\s.,)])" )
151- _CUDA_BINDING_LITERAL_ARRAY_TOKEN = re .compile (r"`([A-Za-z_][A-Za-z0-9_]*)`\[([^\]]+)\]" )
152- _CUDA_BINDING_LITERAL_PLURAL_SUFFIX = re .compile (r"`([A-Za-z_][A-Za-z0-9_]*)`s\b" )
153- _CUDA_BINDING_POINTER_TOKEN = re .compile (r"(?<![\\*])\*(?=[A-Za-z_(])" )
145+ def _sanitize_generated_docstring (lines ):
146+ doc_lines = inspect .cleandoc ("\n " .join (lines )).splitlines ()
147+ if not doc_lines :
148+ return
149+
150+ if "(" in doc_lines [0 ] and ")" in doc_lines [0 ]:
151+ doc_lines = doc_lines [1 :]
152+ while doc_lines and not doc_lines [0 ].strip ():
153+ doc_lines .pop (0 )
154154
155+ if not doc_lines :
156+ lines [:] = []
157+ return
155158
156- def _separate_cuda_binding_role_suffix (match ):
157- role , suffix = match .groups ()
158- if suffix .startswith ("[" ):
159- return f"{ role } ``{ suffix } ``"
160- return f"{ role } { suffix } "
159+ lines [:] = [".. code-block:: text" , "" ]
160+ lines .extend (f" { line } " if line else " " for line in doc_lines )
161161
162162
163- def _fix_cuda_binding_docstring_line (line ):
164- line = _CUDA_BINDING_ROLE_SUFFIX .sub (_separate_cuda_binding_role_suffix , line )
165- line = _CUDA_BINDING_ROLE_POINTER_SUFFIX .sub (r"\1 \\*" , line )
166- line = _CUDA_BINDING_LITERAL_ARRAY_TOKEN .sub (r"``\1[\2]``" , line )
167- line = _CUDA_BINDING_LITERAL_PLURAL_SUFFIX .sub (r"``\1``" , line )
168- line = line .replace ("NVML_GPU_VIRTUALIZATION_?" , "``NVML_GPU_VIRTUALIZATION_?``" )
169- line = line .replace ("void **" , "void \\ *\\ *" )
170- line = line .replace ("void *" , "void \\ *" )
171- line = line .replace ("CUcontext *" , "CUcontext \\ *" )
172- line = line .replace ("CUdeviceptr *" , "CUdeviceptr \\ *" )
173- return _CUDA_BINDING_POINTER_TOKEN .sub (r"\\*" , line )
163+ def _escape_generated_docstring_markup (lines ):
164+ pointer_marker = re .compile (r"(?<!\\)\*(?=[A-Za-z_(])" )
165+ lines [:] = [pointer_marker .sub (r"\\*" , line ) for line in lines ]
174166
175167
176- def _process_cuda_binding_docstring (app , what , name , obj , options , lines ):
177- if name in _CUDA_BINDING_DOCSTRING_NAMES or name .startswith (_CUDA_BINDING_DOCSTRING_PREFIXES ):
178- lines [:] = [_fix_cuda_binding_docstring_line (line ) for line in lines ]
168+ def autodoc_process_docstring (app , what , name , obj , options , lines ):
169+ if name .startswith ("cuda.bindings." ):
170+ _sanitize_generated_docstring (lines )
171+ if lines and lines [0 ] != ".. code-block:: text" :
172+ _escape_generated_docstring_markup (lines )
179173
180174
181175def rewrite_source (app , docname , source ):
@@ -194,5 +188,5 @@ def rewrite_source(app, docname, source):
194188
195189
196190def setup (app ):
197- app .connect ("autodoc-process-docstring" , _process_cuda_binding_docstring )
191+ app .connect ("autodoc-process-docstring" , autodoc_process_docstring )
198192 app .connect ("source-read" , rewrite_source )
0 commit comments