Skip to content

Commit

Permalink
Fix docs for classes with constructors (#174)
Browse files Browse the repository at this point in the history
Avoid the null terminator that function signature includes which any C
program will interpret as end of string.

---------

Co-authored-by: Nicholas Gates <[email protected]>
  • Loading branch information
robert3005 and gatesn authored Oct 4, 2023
1 parent cea2c23 commit 84d3a5d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions example/buffers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ from __future__ import annotations
def sum(buf, /): ...

class ConstantBuffer:
"""
A class implementing a buffer protocol
"""

def __init__(elem, length, /):
pass
4 changes: 4 additions & 0 deletions example/iterators.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations

class Range:
"""
An example of iterable class
"""

def __init__(lower, upper, step, /):
pass
def __iter__(self, /):
Expand Down
4 changes: 3 additions & 1 deletion pydust/src/functions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pydust/src/pytypes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
Expand Down

0 comments on commit 84d3a5d

Please sign in to comment.