示例#1
0
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.PyTuple_Size(items)); i++ {
			item := C.PyTuple_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
	} else if C.myPyTuple_Check(o) != 0 {
		length := int(C.PyTuple_Size(o))
		list := make([]interface{}, length)
		for i := 0; i < length; i++ {
			list[i] = pyObjToInterface(C.PyTuple_GetItem(o, C.Py_ssize_t(i)))
		}
		return list
	}
	return nil
}
func write_response_body(w http.ResponseWriter, iterator *C.PyObject) {
	_iterator := C.PyObject_GetIter(iterator)
	defer func() {
		C.Py_DecRef(_iterator)
		C.Py_DecRef(iterator)
	}()

	if _iterator == nil {
		return
	}

	for {
		item := C.PyIter_Next(_iterator)
		if item == nil || C._PyString_Check(item) != 1 {
			C.Py_DecRef(item)
			break
		}
		length := C.PyString_Size(item)
		if length < 1 {
			C.Py_DecRef(item)
			break
		}

		_s := C.PyString_AsString(item)

		w.Write([]byte(C.GoString(_s)))
		C.Py_DecRef(item)
	}
}
示例#3
0
func PickleDumps(v interface{}) []byte {
	pickleLock.Lock()
	if initialized == 0 {
		pickleInit()
	}
	obj := interfaceToPyObj(v)
	str := C.PyObject_CallFunction2(pickleDumps, obj, highestProtocol)
	gobytes := C.GoBytes(unsafe.Pointer(C.PyString_AsString(str)), C.int(C.PyString_Size(str)))
	C.Py_DecRef(obj)
	C.Py_DecRef(str)
	pickleLock.Unlock()
	return gobytes
}
示例#4
0
func (s *String) Size() int64 {
	ret := C.PyString_Size(c(s))
	return int64(ret)
}
示例#5
0
// Py_ssize_t PyString_Size(PyObject *string)
// Return the length of the string in string object string.
//
// Changed in version 2.5: This function returned an int type. This might require changes in your code for properly supporting 64-bit systems.
func PyString_Size(self *PyObject) int {
	sz := C.PyString_Size(topy(self))
	return int(sz)
}