-
Notifications
You must be signed in to change notification settings - Fork 11
/
object.go
90 lines (77 loc) · 1.83 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package python
// See:
// https://docs.python.org/3/c-api/object.html
import (
"unsafe"
)
/*
#define PY_SSIZE_T_CLEAN
#include <Python.h>
*/
import "C"
// fmt.Stringer interface
func (self *Reference) String() string {
if str := C.PyObject_Str(self.Object); str != nil {
if data := C.PyUnicode_AsUTF8String(str); data != nil {
defer C.Py_DecRef(data)
if string_ := C.PyBytes_AsString(data); string_ != nil {
return C.GoString(string_)
} else {
return ""
}
} else {
return ""
}
} else {
return ""
}
}
func (self *Reference) Acquire() {
C.Py_IncRef(self.Object)
}
func (self *Reference) Release() {
C.Py_DecRef(self.Object)
}
func (self *Reference) Str() (*Reference, error) {
if str := C.PyObject_Str(self.Object); str != nil {
return NewReference(str), nil
} else {
return nil, GetError()
}
}
func (self *Reference) GetAttr(name string) (*Reference, error) {
name_ := C.CString(name)
defer C.free(unsafe.Pointer(name_))
if attr := C.PyObject_GetAttrString(self.Object, name_); attr != nil {
return NewReference(attr), nil
} else {
return nil, GetError()
}
}
func (self *Reference) SetAttr(name string, reference *Reference) error {
name_ := C.CString(name)
defer C.free(unsafe.Pointer(name_))
if C.PyObject_SetAttrString(self.Object, name_, reference.Object) == 0 {
return nil
} else {
return GetError()
}
}
func (self *Reference) Call(args ...interface{}) (*Reference, error) {
if args_, err := NewTuple(args...); err == nil {
if kw, err := NewDict(); err == nil {
return self.CallRaw(args_, kw)
} else {
return nil, err
}
} else {
return nil, err
}
}
func (self *Reference) CallRaw(args *Reference, kw *Reference) (*Reference, error) {
if r := C.PyObject_Call(self.Object, args.Object, kw.Object); r != nil {
return NewReference(r), nil
} else {
return nil, GetError()
}
}