@@ -215,6 +215,13 @@ fn Slots(comptime definition: type, comptime name: [:0]const u8) type {
215
215
}};
216
216
}
217
217
218
+ if (@hasDecl (definition , "__bool__" )) {
219
+ slots_ = slots_ ++ .{ffi.PyType_Slot {
220
+ .slot = ffi .Py_nb_bool ,
221
+ .pfunc = @ptrCast (@constCast (& nb_bool )),
222
+ }};
223
+ }
224
+
218
225
if (richcmp .hasCompare ) {
219
226
slots_ = slots_ ++ .{ffi.PyType_Slot {
220
227
.slot = ffi .Py_tp_richcompare ,
@@ -232,6 +239,16 @@ fn Slots(comptime definition: type, comptime name: [:0]const u8) type {
232
239
}
233
240
}
234
241
242
+ for (funcs .UnaryOperators .kvs ) | kv | {
243
+ if (@hasDecl (definition , kv .key )) {
244
+ const op = UnaryOperator (definition , kv .key );
245
+ slots_ = slots_ ++ .{ffi.PyType_Slot {
246
+ .slot = kv .value ,
247
+ .pfunc = @ptrCast (@constCast (& op .call )),
248
+ }};
249
+ }
250
+ }
251
+
235
252
slots_ = slots_ ++ .{ffi.PyType_Slot {
236
253
.slot = ffi .Py_tp_methods ,
237
254
.pfunc = @ptrCast (@constCast (& methods .pydefs )),
@@ -369,6 +386,12 @@ fn Slots(comptime definition: type, comptime name: [:0]const u8) type {
369
386
const result = tramp .coerceError (definition .__call__ (self , call_args .argsStruct )) catch return null ;
370
387
return (py .createOwned (result ) catch return null ).py ;
371
388
}
389
+
390
+ fn nb_bool (pyself : * ffi.PyObject ) callconv (.C ) c_int {
391
+ const self : * PyTypeStruct (definition ) = @ptrCast (pyself );
392
+ const result = tramp .coerceError (definition .__bool__ (& self .state )) catch return -1 ;
393
+ return @intCast (@intFromBool (result ));
394
+ }
372
395
};
373
396
}
374
397
@@ -593,6 +616,26 @@ fn BinaryOperator(
593
616
};
594
617
}
595
618
619
+ fn UnaryOperator (
620
+ comptime definition : type ,
621
+ comptime op : []const u8 ,
622
+ ) type {
623
+ return struct {
624
+ fn call (pyself : * ffi.PyObject ) callconv (.C ) ? * ffi.PyObject {
625
+ const func = @field (definition , op );
626
+ const typeInfo = @typeInfo (@TypeOf (func )).Fn ;
627
+
628
+ if (typeInfo .params .len != 1 ) @compileError (op ++ " must take exactly one parameter" );
629
+
630
+ // TODO(ngates): do we want to trampoline the self argument?
631
+ const self : * PyTypeStruct (definition ) = @ptrCast (pyself );
632
+
633
+ const result = tramp .coerceError (func (& self .state )) catch return null ;
634
+ return (py .createOwned (result ) catch return null ).py ;
635
+ }
636
+ };
637
+ }
638
+
596
639
fn EqualsOperator (
597
640
comptime definition : type ,
598
641
comptime op : []const u8 ,
0 commit comments