func stringify(obj Object) string { tpyS := C.PyObject_Str(c(obj)) defer C.decref(tpyS) u := C.PyUnicode_AsUTF8String(tpyS) defer C.decref(u) return C.GoString(C.PyBytes_AsString(u)) }
func (code *Code) Run(globals, locals *Base) error { pyCode := (*C.PyCodeObject)(unsafe.Pointer(code)) ret := C.PyEval_EvalCode(pyCode, globals.c(), locals.c()) if ret == nil { return exception() } C.decref(ret) return nil }
func (obj *Base) String() string { if v, ok := AsString(obj); ok { return v.String() } ret := C.PyObject_Str(obj.c()) if ret == nil { return "<nil>" } defer C.decref(ret) return ((*String)(unsafe.Pointer(ret))).String() }
// Error() returns a string representation of the Python exception represented // by the Error e. This is the same as the final line of the Python output from // an uncaught exception. func (e *Error) Error() string { ts := "" en := C.excName(c(e.Kind)) if en.c == nil { tpyS := C.PyObject_Str(c(e.Kind)) defer C.decref(tpyS) ts = C.GoString(C.PyString_AsString(tpyS)) } else { if en.m != nil { ts = C.GoString(en.m) + "." } ts += C.GoString(en.c) } pyS := C.PyObject_Str(c(e.Value)) defer C.decref(pyS) s := C.GoString(C.PyString_AsString(pyS)) return fmt.Sprintf("%s: %s", ts, s) }
func (e *Error) release() error { if e.Kind != nil { e.Kind.Decref() e.Value.Decref() e.Kind = nil e.Value = nil if e.tb != nil { C.decref(e.tb) e.tb = nil } } return nil }
func Import(name string) (*Module, error) { s := C.CString(name) defer C.free(unsafe.Pointer(s)) pyName := C.PyString_FromString(s) defer C.decref(pyName) obj := C.PyImport_Import(pyName) if obj == nil { return nil, exception() } return newModule(obj), nil }
func (u *Unicode) String() string { if u == nil { return "<nil>" } s := C.PyUnicode_AsUTF8String(c(u)) if s == nil { panic(exception()) } defer C.decref(s) ret := C.PyString_AsString(s) if ret == nil { panic(exception()) } return C.GoString(ret) }
func Raise(err error) { var val *C.PyObject var exc = C.PyExc_Exception e, ok := err.(*Error) if ok { exc = e.Kind.c() val = e.Value.c() } else { v := NewString(errors.Detail(err)) val = v.c() defer C.decref(val) } C.PyErr_SetObject(exc, val) }
func (obj *Base) CallMethodObject(name string, args *Tuple) (*Base, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) f := C.PyObject_GetAttrString(obj.c(), cname) if f == nil { return nil, AttributeError.Err(name) } defer C.decref(f) if C.PyCallable_Check(f) == 0 { return nil, TypeError.Err("attribute of type '%s' is not callable", name) } ret := C.PyObject_CallObject(f, args.c()) return obj2ObjErr(ret) }
func (closure *Closure) NewFunction(name string, nin int, doc string) *Base { d := &closure.methodDef d.ml_name = C.CString(name) defer C.free(unsafe.Pointer(d.ml_name)) if C.setMethod(d, C.int(nin)) != 0 { panic("Invalid arguments: nin") } if doc != "" { d.ml_doc = C.CString(doc) defer C.free(unsafe.Pointer(d.ml_doc)) } ctx := uintptr(unsafe.Pointer(closure)) self := C.PyLong_FromLongLong(C.longlong(ctx)) defer C.decref(self) f := C.PyCFunction_NewEx(d, self, nil) return (*Base)(unsafe.Pointer(f)) }
func (obj *BaseObject) CallMethodObjArgs(name string, args ...Object) (Object, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) f := C.PyObject_GetAttrString(c(obj), cname) if f == nil { return nil, AttributeError.Err(name) } defer C.decref(f) if C.PyCallable_Check(f) == 0 { return nil, TypeError.Err("attribute of type '%s' is not callable", name) } t, err := PackTuple(args...) if err != nil { return nil, err } ret := C.PyObject_CallObject(f, c(t)) return obj2ObjErr(ret) }
// Decref decrements obj's reference count, obj may not be nil. func (obj *Base) Decref() { C.decref(obj.c()) }
// Decref decrements obj's reference count, obj may not be nil. func (obj *AbstractObject) Decref() { C.decref(c(obj)) }
// Decref decrements obj's reference count, obj may be nil. func Decref(obj Object) { if obj != nil { C.decref(c(obj)) } }