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)
	}
}
示例#2
0
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)
}
示例#3
0
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)
}
示例#4
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
}
示例#5
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
}
示例#6
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
}
示例#7
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
}
示例#8
0
func pickleInit() {
	C.Py_Initialize()
	var cPickle *C.PyObject = C.PyImport_ImportModule(C.CString("cPickle"))
	pickleLoads = C.PyObject_GetAttrString(cPickle, C.CString("loads"))
	pickleDumps = C.PyObject_GetAttrString(cPickle, C.CString("dumps"))
	highestProtocol = C.PyObject_GetAttrString(cPickle, C.CString("HIGHEST_PROTOCOL"))
	C.Py_DecRef(cPickle)
	initialized = 1
}
示例#9
0
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 wsgi_callback(r *http.Request, w http.ResponseWriter) {
	_environ := upgrade_header_to_wsgi(r)

	_body_request, _ := ioutil.ReadAll(r.Body)
	_body_c := C.CString(string(_body_request))
	defer C.free(unsafe.Pointer(_body_c))

	_response := C.run_wsgi_application(_body_c, _environ)
	defer func() {
		C.Py_DecRef(_response.body)
		C.Py_DecRef(_response.headers)
		C.Py_DecRef(_response.error)
	}()
	if _response.headers == nil || int(_response.status) == 0 {
		w.WriteHeader(500)
		log_wsgi.Error("failed to run python wsgi module.")
		return
	}

	// parse header
	for i := 0; i < int(C.PyList_Size(_response.headers)); i++ {
		_h := C.PyList_GetItem(_response.headers, C.Py_ssize_t(i))
		_k := C.PyTuple_GetItem(_h, C.Py_ssize_t(0))
		_v := C.PyTuple_GetItem(_h, C.Py_ssize_t(1))
		w.Header().Set(
			PyString_AsString(_k),
			PyString_AsString(_v),
		)
	}

	// write body
	w.WriteHeader(int(_response.status))
	if _response.body == nil {
		return
	}
	write_response_body(w, _response.body)
}
示例#11
0
文件: object.go 项目: z0mbie42/py3
func (self *PyObject) DecRef() {
	C.Py_DecRef(self.ptr)
}
示例#12
0
// void Py_CLEAR(PyObject *o)
// Clear sets the PyObject's internal pointer to nil
// before calling Py_DecRef. This avoids the potential issues with
// Python code called by the deallocator referencing invalid,
// partially-deallocated data.
func (self *PyObject) Clear() {
	tmp := self.ptr
	self.ptr = nil
	C.Py_DecRef(tmp)
}