-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprimitive.go
400 lines (331 loc) · 8.11 KB
/
primitive.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package python
// See:
// https://docs.python.org/3/c-api/concrete.html
import (
"fmt"
"unsafe"
)
/*
#define PY_SSIZE_T_CLEAN
#include <Python.h>
*/
import "C"
func NewPrimitiveReference(value interface{}) (*Reference, error) {
if value == nil {
return None, nil
}
switch value_ := value.(type) {
case *Reference:
return value_, nil
case bool:
if value_ {
return True, nil
} else {
return False, nil
}
case int64:
return NewLong(value_)
case int32:
return NewLong(int64(value_))
case int8:
return NewLong(int64(value_))
case int:
return NewLong(int64(value_))
case float64:
return NewFloat(value_)
case float32:
return NewFloat(float64(value_))
case string:
return NewUnicode(value_)
case []interface{}:
return NewList(value_...)
case []byte:
return NewBytes(value_)
}
return nil, fmt.Errorf("unsupported primitive: %s", value)
}
//
// None
//
var None = NewReference(C.Py_None)
//
// Bool
//
var BoolType = NewType(&C.PyBool_Type)
var True = NewReference(C.Py_True)
var False = NewReference(C.Py_False)
func (self *Reference) IsBool() bool {
return self.Type().IsSubtype(BoolType)
}
func (self *Reference) ToBool() bool {
switch self.Object {
case C.Py_True:
return true
case C.Py_False:
return false
}
return false
}
//
// Long
//
var LongType = NewType(&C.PyLong_Type)
func NewLong(value int64) (*Reference, error) {
if long := C.PyLong_FromLong(C.long(value)); long != nil {
return NewReference(long), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsLong() bool {
// More efficient to use the flag
return self.Type().HasFlag(C.Py_TPFLAGS_LONG_SUBCLASS)
}
func (self *Reference) ToInt64() (int64, error) {
if long := C.PyLong_AsLong(self.Object); !HasException() {
return int64(long), nil
} else {
return 0, GetError()
}
}
//
// Float
//
var FloatType = NewType(&C.PyFloat_Type)
func NewFloat(value float64) (*Reference, error) {
if float := C.PyFloat_FromDouble(C.double(value)); float != nil {
return NewReference(float), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsFloat() bool {
return self.Type().IsSubtype(FloatType)
}
func (self *Reference) ToFloat64() (float64, error) {
if double := C.PyFloat_AsDouble(self.Object); !HasException() {
return float64(double), nil
} else {
return 0.0, GetError()
}
}
//
// Unicode
//
var UnicodeType = NewType(&C.PyUnicode_Type)
func NewUnicode(value string) (*Reference, error) {
value_ := C.CString(value)
defer C.free(unsafe.Pointer(value_))
if unicode := C.PyUnicode_FromString(value_); unicode != nil {
return NewReference(unicode), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsUnicode() bool {
// More efficient to use the flag
return self.Type().HasFlag(C.Py_TPFLAGS_UNICODE_SUBCLASS)
}
func (self *Reference) ToString() (string, error) {
if utf8stringBytes := C.PyUnicode_AsUTF8String(self.Object); utf8stringBytes != nil {
defer C.Py_DecRef(utf8stringBytes)
if utf8string := C.PyBytes_AsString(utf8stringBytes); utf8string != nil {
return C.GoString(utf8string), nil
} else {
return "", GetError()
}
} else {
return "", GetError()
}
}
//
// Tuple
//
var TupleType = NewType(&C.PyTuple_Type)
func NewTuple(items ...interface{}) (*Reference, error) {
if tuple, err := NewTupleRaw(len(items)); err == nil {
for index, item := range items {
if item_, err := NewPrimitiveReference(item); err == nil {
if err := tuple.SetTupleItem(index, item_); err != nil {
return nil, err
}
} else {
return nil, err
}
}
return tuple, nil
} else {
return nil, GetError()
}
}
func NewTupleRaw(size int) (*Reference, error) {
if tuple := C.PyTuple_New(C.long(size)); tuple != nil {
return NewReference(tuple), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsTuple() bool {
// More efficient to use the flag
return self.Type().HasFlag(C.Py_TPFLAGS_TUPLE_SUBCLASS)
}
func (self *Reference) SetTupleItem(index int, item *Reference) error {
if C.PyTuple_SetItem(self.Object, C.long(index), item.Object) == 0 {
return nil
} else {
return GetError()
}
}
//
// List
//
var ListType = NewType(&C.PyList_Type)
func NewList(items ...interface{}) (*Reference, error) {
if list, err := NewListRaw(len(items)); err == nil {
for index, item := range items {
if item_, err := NewPrimitiveReference(item); err == nil {
if err := list.SetListItem(index, item_); err != nil {
return nil, err
}
} else {
return nil, err
}
}
return list, nil
} else {
return nil, GetError()
}
}
func NewListRaw(size int) (*Reference, error) {
if list := C.PyList_New(C.long(size)); list != nil {
return NewReference(list), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsList() bool {
// More efficient to use the flag
return self.Type().HasFlag(C.Py_TPFLAGS_LIST_SUBCLASS)
}
func (self *Reference) SetListItem(index int, item *Reference) error {
if C.PyList_SetItem(self.Object, C.long(index), item.Object) == 0 {
return nil
} else {
return GetError()
}
}
//
// Dict
//
var DictType = NewType(&C.PyDict_Type)
func NewDict() (*Reference, error) {
if dict := C.PyDict_New(); dict != nil {
return NewReference(dict), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsDict() bool {
// More efficient to use the flag
return self.Type().HasFlag(C.Py_TPFLAGS_DICT_SUBCLASS)
}
func (self *Reference) SetDictItem(key *Reference, value *Reference) error {
if C.PyDict_SetItem(self.Object, key.Object, value.Object) == 0 {
return nil
} else {
return GetError()
}
}
//
// Set (mutable)
//
var SetType = NewType(&C.PySet_Type)
func NewSet(iterable *Reference) (*Reference, error) {
if set := C.PySet_New(iterable.Object); set != nil {
return NewReference(set), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsSet() bool {
return self.Type().IsSubtype(SetType)
}
//
// Frozen set (immutable)
//
var FrozenSetType = NewType(&C.PyFrozenSet_Type)
func NewFrozenSet(iterable *Reference) (*Reference, error) {
if frozenSet := C.PyFrozenSet_New(iterable.Object); frozenSet != nil {
return NewReference(frozenSet), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsFrozenSet() bool {
return self.Type().IsSubtype(FrozenSetType)
}
//
// Bytes (immutable)
//
var BytesType = NewType(&C.PyBytes_Type)
func NewBytes(value []byte) (*Reference, error) {
size := len(value)
value_ := C.CBytes(value)
defer C.free(value_) // TODO: check this!
if bytes := C.PyBytes_FromStringAndSize((*C.char)(value_), C.long(size)); bytes != nil {
return NewReference(bytes), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsBytes() bool {
// More efficient to use the flag
return self.Type().HasFlag(C.Py_TPFLAGS_BYTES_SUBCLASS)
}
func (self *Reference) ToBytes() ([]byte, error) {
if pointer, size, err := self.AccessBytes(); err == nil {
return C.GoBytes(pointer, C.int(size)), nil
} else {
return nil, err
}
}
func (self *Reference) AccessBytes() (unsafe.Pointer, int, error) {
if string_ := C.PyBytes_AsString(self.Object); string_ != nil {
size := C.PyBytes_Size(self.Object)
return unsafe.Pointer(string_), int(size), nil
} else {
return nil, 0, GetError()
}
}
//
// Byte array (mutable)
//
var ByteArrayType = NewType(&C.PyByteArray_Type)
func NewByteArray(value []byte) (*Reference, error) {
size := len(value)
value_ := C.CBytes(value)
defer C.free(value_) // TODO: check this!
if byteArray := C.PyByteArray_FromStringAndSize((*C.char)(value_), C.long(size)); byteArray != nil {
return NewReference(byteArray), nil
} else {
return nil, GetError()
}
}
func (self *Reference) IsByteArray() bool {
return self.Type().IsSubtype(ByteArrayType)
}
func (self *Reference) ByteArrayToBytes() ([]byte, error) {
if pointer, size, err := self.AccessByteArray(); err == nil {
return C.GoBytes(pointer, C.int(size)), nil
} else {
return nil, err
}
}
func (self *Reference) AccessByteArray() (unsafe.Pointer, int, error) {
if string_ := C.PyByteArray_AsString(self.Object); string_ != nil {
size := C.PyByteArray_Size(self.Object)
return unsafe.Pointer(string_), int(size), nil
} else {
return nil, 0, GetError()
}
}