-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.go
77 lines (65 loc) · 1.53 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
package python
// #include <Python.h>
//
// void pyRelease(PyObject *o)
// {
// Py_DECREF(o);
// }
//
// void pyRetain(PyObject *o)
// {
// Py_INCREF(o);
// }
import "C"
// PyObject represents a python object
type PyObject struct {
ptr *C.PyObject
}
// GetAttrString returns an attribute based on a string key
func (obj *PyObject) GetAttrString(key string) (pyObj *PyObject) {
ptr := C.PyObject_GetAttrString(obj.ptr, C.CString(key))
if ptr != nil {
return &PyObject{ptr: ptr}
}
return
}
// ToLong returns a PyLong
func (obj *PyObject) ToLong() *PyLong {
return &PyLong{PyObject: *obj}
}
// ToFloat returns a PyFloat
func (obj *PyObject) ToFloat() *PyFloat {
return &PyFloat{PyObject: *obj}
}
// ToBool returns a PyBool
func (obj *PyObject) ToBool() *PyBool {
return &PyBool{PyObject: *obj}
}
// ToList returns a PyList
func (obj *PyObject) ToList() *PyList {
return &PyList{PyObject: *obj}
}
// Callable tells if the PyObject is callable
func (obj *PyObject) Callable() bool {
return C.PyCallable_Check(obj.ptr) == 1
}
// Call call a callable Python object callable_object, with arguments given by the tuple args
func (obj *PyObject) Call(args *PyTuple) *PyObject {
ptr := C.PyObject_CallObject(obj.ptr, args.ptr)
if ptr != nil {
return &PyObject{ptr: ptr}
}
return nil
}
// Release decreases retain count for PyObject
func (obj *PyObject) Release() {
if obj.ptr != nil {
C.pyRetain(obj.ptr)
}
}
// Retain increases retain count for PyObject
func (obj *PyObject) Retain() {
if obj.ptr != nil {
C.pyRelease(obj.ptr)
}
}