Пример #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()
	}
}
Пример #2
0
// NewDict creates a new empty dictionary.
//
// Return value: New Reference.
func NewDict() (*Dict, error) {
	ret := C.PyDict_New()
	if ret == nil {
		return nil, exception()
	}
	return newDict(ret), nil
}
Пример #3
0
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
}
Пример #4
0
// encodeDict translates a Go map to a Python object.
func encodeDict(m map[interface{}]interface{}) (pyDict *C.PyObject, err error) {
	pyDict = C.PyDict_New()

	var ok bool

	defer func() {
		if !ok {
			C.DECREF(pyDict)
			pyDict = nil
		}
	}()

	for key, value := range m {
		if err = encodeDictItem(pyDict, key, value); err != nil {
			return
		}
	}

	ok = true

	return
}
Пример #5
0
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
}
Пример #6
0
// PyObject* PyDict_New()
// Return value: New reference.
// Return a new empty dictionary, or NULL on failure.
func PyDict_New() *PyObject {
	return togo(C.PyDict_New())
}
Пример #7
0
// NewDict creates a new empty dictionary.
//
// Return value: New Reference.
func NewDict() *Dict {
	ret := C.PyDict_New()
	return newDict(ret)
}