-
Notifications
You must be signed in to change notification settings - Fork 9
/
object.go
164 lines (142 loc) · 3.2 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
// #include "gopy_types.h"
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
// static inline int exceptionCheck(PyObject *obj) {
// return PyExceptionClass_Check(obj);
// }
import "C"
import (
"reflect"
"sync"
"unsafe"
)
type Op int
const (
LT = Op(C.Py_LT)
LE = Op(C.Py_LE)
EQ = Op(C.Py_EQ)
NE = Op(C.Py_NE)
GT = Op(C.Py_GT)
GE = Op(C.Py_GE)
)
// Object is the generic interface that represents a Python object. All of the
// concrete types satisfy the Object interface.
type Object interface {
Base() *BaseObject
Type() *Type
Decref()
Incref()
IsTrue() bool
Not() bool
Free()
}
// None is the Python equivalent to nil.
var None = (*NoneObject)(unsafe.Pointer(&C._Py_NoneStruct))
// NoneObject is the type of the None value. The only value of this type is
// None.
type NoneObject struct {
AbstractObject
}
func (n *NoneObject) String() string {
return "None"
}
func c(obj Object) *C.PyObject {
if obj == nil {
return nil
}
return (*C.PyObject)(unsafe.Pointer(obj.Base()))
}
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))
}
var (
typeLock sync.RWMutex
types = make(map[*C.PyTypeObject]*Class)
)
func registerType(pyType *C.PyTypeObject, class *Class) {
typeLock.Lock()
defer typeLock.Unlock()
types[pyType] = class
}
func getType(pyType *C.PyTypeObject) (*Class, bool) {
typeLock.RLock()
defer typeLock.RUnlock()
class, ok := types[pyType]
return class, ok
}
func obj2Class(c *Class, obj *C.PyObject) (Object, bool) {
vp := reflect.NewAt(reflect.TypeOf(c.Pointer), unsafe.Pointer(&obj))
o, ok := vp.Elem().Interface().(Object)
return o, ok
}
func newObject(obj *C.PyObject) Object {
if obj == nil {
return nil
}
o := unsafe.Pointer(obj)
if o == unsafe.Pointer(None) {
return None
}
pyType := (*C.PyTypeObject)(obj.ob_type)
class, ok := getType(pyType)
if ok {
ret, ok := obj2Class(class, obj)
if ok {
return ret
}
}
for pyType.tp_base != nil {
pyType = (*C.PyTypeObject)(unsafe.Pointer(pyType.tp_base))
class, ok := getType(pyType)
if ok {
ret, ok := obj2Class(class, obj)
if ok {
return ret
}
}
}
switch C.getBaseGoPyType(obj) {
case C.GoPyList_Type:
return (*List)(o)
case C.GoPyTuple_Type:
return (*Tuple)(o)
case C.GoPyDict_Type:
return (*Dict)(o)
case C.GoPyUnicode_Type:
return (*Unicode)(o)
case C.GoPyBool_Type:
return newBool(obj)
case C.GoPyLong_Type:
return (*Long)(o)
case C.GoPyFloat_Type:
return (*Float)(o)
case C.GoPyModule_Type:
return (*Module)(o)
case C.GoPyType_Type:
return (*Type)(o)
case C.GoPyCode_Type:
return (*Code)(o)
case C.GoPyCFunction_Type:
return (*CFunction)(o)
case C.GoPyComplex_Type:
return (*Complex)(o)
case C.GoPyFrozenSet_Type:
return (*FrozenSet)(o)
case C.GoPySet_Type:
return (*Set)(o)
case C.GoPyFunction_Type:
return (*Function)(o)
}
if C.exceptionCheck(obj) != 0 {
return newException(obj)
}
return newBaseObject(obj)
}