-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
254 lines (246 loc) · 6.31 KB
/
convert.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gp
/*
#include <Python.h>
*/
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
func From(from any) Object {
switch v := from.(type) {
case Objecter:
return newObject(v.cpyObj())
case int8:
return newObject(C.PyLong_FromLong(C.long(v)))
case int16:
return newObject(C.PyLong_FromLong(C.long(v)))
case int32:
return newObject(C.PyLong_FromLong(C.long(v)))
case int64:
return newObject(C.PyLong_FromLongLong(C.longlong(v)))
case int:
if unsafe.Sizeof(v) == unsafe.Sizeof(int64(0)) {
return newObject(C.PyLong_FromLongLong(C.longlong(v)))
} else {
return newObject(C.PyLong_FromLong(C.long(v)))
}
case uint8:
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
case uint16:
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
case uint32:
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
case uint64:
return newObject(C.PyLong_FromUnsignedLongLong(C.ulonglong(v)))
case uint:
if unsafe.Sizeof(v) == unsafe.Sizeof(uint64(0)) {
return newObject(C.PyLong_FromUnsignedLongLong(C.ulonglong(v)))
} else {
return newObject(C.PyLong_FromUnsignedLong(C.ulong(v)))
}
case float64:
return newObject(C.PyFloat_FromDouble(C.double(v)))
case float32:
return newObject(C.PyFloat_FromDouble(C.double(v)))
case string:
cstr := AllocCStr(v)
o := C.PyUnicode_FromString(cstr)
C.free(unsafe.Pointer(cstr))
return newObject(o)
case complex128:
return MakeComplex(v).Object
case complex64:
return MakeComplex(complex128(v)).Object
case []byte:
return MakeBytes(v).Object
case bool:
if v {
return True().Object
} else {
return False().Object
}
case *C.PyObject:
return newObject(v)
default:
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Ptr:
if vv.Elem().Type().Kind() == reflect.Struct {
maps := getGlobalData()
if pyType, ok := maps.pyTypes[vv.Elem().Type()]; ok {
wrapper := allocWrapper((*C.PyTypeObject)(unsafe.Pointer(pyType)), vv.Interface())
return newObject((*C.PyObject)(unsafe.Pointer(wrapper)))
}
}
return From(vv.Elem().Interface())
case reflect.Slice:
return fromSlice(vv).Object
case reflect.Map:
return fromMap(vv).Object
case reflect.Struct:
return fromStruct(vv)
case reflect.Func:
return FuncOf(vv.Interface()).Object
}
panic(fmt.Errorf("unsupported type for Python: %T\n", v))
}
}
func ToValue(from Object, to reflect.Value) bool {
if !to.IsValid() || !to.CanSet() {
panic(fmt.Errorf("value is not valid or cannot be set: %v\n", to))
}
switch to.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
if from.IsLong() {
to.SetInt(cast[Long](from).Int64())
} else {
return false
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
if from.IsLong() {
to.SetUint(cast[Long](from).Uint64())
} else {
return false
}
case reflect.Float32, reflect.Float64:
if from.IsFloat() || from.IsLong() {
to.SetFloat(cast[Float](from).Float64())
} else {
return false
}
case reflect.Complex64, reflect.Complex128:
if from.IsComplex() {
to.SetComplex(cast[Complex](from).Complex128())
} else {
return false
}
case reflect.String:
if from.IsStr() {
to.SetString(cast[Str](from).String())
} else {
return false
}
case reflect.Bool:
if from.IsBool() {
to.SetBool(cast[Bool](from).Bool())
} else {
return false
}
case reflect.Slice:
if to.Type().Elem().Kind() == reflect.Uint8 { // []byte
if from.IsBytes() {
to.SetBytes(cast[Bytes](from).Bytes())
} else {
return false
}
} else {
if from.IsList() {
list := cast[List](from)
l := list.Len()
slice := reflect.MakeSlice(to.Type(), l, l)
for i := 0; i < l; i++ {
item := list.GetItem(i)
ToValue(item, slice.Index(i))
}
to.Set(slice)
} else {
return false
}
}
case reflect.Map:
if from.IsDict() {
t := to.Type()
to.Set(reflect.MakeMap(t))
dict := cast[Dict](from)
dict.Items()(func(key, value Object) bool {
vk := reflect.New(t.Key()).Elem()
vv := reflect.New(t.Elem()).Elem()
if !ToValue(key, vk) || !ToValue(value, vv) {
return false
}
to.SetMapIndex(vk, vv)
return true
})
return true
} else {
return false
}
case reflect.Struct:
if from.IsDict() {
dict := cast[Dict](from)
t := to.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
key := goNameToPythonName(field.Name)
if !dict.HasKey(MakeStr(key)) {
continue
}
value := dict.Get(MakeStr(key))
if !ToValue(value, to.Field(i)) {
SetTypeError(fmt.Errorf("failed to convert value to %v", field.Name))
return false
}
}
} else {
maps := getGlobalData()
tyMeta := maps.typeMetas[from.Type().cpyObj()]
if tyMeta == nil {
return false
}
wrapper := (*wrapperType)(unsafe.Pointer(from.cpyObj()))
to.Set(reflect.ValueOf(wrapper.goObj).Elem())
return true
}
default:
panic(fmt.Errorf("unsupported type conversion from Python object to %v", to.Type()))
}
return true
}
func fromSlice(v reflect.Value) List {
l := v.Len()
list := newList(C.PyList_New(C.Py_ssize_t(l)))
ty := v.Type().Elem()
maps := getGlobalData()
pyType, ok := maps.pyTypes[ty]
if !ok {
for i := 0; i < l; i++ {
C.PyList_SetItem(list.obj, C.Py_ssize_t(i), From(v.Index(i).Interface()).obj)
}
} else {
for i := 0; i < l; i++ {
elem := v.Index(i)
elemAddr := elem.Addr()
wrapper := allocWrapper((*C.PyTypeObject)(unsafe.Pointer(pyType)), 0)
wrapper.goObj = elemAddr.Interface()
C.PyList_SetItem(list.obj, C.Py_ssize_t(i), (*C.PyObject)(unsafe.Pointer(wrapper)))
}
}
return list
}
func fromMap(v reflect.Value) Dict {
dict := newDict(C.PyDict_New())
iter := v.MapRange()
for iter.Next() {
dict.Set(From(iter.Key().Interface()), From(iter.Value().Interface()))
}
return dict
}
func fromStruct(v reflect.Value) Object {
ty := v.Type()
maps := getGlobalData()
if typeObj, ok := maps.pyTypes[ty]; ok {
ptr := reflect.New(ty)
ptr.Elem().Set(v)
wrapper := allocWrapper((*C.PyTypeObject)(unsafe.Pointer(typeObj)), ptr.Interface())
return newObject((*C.PyObject)(unsafe.Pointer(wrapper)))
}
dict := newDict(C.PyDict_New())
for i := 0; i < ty.NumField(); i++ {
field := ty.Field(i)
key := goNameToPythonName(field.Name)
dict.Set(MakeStr(key).Object, From(v.Field(i).Interface()))
}
return dict.Object
}