Skip to content

Commit f60b104

Browse files
committed
Updated the docstring generation system.
1 parent 81b6f45 commit f60b104

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

sdl3/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sys, os, ctypes
1+
import sys, os, ctypes, _ctypes
22

33
if "PYSDL3_DISABLE_DOCS" not in os.environ:
44
os.environ["PYSDL3_DISABLE_DOCS"] = "0"
@@ -30,20 +30,29 @@ def SDL_FUNC(name, retType, *args):
3030
with open(__docs_file__, "w") as file:
3131
result = "from .SDL import *\n\n"
3232
result += "from .__init__ import ctypes, dll\n\n"
33+
definitions, types = "", set()
3334

3435
for index, name in enumerate(functions.keys()):
3536
retType, args = functions[name].restype, functions[name].argtypes
36-
getType = lambda i: "None" if i is None else ("ctypes." if "SDL_" not in i.__name__ else "") \
37-
+ i.__name__.replace("CFunctionType", "_CFuncPtr").replace("LP_", "")
3837

39-
result += f"def {name}({", ".join([f"_{index}: {getType(i)}" for index, i in enumerate(args)])}) -> {getType(retType)}:\n"
40-
result += f" \"\"\"This function is auto-generated.\"\"\"\n"
41-
result += f" return dll.{name}({", ".join([f"_{index}" for index, i in enumerate(args)])})"
38+
def getName(i):
39+
if i is None: return "None"
40+
name = i.__name__.replace("CFunctionType", "_CFuncPtr")
41+
if "SDL_" in name or "LP_" in name: types.add(name); return name
42+
else: return f"ctypes.{name}"
43+
44+
definitions += f"def {name}({", ".join([f"_{index}: {getName(i)}" for index, i in enumerate(args)])}) -> {getName(retType)}:\n"
45+
definitions += f" \"\"\"This function is auto-generated.\"\"\"\n"
46+
definitions += f" return dll.{name}({", ".join([f"_{index}" for index, i in enumerate(args)])})"
4247

4348
if index != len(functions.keys()) - 1:
44-
result += "\n\n"
45-
46-
file.write(result)
49+
definitions += "\n\n"
50+
51+
for i in types:
52+
result += f"class {i}(ctypes._Pointer):\n"
53+
result += " \"\"\"This class is auto-generated.\"\"\"\n\n"
54+
55+
file.write(result + definitions)
4756

4857
from .__docs__ import *
4958

0 commit comments

Comments
 (0)