From 8578af24af431f99fbef2fcc0522b4a270a8ea01 Mon Sep 17 00:00:00 2001 From: Karim Jahed Date: Wed, 29 Jun 2022 11:28:29 -0400 Subject: [PATCH] support error slices --- bind/gen.go | 8 ++++++++ bind/stdtypes.go | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/bind/gen.go b/bind/gen.go index 56a9af0..c36dc9b 100644 --- a/bind/gen.go +++ b/bind/gen.go @@ -164,6 +164,14 @@ func complex128PyToGo(o *C.PyObject) complex128 { return complex(float64(v.real), float64(v.imag)) } +// errorGoToPy converts a Go error to python-compatible C.CString +func errorGoToPy(e error) *C.char { + if e != nil { + return C.CString(e.Error()) + } + return C.CString("") +} + %[9]s ` diff --git a/bind/stdtypes.go b/bind/stdtypes.go index 89e3d2b..12e8fe2 100644 --- a/bind/stdtypes.go +++ b/bind/stdtypes.go @@ -24,7 +24,7 @@ func makeGoPackage() { func addStdSliceMaps() { makeGoPackage() gopk := goPackage.pkg - sltyps := []string{"int", "int64", "int32", "int16", "int8", "uint", "uint64", "uint32", "uint16", "uint8", "bool", "byte", "rune", "float64", "float32", "string"} + sltyps := []string{"int", "int64", "int32", "int16", "int8", "uint", "uint64", "uint32", "uint16", "uint8", "bool", "byte", "rune", "float64", "float32", "string", "error"} for _, tn := range sltyps { universe.addSliceType(gopk, nil, types.NewSlice(universe.sym(tn).gotyp), skType, "Slice_"+tn, "[]"+tn) } @@ -342,16 +342,16 @@ func stdBasicTypes() map[string]*symbol { gopkg: look("error").Pkg(), goobj: look("error"), gotyp: look("error").Type(), - kind: skType | skInterface, + kind: skType | skBasic, goname: "error", id: "error", cpyname: "char*", cgoname: "*C.char", pysig: "str", - go2py: "C.CString", + go2py: "errorGoToPy", py2go: "errors.New(C.GoString", py2goParenEx: ")", - zval: `""`, + zval: `nil`, pyfmt: "O&", }, }