Skip to content

Commit 7c230ec

Browse files
committed
check Python c-api error code
1 parent 2c6e240 commit 7c230ec

File tree

7 files changed

+35
-39
lines changed

7 files changed

+35
-39
lines changed

dict.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ func newDict(obj *PyObject) Dict {
1818
}
1919

2020
func DictFromPairs(pairs ...any) Dict {
21-
if len(pairs)%2 != 0 {
22-
panic("DictFromPairs requires an even number of arguments")
23-
}
21+
check(len(pairs)%2 == 0, "DictFromPairs requires an even number of arguments")
2422
dict := newDict(C.PyDict_New())
2523
for i := 0; i < len(pairs); i += 2 {
2624
key := From(pairs[i])
@@ -62,9 +60,7 @@ func (d Dict) SetString(key string, value Objecter) {
6260
ckey := AllocCStr(key)
6361
r := C.PyDict_SetItemString(d.obj, ckey, valueObj)
6462
C.free(unsafe.Pointer(ckey))
65-
if r != 0 {
66-
panic(fmt.Errorf("failed to set item string: %v", r))
67-
}
63+
check(r == 0, fmt.Sprintf("failed to set item string: %v", r))
6864
}
6965

7066
func (d Dict) GetString(key string) Object {
@@ -81,9 +77,7 @@ func (d Dict) Del(key Objecter) {
8177

8278
func (d Dict) ForEach(fn func(key, value Object)) {
8379
items := C.PyDict_Items(d.obj)
84-
if items == nil {
85-
panic(fmt.Errorf("failed to get items of dict"))
86-
}
80+
check(items != nil, "failed to get items of dict")
8781
defer C.Py_DecRef(items)
8882
iter := C.PyObject_GetIter(items)
8983
for {

function.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
471472
func 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

535530
func (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
}

list.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package gp
44
#include <Python.h>
55
*/
66
import "C"
7+
import "fmt"
78

89
type List struct {
910
Object
@@ -31,13 +32,15 @@ func (l List) GetItem(index int) Object {
3132
func (l List) SetItem(index int, item Objecter) {
3233
itemObj := item.Obj()
3334
C.Py_IncRef(itemObj)
34-
C.PyList_SetItem(l.obj, C.Py_ssize_t(index), itemObj)
35+
r := C.PyList_SetItem(l.obj, C.Py_ssize_t(index), itemObj)
36+
check(r == 0, fmt.Sprintf("failed to set item %d in list", index))
3537
}
3638

3739
func (l List) Len() int {
3840
return int(C.PyList_Size(l.obj))
3941
}
4042

4143
func (l List) Append(obj Objecter) {
42-
C.PyList_Append(l.obj, obj.Obj())
44+
r := C.PyList_Append(l.obj, obj.Obj())
45+
check(r == 0, "failed to append item to list")
4346
}

object.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,9 @@ func (obj Object) AttrFunc(name string) Func {
117117

118118
func (obj Object) SetAttr(name string, value any) {
119119
cname := AllocCStr(name)
120-
if C.PyObject_SetAttrString(obj.obj, cname, From(value).obj) != 0 {
121-
C.PyErr_Print()
122-
panic(fmt.Errorf("failed to set attribute %s", name))
123-
}
120+
r := C.PyObject_SetAttrString(obj.obj, cname, From(value).obj)
121+
C.PyErr_Print()
122+
check(r == 0, fmt.Sprintf("failed to set attribute %s", name))
124123
C.free(unsafe.Pointer(cname))
125124
}
126125

python.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func Initialize() {
2121
}
2222

2323
func Finalize() {
24-
C.Py_FinalizeEx()
24+
r := C.Py_FinalizeEx()
25+
check(r == 0, "failed to finalize Python")
2526
typeMetaMap = make(map[*C.PyObject]*typeMeta)
2627
pyTypeMap = make(map[reflect.Type]*C.PyObject)
2728
}
@@ -112,3 +113,11 @@ func RunString(code string) error {
112113
}
113114
return nil
114115
}
116+
117+
// ----------------------------------------------------------------------------
118+
119+
func check(b bool, msg string) {
120+
if !b {
121+
panic(msg)
122+
}
123+
}

tuple.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package gp
44
#include <Python.h>
55
*/
66
import "C"
7+
import "fmt"
78

89
type Tuple struct {
910
Object
@@ -35,7 +36,8 @@ func (t Tuple) Get(index int) Object {
3536
func (t Tuple) Set(index int, obj Objecter) {
3637
objObj := obj.Obj()
3738
C.Py_IncRef(objObj)
38-
C.PyTuple_SetItem(t.obj, C.Py_ssize_t(index), objObj)
39+
r := C.PyTuple_SetItem(t.obj, C.Py_ssize_t(index), objObj)
40+
check(r == 0, fmt.Sprintf("failed to set item %d in tuple", index))
3941
}
4042

4143
func (t Tuple) Len() int {

unicode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (s Str) Len() int {
3232

3333
func (s Str) ByteLen() int {
3434
var l C.long
35-
C.PyUnicode_AsUTF8AndSize(s.obj, &l)
35+
_ = C.PyUnicode_AsUTF8AndSize(s.obj, &l)
3636
return int(l)
3737
}
3838

0 commit comments

Comments
 (0)