forked from redbo/goswiftobj
/
pickle.go
118 lines (107 loc) · 3.66 KB
/
pickle.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
package main
/*
#cgo pkg-config: python
#include <Python.h>
#define INLINE __attribute__((always_inline))
INLINE int myPyString_Check(PyObject *o) {return PyString_Check(o);}
INLINE int myPyDict_Check(PyObject *o) {return PyDict_Check(o);}
INLINE int myPyInt_Check(PyObject *o) {return PyInt_Check(o);}
INLINE PyObject *PyObject_CallFunction1(PyObject *f, PyObject *o1)
{return PyObject_CallFunctionObjArgs(f, o1, NULL);}
INLINE PyObject *PyNone() {Py_INCREF(Py_None); return Py_None;}
*/
import "C"
import (
"sync"
"unsafe"
)
var initialized int = 0
var pickle_loads *C.PyObject
var pickle_dumps *C.PyObject
var pickle_lock sync.Mutex
func _pickle_init () {
if initialized == 0 {
C.Py_Initialize()
var cPickle *C.PyObject = C.PyImport_ImportModule(C.CString("cPickle"))
pickle_loads = C.PyObject_GetAttrString(cPickle, C.CString("loads"))
pickle_dumps = C.PyObject_GetAttrString(cPickle, C.CString("dumps"))
C.Py_DecRef(cPickle)
initialized = 1
}
}
func pyObjToInterface(o *C.PyObject) (interface {}) {
if C.myPyString_Check(o) != 0 {
return C.GoStringN(C.PyString_AsString(o), C.int(C.PyString_Size(o)))
} else if C.myPyInt_Check(o) != 0 {
return int64(C.PyInt_AsLong(o))
} else if C.myPyDict_Check(o) != 0 {
v := make(map[interface{}]interface{})
items := C.PyDict_Items(o)
for i := 0; i < int(C.PyList_Size(items)); i++ {
item := C.PyList_GetItem(items, C.Py_ssize_t(i))
key := C.PyTuple_GetItem(item, 0)
value := C.PyTuple_GetItem(item, 1)
v[pyObjToInterface(key)] = pyObjToInterface(value)
}
C.Py_DecRef(items)
return v
}
return nil
}
func dictAddItem(dict *C.PyObject, key interface{}, value interface{}) {
pykey := interfaceToPyObj(key)
pyvalue := interfaceToPyObj(value)
C.PyDict_SetItem(dict, pykey, pyvalue)
C.Py_DecRef(pykey)
C.Py_DecRef(pyvalue)
}
func interfaceToPyObj(o interface{}) (*C.PyObject) {
switch o.(type) {
case int:
return C.PyInt_FromLong(C.long(o.(int)))
case int64:
return C.PyInt_FromLong(C.long(o.(int64)))
case string:
strvalue := C.CString(o.(string))
defer C.free(unsafe.Pointer(strvalue))
return C.PyString_FromStringAndSize(strvalue, C.Py_ssize_t(len(o.(string))))
case map[interface{}]interface{}:
dict := C.PyDict_New()
for key, value := range o.(map[interface{}]interface{}) {dictAddItem(dict, key, value)}
return dict
case map[string]string:
dict := C.PyDict_New()
for key, value := range o.(map[string]string) {dictAddItem(dict, key, value)}
return dict
case map[string]interface{}:
dict := C.PyDict_New()
for key, value := range o.(map[string]interface{}) {dictAddItem(dict, key, value)}
return dict
default:
return C.PyNone()
}
}
func PickleLoads(data string) (interface{}) {
pickle_lock.Lock()
_pickle_init()
datastr := C.CString(data)
str := C.PyString_FromStringAndSize(datastr, C.Py_ssize_t(len(data)))
C.free(unsafe.Pointer(datastr))
obj := C.PyObject_CallFunction1(pickle_loads, str)
v := pyObjToInterface(obj)
C.Py_DecRef(obj)
C.Py_DecRef(str)
pickle_lock.Unlock()
return v
}
func PickleDumps(v interface{}) (string) {
pickle_lock.Lock()
_pickle_init()
obj := interfaceToPyObj(v)
str := C.PyObject_CallFunction1(pickle_dumps, obj)
gostr := pyObjToInterface(str)
C.Py_DecRef(obj)
C.Py_DecRef(str)
pickle_lock.Unlock()
return gostr.(string)
}