From 84d3a5d950e6b924d5fb2d069edc70e616e89586 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Wed, 4 Oct 2023 13:26:56 +0100 Subject: [PATCH] Fix docs for classes with constructors (#174) Avoid the null terminator that function signature includes which any C program will interpret as end of string. --------- Co-authored-by: Nicholas Gates --- example/buffers.pyi | 4 ++++ example/iterators.pyi | 4 ++++ pydust/src/functions.zig | 4 +++- pydust/src/pytypes.zig | 4 ++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/example/buffers.pyi b/example/buffers.pyi index 6947537e..11484d46 100644 --- a/example/buffers.pyi +++ b/example/buffers.pyi @@ -3,5 +3,9 @@ from __future__ import annotations def sum(buf, /): ... class ConstantBuffer: + """ + A class implementing a buffer protocol + """ + def __init__(elem, length, /): pass diff --git a/example/iterators.pyi b/example/iterators.pyi index a545a582..7289c3e0 100644 --- a/example/iterators.pyi +++ b/example/iterators.pyi @@ -1,6 +1,10 @@ from __future__ import annotations class Range: + """ + An example of iterable class + """ + def __init__(lower, upper, step, /): pass def __iter__(self, /): diff --git a/pydust/src/functions.zig b/pydust/src/functions.zig index 8e570ee9..73887773 100644 --- a/pydust/src/functions.zig +++ b/pydust/src/functions.zig @@ -432,6 +432,7 @@ fn sigSize(comptime sig: Signature) usize { var argSize: u64 = sig.name.len; // Count the size of the output string for (args) |arg| { + // +2 for ", " argSize += arg.len + 2; } @@ -440,7 +441,8 @@ fn sigSize(comptime sig: Signature) usize { } // The size is the size of all arguments plus the padding after argument list - return argSize + 8; + // "(" + ")\n--\n\n" => 7 + return argSize + 7; } fn sigArgs(comptime sig: Signature) ![]const []const u8 { diff --git a/pydust/src/pytypes.zig b/pydust/src/pytypes.zig index 9aa01e97..38d83aeb 100644 --- a/pydust/src/pytypes.zig +++ b/pydust/src/pytypes.zig @@ -425,6 +425,10 @@ fn Doc(comptime definition: type, comptime name: [:0]const u8) type { if (@hasDecl(definition, "__doc__")) { @memcpy(userDoc[docOffset..], definition.__doc__); } + + // Add null terminator + userDoc[userDoc.len] = 0; + break :blk userDoc; }; };