Example #1
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)
}
Example #2
0
func encodeDictItem(pyDict *C.PyObject, key, value interface{}) (err error) {
	pyKey, err := encode(key)
	if err != nil {
		return
	}
	defer C.DECREF(pyKey)

	pyValue, err := encode(value)
	if err != nil {
		return
	}
	defer C.DECREF(pyValue)

	if C.PyDict_SetItem(pyDict, pyKey, pyValue) < 0 {
		err = getError()
	}
	return
}
func GenerateEnviron(r *http.Request) *C.PyObject {
	_environ := C.PyDict_New()

	for k, _items := range r.Header {
		_values_tuple := C.PyTuple_New(C.Py_ssize_t(len(_items)))
		for i, _item := range _items {
			C.PyTuple_SetItem(
				_values_tuple,
				C.Py_ssize_t(i),
				PyString_FromString(_item),
			)
		}
		C.PyDict_SetItem(
			_environ,
			PyString_FromString(k),
			_values_tuple,
		)
	}
	//_environ = upgrade_to_wsgi(r, _environ)

	return _environ
}
Example #4
0
// SetItem inserts "val" into dictionary d with the key "key".  If "key" is not
// hashable, then a TypeError will be returned.
func (d *Dict) SetItem(key, val Object) error {
	ret := C.PyDict_SetItem(c(d), c(key), c(val))
	return int2Err(ret)
}
func upgrade_header_to_wsgi(r *http.Request) *C.PyObject {
	_environ := C.PyDict_New()

	for k, _items := range r.Header {
		_values_tuple := C.PyTuple_New(C.Py_ssize_t(len(_items)))
		for i, _item := range _items {
			C.PyTuple_SetItem(
				_values_tuple,
				C.Py_ssize_t(i),
				PyString_FromString(_item),
			)
		}

		// convert header name
		_k := strings.ToUpper(strings.Replace(k, "-", "_", -1))
		C.PyDict_SetItem(
			_environ,
			PyString_FromString("HTTP_"+_k),
			_values_tuple,
		)
	}

	C.PyDict_SetItem(
		_environ,
		PyString_FromString("X_FROM"),
		PyString_FromString("gowsgi"),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("REQUEST_METHOD"),
		PyString_FromString(r.Method),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("SCRIPT_NAME"),
		PyString_FromString(r.URL.Path),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("PATH_INFO"),
		PyString_FromString(r.URL.Path),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("QUERY_STRING"),
		PyString_FromString(r.URL.RawQuery),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("CONTENT_TYPE"),
		PyString_FromString(""),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("CONTENT_LENGTH"),
		PyString_FromString("0"),
	)

	_host, _port, _ := net.SplitHostPort(r.Host)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("SERVER_NAME"),
		PyString_FromString(_host),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("SERVER_PORT"),
		PyString_FromString(_port),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("SERVER_PROTOCOL"),
		PyString_FromString(r.Proto),
	)

	C.PyDict_SetItem(
		_environ,
		PyString_FromString("wsgi.url_scheme"),
		PyString_FromString(strings.ToLower(strings.Split(r.Proto, "/")[0])),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("wsgi.multithread"),
		C.PyBool_FromLong(1),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("wsgi.multiprocess"),
		C.PyBool_FromLong(1),
	)
	C.PyDict_SetItem(
		_environ,
		PyString_FromString("wsgi.run_once"),
		C.PyBool_FromLong(0),
	)

	return _environ
}
Example #6
0
// int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
// Insert value into the dictionary p with a key of key. key must be hashable; if it isn’t, TypeError will be raised. Return 0 on success or -1 on failure.
// int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
// Insert value into the dictionary p using key as a key. key should be a char*. The key object is created using PyString_FromString(key). Return 0 on success or -1 on failure.
func PyDict_SetItem(self, key, val *PyObject) error {
	err := C.PyDict_SetItem(topy(self), topy(key), topy(val))
	return int2err(err)
}
Example #7
0
File: dict.go Project: MogeiWang/py
// SetItem inserts "val" into dictionary d with the key "key".  If "key" is not
// hashable, then a TypeError will be returned.
func (d *Dict) SetItem(key, val *Base) error {
	ret := C.PyDict_SetItem(d.c(), key.c(), val.c())
	return int2Err(ret)
}