@@ -74,7 +74,8 @@ func (f Func) Call(args ...any) Object {
7474 for i , arg := range args {
7575 obj := From (arg ).Obj ()
7676 C .Py_IncRef (obj )
77- C .PyTuple_SetItem (argsTuple , C .Py_ssize_t (i ), obj )
77+ r := C .PyTuple_SetItem (argsTuple , C .Py_ssize_t (i ), obj )
78+ check (r == 0 , fmt .Sprintf ("failed to set item %d in tuple" , i ))
7879 }
7980 return newObject (C .PyObject_CallObject (f .obj , argsTuple ))
8081 }
@@ -471,9 +472,7 @@ func getMembers(t reflect.Type, methods map[uint]*slotMeta) (members *C.PyMember
471472func AddType [T any ](m Module , init any , name string , doc string ) Object {
472473 wrapper := wrapperType [T ]{}
473474 ty := reflect .TypeOf (wrapper .v )
474- if ty .Kind () != reflect .Struct {
475- panic ("AddType: t must be a struct" )
476- }
475+ check (ty .Kind () == reflect .Struct , "AddType: t must be a struct" )
477476
478477 meta := & typeMeta {
479478 typ : ty ,
@@ -517,27 +516,21 @@ func AddType[T any](m Module, init any, name string, doc string) Object {
517516 }
518517
519518 typeObj := C .PyType_FromSpec (spec )
520- if typeObj == nil {
521- panic (fmt .Sprintf ("Failed to create type %s" , name ))
522- }
519+ check (typeObj != nil , fmt .Sprintf ("Failed to create type %s" , name ))
523520
524521 typeMetaMap [typeObj ] = meta
525522 pyTypeMap [ty ] = typeObj
526523
527- if C .PyModule_AddObject (m .obj , C .CString (name ), typeObj ) < 0 {
528- C .Py_DecRef (typeObj )
529- panic (fmt .Sprintf ("Failed to add type %s to module" , name ))
530- }
524+ r := C .PyModule_AddObjectRef (m .obj , C .CString (name ), typeObj )
525+ check (r == 0 , fmt .Sprintf ("Failed to add type %s to module" , name ))
531526
532527 return newObject (typeObj )
533528}
534529
535530func (m Module ) AddMethod (name string , fn any , doc string ) Func {
536531 v := reflect .ValueOf (fn )
537532 t := v .Type ()
538- if t .Kind () != reflect .Func {
539- panic ("AddFunction: fn must be a function" )
540- }
533+ check (t .Kind () == reflect .Func , "AddFunction: fn must be a function" )
541534
542535 if name == "" {
543536 name = runtime .FuncForPC (v .Pointer ()).Name ()
@@ -583,14 +576,10 @@ func (m Module) AddMethod(name string, fn any, doc string) Func {
583576 }
584577
585578 pyFunc := C .PyCFunction_New (def , m .obj )
586- if pyFunc == nil {
587- panic (fmt .Sprintf ("Failed to create function %s" , name ))
588- }
579+ check (pyFunc != nil , fmt .Sprintf ("Failed to create function %s" , name ))
589580
590- if C .PyModule_AddObject (m .obj , cName , pyFunc ) < 0 {
591- C .Py_DecRef (pyFunc )
592- panic (fmt .Sprintf ("Failed to add function %s to module" , name ))
593- }
581+ r := C .PyModule_AddObjectRef (m .obj , cName , pyFunc )
582+ check (r == 0 , fmt .Sprintf ("Failed to add function %s to module" , name ))
594583
595584 return newFunc (pyFunc )
596585}
0 commit comments