Ejemplo n.º 1
0
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()
	}
}
Ejemplo n.º 2
0
func PickleLoads(data []byte) interface{} {
	pickleLock.Lock()
	if initialized == 0 {
		pickleInit()
	}
	str := C.PyString_FromStringAndSize((*C.char)(unsafe.Pointer(&data[0])), C.Py_ssize_t(len(data)))
	obj := C.PyObject_CallFunction1(pickleLoads, str)
	v := pyObjToInterface(obj)
	C.Py_DecRef(obj)
	C.Py_DecRef(str)
	pickleLock.Unlock()
	return v
}
Ejemplo n.º 3
0
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
}
Ejemplo n.º 4
0
// PyObject* PyString_FromStringAndSize(const char *v, Py_ssize_t len)
// Return value: New reference.
// Return a new string object with a copy of the string v as value and length len on success, and NULL on failure. If v is NULL, the contents of the string are uninitialized.
//
// Changed in version 2.5: This function used an int type for len. This might require changes in your code for properly supporting 64-bit systems.
func PyString_FromStringAndSize(v string, sz int) *PyObject {
	cstr := C.CString(v)
	defer C.free(unsafe.Pointer(cstr))
	return togo(C.PyString_FromStringAndSize(cstr, C.Py_ssize_t(sz)))
}
Ejemplo n.º 5
0
// encode translates a Go value (or a wrapped Python object) to a Python
// object.
func encode(x interface{}) (pyValue *C.PyObject, err error) {
	if x == nil {
		pyValue = C.None_INCREF()
		return
	}

	switch value := x.(type) {
	case bool:
		var i C.long
		if value {
			i = 1
		}
		pyValue = C.PyBool_FromLong(i)

	case byte: // alias uint8
		c := C.char(value)
		pyValue = C.PyString_FromStringAndSize(&c, 1)

	case complex64:
		pyValue = C.PyComplex_FromDoubles(C.double(real(value)), C.double(imag(value)))

	case complex128:
		pyValue = C.PyComplex_FromDoubles(C.double(real(value)), C.double(imag(value)))

	case float32:
		pyValue = C.PyFloat_FromDouble(C.double(value))

	case float64:
		pyValue = C.PyFloat_FromDouble(C.double(value))

	case int: // alias rune
		pyValue = C.PyInt_FromLong(C.long(value))

	case int8:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int16:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int32:
		pyValue = C.PyInt_FromLong(C.long(value))

	case int64:
		pyValue = C.Long_FromInt64(C.int64_t(value))

	case string:
		pyValue = C.String_FromGoStringPtr(unsafe.Pointer(&value))

	case uint:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uint16:
		pyValue = C.PyInt_FromLong(C.long(value))

	case uint32:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uint64:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case uintptr:
		pyValue = C.Long_FromUint64(C.uint64_t(value))

	case []interface{}:
		return encodeTuple(value)

	case map[interface{}]interface{}:
		return encodeDict(value)

	case *object:
		pyValue = value.pyObject
		C.INCREF(pyValue)

	default:
		err = fmt.Errorf("unable to translate %t to Python", x)
		return
	}

	if pyValue == nil {
		err = getError()
	}
	return
}