Skip to content

Commit ee28300

Browse files
committed
Add Object.getItemData and Metadata.getMemberDefs
1 parent dc0923a commit ee28300

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

py.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,10 @@ pub inline fn ObjectProtocol(comptime T: type) type {
752752
return @ptrCast(c._PyObject_GetDictPtr(@ptrCast(self)));
753753
}
754754

755+
pub inline fn getItemData(self: *T) ?*anyopaque {
756+
return @ptrCast(c.PyObject_GetItemData(@ptrCast(self)));
757+
}
758+
755759
// Compare the values of o1 and o2 using the operation specified by opid, like PyObject_RichCompare(),
756760
// but returns -1 on error, 0 if the result is false, 1 otherwise.
757761
pub inline fn compare(self: *const T, comptime op: CompareOp, other: *const Object) !bool {
@@ -1187,6 +1191,15 @@ pub const Metaclass = extern struct {
11871191
impl: c.PyHeapTypeObject,
11881192
// Import the object protocol
11891193
pub usingnamespace ObjectProtocol(@This());
1194+
1195+
pub inline fn getMemberDefs(self: *Metaclass) ![]c.PyMemberDef {
1196+
if (self.getItemData()) |ptr| {
1197+
const n: usize = @intCast(c.Py_SIZE(@ptrCast(self)));
1198+
const members: [*]c.PyMemberDef = @alignCast(@ptrCast(ptr));
1199+
return @ptrCast(members[0..n]);
1200+
}
1201+
return error.PyError;
1202+
}
11901203
};
11911204

11921205
pub const Bool = extern struct {

0 commit comments

Comments
 (0)