Beispiel #1
0
static IDProperty *idp_from_PySequence(const char *name, PyObject *ob)
{
	Py_buffer buffer;
	bool use_buffer = false;

	if (PyObject_CheckBuffer(ob)) {
		PyObject_GetBuffer(ob, &buffer, PyBUF_SIMPLE | PyBUF_FORMAT);
		char format = *buffer.format;
		if (ELEM(format, 'i', 'f', 'd')) {
			use_buffer = true;
		}
		else {
			PyBuffer_Release(&buffer);
		}
	}

	if (use_buffer) {
		IDProperty *prop = idp_from_PySequence_Buffer(name, &buffer);
		PyBuffer_Release(&buffer);
		return prop;
	}
	else {
		PyObject *ob_seq_fast = PySequence_Fast(ob, "py -> idprop");
		if (ob_seq_fast != NULL) {
			IDProperty *prop = idp_from_PySequence_Fast(name, ob_seq_fast);
			Py_DECREF(ob_seq_fast);
			return prop;
		}
		else {
			return NULL;
		}
	}
}
Beispiel #2
0
static PyObject *
Stream_func_write(Stream *self, PyObject *args)
{
    PyObject *data;
    PyObject *callback = Py_None;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "O|O:write", &data, &callback)) {
        return NULL;
    }

    if (callback != Py_None && !PyCallable_Check(callback)) {
        PyErr_SetString(PyExc_TypeError, "'callback' must be a callable or None");
        return NULL;
    }

    if (PyObject_CheckBuffer(data)) {
        return pyuv__stream_write_bytes(self, data, callback, NULL);
    } else if (!PyUnicode_Check(data) && PySequence_Check(data)) {
        return pyuv__stream_write_sequence(self, data, callback, NULL);
    } else {
        PyErr_SetString(PyExc_TypeError, "only bytes and sequences are supported");
        return NULL;
    }
}
Beispiel #3
0
static PyObject *
pyaio_write(PyObject *dummy, PyObject *args) {

    PyObject *buffer;
    Py_buffer *buffer_view;

    int fd;
    Py_ssize_t offset, ret;

    Pyaio_cb *aio;
    PyObject *callback, *return_;
    Py_XINCREF(args);
    if (PyArg_ParseTuple(args, "iOnO:set_callback", &fd, &buffer,
                          &offset, &callback)) {
        if (!PyCallable_Check(callback)) {
            PyErr_SetString(PyExc_TypeError,
                    "parameter must be callable");
            return NULL;
        }
        if (!PyObject_CheckBuffer(buffer)) {
            PyErr_SetString(PyExc_TypeError,
                    "write buffer must support buffer interface");
            return NULL;
        }
        Py_XINCREF(callback); /* Add a reference to new callback */
        Py_XINCREF(buffer);
    }
    Py_XDECREF(args);

    aio = malloc(sizeof(Pyaio_cb));
    buffer_view = &(aio->buffer_view);
    /* Get a Buffer INCREF */
    PyObject_GetBuffer(buffer, buffer_view, PyBUF_CONTIG_RO);


    aio->cb = malloc(sizeof(struct aiocb));
    bzero((void *) aio->cb, sizeof(struct aiocb));
    aio->read = 0;  /* Write Operation */
    aio->callback = callback;
    aio->buffer = buffer;
    aio->cb->aio_buf = buffer_view->buf;
    aio->cb->aio_fildes = fd;
    aio->cb->aio_nbytes = buffer_view->len;
    aio->cb->aio_offset = offset;
    aio->cb->aio_sigevent.sigev_notify = SIGEV_THREAD;
    aio->cb->aio_sigevent.sigev_notify_attributes = NULL;
    aio->cb->aio_sigevent.sigev_notify_function = aio_completion_handler;
    aio->cb->aio_sigevent.sigev_value.sival_ptr = aio;

    ret = aio_write(aio->cb);

    if (ret < 0) {
        return PyErr_SetFromErrno(PyExc_IOError);
    }
    else {
        return_ = Py_BuildValue("n", ret);
        return return_;
    }
}
Beispiel #4
0
	DKObject<DKStream> OpenResourceStream(const DKString& name) const override
	{
		DKObject<DKStream> result = NULL;
		if (name.Length() > 0)
		{
			if (this->object && Py_IsInitialized())
			{
				DCObjectCallPyCallableGIL([&]()
				{
					PyObject* ret = PyObject_CallMethod(object,
						"openResourceStream", "N",
						PyUnicode_FromWideChar(name, -1));

					if (ret)
					{
						if (PyObject_TypeCheck(ret, DCStreamTypeObject()))
						{
							result = DCStreamToObject(ret);
						}
						else if (PyObject_TypeCheck(ret, DCDataTypeObject()))
						{
							result = DKOBJECT_NEW DKDataStream(DCDataToObject(ret));
						}
						else if (PyObject_CheckBuffer(ret))
						{
							auto cleanup = [](Py_buffer* view)
							{
								if (view->obj)
									PyBuffer_Release(view);
								delete view;
							};
							Py_buffer* view = new Py_buffer();
							if (PyObject_GetBuffer(ret, view, PyBUF_SIMPLE) == 0)
							{
								DKObject<DKData> data = DKData::StaticData(view->buf, view->len, true,
									DKFunction(cleanup)->Invocation(view));

								result = DKOBJECT_NEW DKDataStream(data);
							}
							else
								cleanup(view);
						}
					}
					Py_XDECREF(ret);
				});
			}
		}
		return result;
	}
Beispiel #5
0
static PyObject *
put_buf( PyObject *self, PyObject *args ) {
    (void)self;

    PyObject *py_chan, *b;
    // get arg objects
    if( !PyArg_ParseTuple(args, "OO", &py_chan, &b) ) {
        return NULL;
    }

    // parse channel
    ach_channel_t *c = parse_channel_pointer(py_chan);
    if( NULL == c ) {
        return NULL;
    }

    // parse buffer
    if( ! PyObject_CheckBuffer(b) ) {
        PyErr_SetString( PyExc_TypeError, "invalid buffer" );
        return NULL;
    }

    // view buffer
    Py_buffer buf;
    if( PyObject_GetBuffer( b, &buf, PyBUF_SIMPLE ) ) {
        PyErr_SetString( PyExc_BufferError, "couldn't view buffer" );
        return NULL;
    }

    // make the damn call
    ach_status_t r = ach_put( c, buf.buf, (size_t)buf.len );

    // check the result
    if( ACH_OK != r ) {
        PyErr_SetString( ach_py_error, ach_result_to_string(r) );
        return NULL;
    }

    // cleanup
    PyBuffer_Release(&buf);
    Py_RETURN_NONE;
}
Beispiel #6
0
static PyObject *
asciistr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyObject *self = NULL;
    PyObject *x = NULL;
    Py_buffer view;
    static char *kwlist[] = {"object", 0};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:asciistr", kwlist, &x)) {
        return NULL;
    }
    if (x == NULL)
        _Py_RETURN_ASCIISTR_EMPTY();

    if (PyObject_CheckBuffer(x)) {
        if (PyObject_GetBuffer(x, &view, PyBUF_CONTIG_RO) < 0) {
            return NULL;
        }

        self = PyUnicode_FromString((char *)view.buf);
        PyBuffer_Release(&view);
    } else {
        self = PyObject_Str(x);
    }

    if (PyUnicode_KIND(self) != PyUnicode_1BYTE_KIND) {
        Py_DECREF(self);
        PyErr_SetString(PyExc_ValueError, "asciistr must contain no high-byte characters");
        return (NULL);
    }
    if (PyUnicode_MAX_CHAR_VALUE(self) > 127) {
        Py_DECREF(self);
        PyErr_SetString(PyExc_ValueError, "asciistr must contain no high-byte characters");
        return (NULL);
    }

    /* Probably going to hell for this. */
    Py_TYPE(self) = type;

    return self;
}
inline bool
setViaBuffer(jarray array, int start, uint length, PyObject* sequence, setFnc setter) {
	//creates a PyMemoryView from sequence check for typeError,
	// if no underlying py_buff exists.
    if(! PyObject_CheckBuffer(sequence)) {
        return false;
    }

    // ensure memory is contiguous and 'C' ordered, this may involve a copy.
    PyObject* memview = PyMemoryView_GetContiguous(sequence, PyBUF_READ, 'C');
    // this function is defined in jpype_memory_view, but unusable?!
//    PyObject* memview = PyMemoryView_FromObject(sequence);

    // check for TypeError, if no underlying py_buff exists.
	PyObject* err = PyErr_Occurred();
	if (err) {
	    PyErr_Clear();
	    return false;
	}

	// create a memory view
	Py_buffer* py_buff = PyMemoryView_GET_BUFFER(memview);

    // ensure length of buffer contains enough elements somehow.
    if ((py_buff->len / sizeof(jelementtype)) != length) {
        std::stringstream ss;
        ss << "Underlying buffer does not contain requested number of elements! Has "
           << py_buff->len << ", but " << length <<" are requested. Element size is "
           << sizeof(jelementtype);
        RAISE(JPypeException, ss.str());
    }

    jarraytype a = (jarraytype)array;
    jelementtype* buffer = (jelementtype*) py_buff->buf;
    JPJavaEnv* env = JPEnv::getJava();

    try {
        (env->*setter)(a, start, length, buffer);
    } RETHROW_CATCH( /*cleanup*/ Py_DECREF(py_buff); Py_DECREF(memview); );
Beispiel #8
0
static bool get_buffer(PyObject *obj, Py_buffer *buf)
{
	if (PyUnicode_Check(obj)) {
		PyErr_SetString(PyExc_TypeError,
				"Unicode-objects must be encoded before hashing");
		return false;
	}
	if (!PyObject_CheckBuffer(obj)) {
		PyErr_SetString(PyExc_TypeError,
				"object supporting the buffer API required");
		return false;
	}
	if (PyObject_GetBuffer(obj, buf, PyBUF_SIMPLE) == -1) {
		return false;
	}
	if (buf->ndim > 1) {
		PyErr_SetString(PyExc_BufferError,
				"Buffer must be single dimension");
		PyBuffer_Release(buf);
		return false;
	}
	return true;
}
Beispiel #9
0
static PyObject *
binary_quote(binaryObject *self)
{
    char *to = NULL;
    const char *buffer = NULL;
    Py_ssize_t buffer_len;
    size_t len = 0;
    PyObject *rv = NULL;
#if HAS_MEMORYVIEW
    Py_buffer view;
    int got_view = 0;
#endif

    /* if we got a plain string or a buffer we escape it and save the buffer */

#if HAS_MEMORYVIEW
    if (PyObject_CheckBuffer(self->wrapped)) {
        if (0 > PyObject_GetBuffer(self->wrapped, &view, PyBUF_CONTIG_RO)) {
            goto exit;
        }
        got_view = 1;
        buffer = (const char *)(view.buf);
        buffer_len = view.len;
    }
#endif

#if HAS_BUFFER
    if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
        if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
                                  &buffer_len) < 0) {
            goto exit;
        }
    }
#endif

    if (!buffer) {
        goto exit;
    }

    /* escape and build quoted buffer */

    to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len,
        &len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
    if (to == NULL) {
        PyErr_NoMemory();
        goto exit;
    }

    if (len > 0)
        rv = Bytes_FromFormat(
            (self->conn && ((connectionObject*)self->conn)->equote)
                ? "E'%s'::bytea" : "'%s'::bytea" , to);
    else
        rv = Bytes_FromString("''::bytea");

exit:
    if (to) { PQfreemem(to); }
#if HAS_MEMORYVIEW
    if (got_view) { PyBuffer_Release(&view); }
#endif

    /* Allow Binary(None) to work */
    if (self->wrapped == Py_None) {
        Py_INCREF(psyco_null);
        rv = psyco_null;
    }

    /* if the wrapped object is not bytes or a buffer, this is an error */
    if (!rv && !PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError, "can't escape %s to binary",
            Py_TYPE(self->wrapped)->tp_name);
    }

    return rv;
}
Beispiel #10
0
/*
 * Convert a Python object to the values needed to create a voidptr.
 */
static int vp_convertor(PyObject *arg, struct vp_values *vp)
{
    void *ptr;
    SIP_SSIZE_T size = -1;
    int rw = TRUE;

    if (arg == Py_None)
        ptr = NULL;
#if defined(SIP_USE_PYCAPSULE)
    else if (PyCapsule_CheckExact(arg))
        ptr = PyCapsule_GetPointer(arg, NULL);
#endif
#if defined(SIP_SUPPORT_PYCOBJECT)
    else if (PyCObject_Check(arg))
        ptr = PyCObject_AsVoidPtr(arg);
#endif
    else if (PyObject_TypeCheck(arg, &sipVoidPtr_Type))
    {
        ptr = ((sipVoidPtrObject *)arg)->voidptr;
        size = ((sipVoidPtrObject *)arg)->size;
        rw = ((sipVoidPtrObject *)arg)->rw;
    }
#if PY_VERSION_HEX >= 0x02060300
    else if (PyObject_CheckBuffer(arg))
    {
        Py_buffer view;

        if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) < 0)
            return 0;

        ptr = view.buf;
        size = view.len;
        rw = !view.readonly;

        PyBuffer_Release(&view);
    }
#endif
#if PY_VERSION_HEX < 0x03000000
    else if (PyObject_AsReadBuffer(arg, (const void **)&ptr, &size) >= 0)
    {
        rw = (Py_TYPE(arg)->tp_as_buffer->bf_getwritebuffer != NULL);
    }
#endif
    else
    {
        PyErr_Clear();
        ptr = PyLong_AsVoidPtr(arg);

        if (PyErr_Occurred())
        {
#if PY_VERSION_HEX >= 0x03010000
            PyErr_SetString(PyExc_TypeError, "a single integer, CObject, None, buffer protocol implementor or another sip.voidptr object is required");
#else
            PyErr_SetString(PyExc_TypeError, "a single integer, Capsule, CObject, None, buffer protocol implementor or another sip.voidptr object is required");
#endif
            return 0;
        }
    }

    vp->voidptr = ptr;
    vp->size = size;
    vp->rw = rw;

    return 1;
}
Beispiel #11
0
NPY_NO_EXPORT int
PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
                              PyArray_Descr **out_dtype, int string_type)
{
    int i, size;
    PyArray_Descr *dtype = NULL;
    PyObject *ip;
    Py_buffer buffer_view;

    /* Check if it's an ndarray */
    if (PyArray_Check(obj)) {
        dtype = PyArray_DESCR((PyArrayObject *)obj);
        Py_INCREF(dtype);
        goto promote_types;
    }

    /* See if it's a python None */
    if (obj == Py_None) {
        dtype = PyArray_DescrFromType(NPY_OBJECT);
        if (dtype == NULL) {
            goto fail;
        }
        Py_INCREF(dtype);
        goto promote_types;
    }
    /* Check if it's a NumPy scalar */
    else if (PyArray_IsScalar(obj, Generic)) {
        if (!string_type) {
            dtype = PyArray_DescrFromScalar(obj);
            if (dtype == NULL) {
                goto fail;
            }
        }
        else {
            int itemsize;
            PyObject *temp;

            if (string_type == NPY_STRING) {
                if ((temp = PyObject_Str(obj)) == NULL) {
                    return -1;
                }
#if defined(NPY_PY3K)
    #if PY_VERSION_HEX >= 0x03030000
                itemsize = PyUnicode_GetLength(temp);
    #else
                itemsize = PyUnicode_GET_SIZE(temp);
    #endif
#else
                itemsize = PyString_GET_SIZE(temp);
#endif
            }
            else if (string_type == NPY_UNICODE) {
#if defined(NPY_PY3K)
                if ((temp = PyObject_Str(obj)) == NULL) {
#else
                if ((temp = PyObject_Unicode(obj)) == NULL) {
#endif
                    return -1;
                }
                itemsize = PyUnicode_GET_DATA_SIZE(temp);
#ifndef Py_UNICODE_WIDE
                itemsize <<= 1;
#endif
            }
            else {
                goto fail;
            }
            Py_DECREF(temp);
            if (*out_dtype != NULL &&
                    (*out_dtype)->type_num == string_type &&
                    (*out_dtype)->elsize >= itemsize) {
                return 0;
            }
            dtype = PyArray_DescrNewFromType(string_type);
            if (dtype == NULL) {
                goto fail;
            }
            dtype->elsize = itemsize;
        }
        goto promote_types;
    }

    /* Check if it's a Python scalar */
    dtype = _array_find_python_scalar_type(obj);
    if (dtype != NULL) {
        if (string_type) {
            int itemsize;
            PyObject *temp;

            if (string_type == NPY_STRING) {
                if ((temp = PyObject_Str(obj)) == NULL) {
                    return -1;
                }
#if defined(NPY_PY3K)
    #if PY_VERSION_HEX >= 0x03030000
                itemsize = PyUnicode_GetLength(temp);
    #else
                itemsize = PyUnicode_GET_SIZE(temp);
    #endif
#else
                itemsize = PyString_GET_SIZE(temp);
#endif
            }
            else if (string_type == NPY_UNICODE) {
#if defined(NPY_PY3K)
                if ((temp = PyObject_Str(obj)) == NULL) {
#else
                if ((temp = PyObject_Unicode(obj)) == NULL) {
#endif
                    return -1;
                }
                itemsize = PyUnicode_GET_DATA_SIZE(temp);
#ifndef Py_UNICODE_WIDE
                itemsize <<= 1;
#endif
            }
            else {
                goto fail;
            }
            Py_DECREF(temp);
            if (*out_dtype != NULL &&
                    (*out_dtype)->type_num == string_type &&
                    (*out_dtype)->elsize >= itemsize) {
                return 0;
            }
            dtype = PyArray_DescrNewFromType(string_type);
            if (dtype == NULL) {
                goto fail;
            }
            dtype->elsize = itemsize;
        }
        goto promote_types;
    }

    /* Check if it's an ASCII string */
    if (PyBytes_Check(obj)) {
        int itemsize = PyString_GET_SIZE(obj);

        /* If it's already a big enough string, don't bother type promoting */
        if (*out_dtype != NULL &&
                        (*out_dtype)->type_num == NPY_STRING &&
                        (*out_dtype)->elsize >= itemsize) {
            return 0;
        }
        dtype = PyArray_DescrNewFromType(NPY_STRING);
        if (dtype == NULL) {
            goto fail;
        }
        dtype->elsize = itemsize;
        goto promote_types;
    }

    /* Check if it's a Unicode string */
    if (PyUnicode_Check(obj)) {
        int itemsize = PyUnicode_GET_DATA_SIZE(obj);
#ifndef Py_UNICODE_WIDE
        itemsize <<= 1;
#endif

        /*
         * If it's already a big enough unicode object,
         * don't bother type promoting
         */
        if (*out_dtype != NULL &&
                        (*out_dtype)->type_num == NPY_UNICODE &&
                        (*out_dtype)->elsize >= itemsize) {
            return 0;
        }
        dtype = PyArray_DescrNewFromType(NPY_UNICODE);
        if (dtype == NULL) {
            goto fail;
        }
        dtype->elsize = itemsize;
        goto promote_types;
    }

    /* PEP 3118 buffer interface */
    if (PyObject_CheckBuffer(obj) == 1) {
        memset(&buffer_view, 0, sizeof(Py_buffer));
        if (PyObject_GetBuffer(obj, &buffer_view,
                               PyBUF_FORMAT|PyBUF_STRIDES) == 0 ||
            PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT) == 0) {

            PyErr_Clear();
            dtype = _descriptor_from_pep3118_format(buffer_view.format);
            PyBuffer_Release(&buffer_view);
            if (dtype) {
                goto promote_types;
            }
        }
        else if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_STRIDES) == 0 ||
                 PyObject_GetBuffer(obj, &buffer_view, PyBUF_SIMPLE) == 0) {

            PyErr_Clear();
            dtype = PyArray_DescrNewFromType(NPY_VOID);
            dtype->elsize = buffer_view.itemsize;
            PyBuffer_Release(&buffer_view);
            goto promote_types;
        }
        else {
            PyErr_Clear();
        }
    }

    /* The array interface */
    ip = PyArray_GetAttrString_SuppressException(obj, "__array_interface__");
    if (ip != NULL) {
        if (PyDict_Check(ip)) {
            PyObject *typestr;
#if defined(NPY_PY3K)
            PyObject *tmp = NULL;
#endif
            typestr = PyDict_GetItemString(ip, "typestr");
#if defined(NPY_PY3K)
            /* Allow unicode type strings */
            if (PyUnicode_Check(typestr)) {
                tmp = PyUnicode_AsASCIIString(typestr);
                typestr = tmp;
            }
#endif
            if (typestr && PyBytes_Check(typestr)) {
                dtype =_array_typedescr_fromstr(PyBytes_AS_STRING(typestr));
#if defined(NPY_PY3K)
                if (tmp == typestr) {
                    Py_DECREF(tmp);
                }
#endif
                Py_DECREF(ip);
                if (dtype == NULL) {
                    goto fail;
                }
                goto promote_types;
            }
        }
        Py_DECREF(ip);
    }

    /* The array struct interface */
    ip = PyArray_GetAttrString_SuppressException(obj, "__array_struct__");
    if (ip != NULL) {
        PyArrayInterface *inter;
        char buf[40];

        if (NpyCapsule_Check(ip)) {
            inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(ip);
            if (inter->two == 2) {
                PyOS_snprintf(buf, sizeof(buf),
                        "|%c%d", inter->typekind, inter->itemsize);
                dtype = _array_typedescr_fromstr(buf);
                Py_DECREF(ip);
                if (dtype == NULL) {
                    goto fail;
                }
                goto promote_types;
            }
        }
        Py_DECREF(ip);
    }

    /* The old buffer interface */
#if !defined(NPY_PY3K)
    if (PyBuffer_Check(obj)) {
        dtype = PyArray_DescrNewFromType(NPY_VOID);
        if (dtype == NULL) {
            goto fail;
        }
        dtype->elsize = Py_TYPE(obj)->tp_as_sequence->sq_length(obj);
        PyErr_Clear();
        goto promote_types;
    }
#endif

    /* The __array__ attribute */
    ip = PyArray_GetAttrString_SuppressException(obj, "__array__");
    if (ip != NULL) {
        Py_DECREF(ip);
        ip = PyObject_CallMethod(obj, "__array__", NULL);
        if(ip && PyArray_Check(ip)) {
            dtype = PyArray_DESCR((PyArrayObject *)ip);
            Py_INCREF(dtype);
            Py_DECREF(ip);
            goto promote_types;
        }
        Py_XDECREF(ip);
        if (PyErr_Occurred()) {
            goto fail;
        }
    }

    /* Not exactly sure what this is about... */
#if !defined(NPY_PY3K)
    if (PyInstance_Check(obj)) {
        dtype = _use_default_type(obj);
        if (dtype == NULL) {
            goto fail;
        }
        else {
            goto promote_types;
        }
    }
#endif

    /*
     * If we reached the maximum recursion depth without hitting one
     * of the above cases, the output dtype should be OBJECT
     */
    if (maxdims == 0 || !PySequence_Check(obj)) {
        if (*out_dtype == NULL || (*out_dtype)->type_num != NPY_OBJECT) {
            Py_XDECREF(*out_dtype);
            *out_dtype = PyArray_DescrFromType(NPY_OBJECT);
            if (*out_dtype == NULL) {
                return -1;
            }
        }
        return 0;
    }

    /* Recursive case */
    size = PySequence_Size(obj);
    if (size < 0) {
        goto fail;
    }
    /* Recursive call for each sequence item */
    for (i = 0; i < size; ++i) {
        int res;
        ip = PySequence_GetItem(obj, i);
        if (ip == NULL) {
            goto fail;
        }
        res = PyArray_DTypeFromObjectHelper(ip, maxdims - 1,
                                            out_dtype, string_type);
        if (res < 0) {
            Py_DECREF(ip);
            goto fail;
        }
        else if (res > 0) {
            Py_DECREF(ip);
            return res;
        }
        Py_DECREF(ip);
    }

    return 0;


promote_types:
    /* Set 'out_dtype' if it's NULL */
    if (*out_dtype == NULL) {
        if (!string_type && dtype->type_num == NPY_STRING) {
            Py_DECREF(dtype);
            return RETRY_WITH_STRING;
        }
        if (!string_type && dtype->type_num == NPY_UNICODE) {
            Py_DECREF(dtype);
            return RETRY_WITH_UNICODE;
        }
        *out_dtype = dtype;
        return 0;
    }
    /* Do type promotion with 'out_dtype' */
    else {
        PyArray_Descr *res_dtype = PyArray_PromoteTypes(dtype, *out_dtype);
        Py_DECREF(dtype);
        if (res_dtype == NULL) {
            return -1;
        }
        if (!string_type &&
                res_dtype->type_num == NPY_UNICODE &&
                (*out_dtype)->type_num != NPY_UNICODE) {
            Py_DECREF(res_dtype);
            return RETRY_WITH_UNICODE;
        }
        if (!string_type &&
                res_dtype->type_num == NPY_STRING &&
                (*out_dtype)->type_num != NPY_STRING) {
            Py_DECREF(res_dtype);
            return RETRY_WITH_STRING;
        }
        Py_DECREF(*out_dtype);
        *out_dtype = res_dtype;
        return 0;
    }

fail:
    Py_XDECREF(*out_dtype);
    *out_dtype = NULL;
    return -1;
}

#undef RETRY_WITH_STRING
#undef RETRY_WITH_UNICODE

/* new reference */
NPY_NO_EXPORT PyArray_Descr *
_array_typedescr_fromstr(char *c_str)
{
    PyArray_Descr *descr = NULL;
    PyObject *stringobj = PyString_FromString(c_str);

    if (stringobj == NULL) {
        return NULL;
    }
    if (PyArray_DescrConverter(stringobj, &descr) != NPY_SUCCEED) {
        Py_DECREF(stringobj);
        return NULL;
    }
    Py_DECREF(stringobj);
    return descr;
}


NPY_NO_EXPORT char *
index2ptr(PyArrayObject *mp, npy_intp i)
{
    npy_intp dim0;

    if (PyArray_NDIM(mp) == 0) {
        PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed");
        return NULL;
    }
    dim0 = PyArray_DIMS(mp)[0];
    if (check_and_adjust_index(&i, dim0, 0) < 0)
        return NULL;
    if (i == 0) {
        return PyArray_DATA(mp);
    }
    return PyArray_BYTES(mp)+i*PyArray_STRIDES(mp)[0];
}
Beispiel #12
0
int uwsgi_python_send_body(struct wsgi_request *wsgi_req, PyObject *chunk) {
	char *content = NULL;
	size_t content_len = 0;

#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER)
	Py_buffer pbuf;
	int has_buffer = 0;
#endif

	if (!up.wsgi_accept_buffer && !wsgi_req->is_raw) goto strict;

#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER)
	if (PyObject_CheckBuffer(chunk)) {
		if (!PyObject_GetBuffer(chunk, &pbuf, PyBUF_SIMPLE)) {
			content = (char *) pbuf.buf;
			content_len = (size_t) pbuf.len;
			has_buffer = 1;
			goto found;
		}
	}
#else
	if (PyObject_CheckReadBuffer(chunk)) {
#ifdef UWSGI_PYTHON_OLD
		int buffer_len = 0;
		if (!PyObject_AsCharBuffer(chunk, (const char **) &content, &buffer_len)) {
#else
		if (!PyObject_AsCharBuffer(chunk, (const char **) &content, (Py_ssize_t *) &content_len)) {
#endif
			PyErr_Clear();
			goto found;
		}
#ifdef UWSGI_PYTHON_OLD
		content_len = buffer_len;
#endif
	}
#endif

strict:
	// fallback
	if (PyString_Check(chunk)) {
               	content = PyString_AsString(chunk);
               	content_len = PyString_Size(chunk);
	}

found:
	if (content) {
                UWSGI_RELEASE_GIL
                uwsgi_response_write_body_do(wsgi_req, content, content_len);
                UWSGI_GET_GIL
#if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER)
		if (has_buffer) PyBuffer_Release(&pbuf);
#endif
                uwsgi_py_check_write_errors {
                       	uwsgi_py_write_exception(wsgi_req);
			return -1;
                }
		return 1;
	}
	return 0;
} 

/*
this is a hack for supporting non-file object passed to wsgi.file_wrapper
*/
static void uwsgi_python_consume_file_wrapper_read(struct wsgi_request *wsgi_req, PyObject *pychunk) {
	PyObject *read_method_args = NULL;
	PyObject *read_method = PyObject_GetAttrString(pychunk, "read");
	if (wsgi_req->sendfile_fd_chunk > 0) {
        	read_method_args = PyTuple_New(1);
		PyTuple_SetItem(read_method_args, 0, PyInt_FromLong(wsgi_req->sendfile_fd_chunk));
	}
	else {
        	read_method_args = PyTuple_New(0);
	}
	for(;;) {
        	PyObject *read_method_output = PyEval_CallObject(read_method, read_method_args);
                if (PyErr_Occurred()) {
                	uwsgi_manage_exception(wsgi_req, 0);
			break;
                }
		if (!read_method_output) break;
               	if (PyString_Check(read_method_output)) {
                     	char *content = PyString_AsString(read_method_output);
                        size_t content_len = PyString_Size(read_method_output);
			if (content_len == 0) {
                        	Py_DECREF(read_method_output);
				break;
			}
                        UWSGI_RELEASE_GIL
                        uwsgi_response_write_body_do(wsgi_req, content, content_len);
                        UWSGI_GET_GIL
                }
		Py_DECREF(read_method_output);
		if (wsgi_req->sendfile_fd_chunk == 0) break;
	}
        Py_DECREF(read_method_args);
        Py_DECREF(read_method);
}
	PyBufferWrapper(PyObject *const data, const uint32_t count, const bool writable)
	:
		m_count(count),
		m_length(UINT32_MAX)
	{
		memset(m_buffers, 0, sizeof(double*) * MAX_BUFFERS);
		memset(m_views, 0, sizeof(Py_buffer) * MAX_BUFFERS);

		if (!((data) && PyTuple_Check(data)))
		{
			PY_EXCEPTION("Input Python object does not appear to be a tuple!");
			throw std::invalid_argument("Invalid buffer object!");
		}
		if (!((count > 0) && (count <= MAX_BUFFERS) && (static_cast<uint32_t>(PyTuple_Size(data)) >= count)))
		{
			PY_EXCEPTION("Input tuple does not have the required minimum size!");
			throw std::invalid_argument("Invalid buffer count!");
		}

		PyObject *pyArrays[MAX_BUFFERS];
		for (uint32_t c = 0; c < count; ++c)
		{
			if (!(pyArrays[c] = PyTuple_GetItem(data, static_cast<Py_ssize_t>(c))))
			{
				PY_EXCEPTION("Failed to obtain the n-th element of the given input tuple!");
				throw std::invalid_argument("PyTuple_GetItem() error!");
			}
			if (PyObject_IsInstance(pyArrays[c], g_arrayClass) <= 0)
			{
				PY_EXCEPTION("The n-th element of the given tuple is not an array object!");
				throw std::invalid_argument("PyObject_IsInstance() error!");
			}
		}

		for (uint32_t c = 0; c < count; ++c)
		{
			if (!PyObject_CheckBuffer(pyArrays[c]))
			{
				PY_EXCEPTION("The provided array object does not support the 'buffer' protocol!");
				releaseBuffers();
				throw std::invalid_argument("PyObject_CheckBuffer() error!");
			}
			if (PyObject_GetBuffer(pyArrays[c], &m_views[c], (writable ? PyBUF_WRITABLE : 0)) < 0)
			{
				PY_EXCEPTION("Failed to get the internal buffer from array object!");
				releaseBuffers();
				throw std::invalid_argument("PyObject_GetBuffer() error!");
			}
			if (m_views[c].itemsize != sizeof(double))
			{
				PY_EXCEPTION("The provided array has an invalid item size! Not 'double' array?");
				releaseBuffers();
				throw std::invalid_argument("Py_buffer.itemsize is invalid!");
			}
			if ((m_length = std::min(m_length, static_cast<uint32_t>(m_views[c].len / m_views[c].itemsize))) < 1)
			{
				PY_EXCEPTION("The size of the provided buffers is way too small!");
				releaseBuffers();
				throw std::invalid_argument("Py_buffer.len is invalid!");
			}
			if (!(m_buffers[c] = reinterpret_cast<double*>(m_views[c].buf)))
			{
				PY_EXCEPTION("Pointer to the arrays's buffer appears to be NULL!");
				releaseBuffers();
				throw std::invalid_argument("Py_buffer.buf is invalid!");
			}
		}
	}
Beispiel #14
0
static PyObject *
get_buf( PyObject *self, PyObject *args ) {
    (void)self;

    PyObject *py_chan, *b;
    int wait, last;
    // get arg objects
    if( !PyArg_ParseTuple(args, "OOii", &py_chan, &b, &wait, &last) ) {
        return NULL;
    }

    // parse channel
    ach_channel_t *c = parse_channel_pointer(py_chan);
    if( NULL == c ) {
        return NULL;
    }

    // parse buffer
    if( ! PyObject_CheckBuffer(b) ) {
        PyErr_SetString( PyExc_TypeError, "invalid buffer" );
        return NULL;
    }

    // view buffer
    Py_buffer buf;
    if( PyObject_GetBuffer( b, &buf, PyBUF_WRITABLE ) ) {
        PyErr_SetString( PyExc_BufferError, "couldn't view writable buffer" );
        return NULL;
    }

    // parse opts
    int opts = 0;
    if (wait) opts |= ACH_O_WAIT;
    if (last) opts |= ACH_O_LAST;

    // make the damn call
    size_t frame_size;
    ach_status_t r = ach_get( c, buf.buf, (size_t)buf.len,
                              &frame_size, NULL, opts );
    // cleanup
    PyBuffer_Release(&buf);

    // return
    switch(r) {
    case ACH_OK:
    case ACH_STALE_FRAMES:
    case ACH_MISSED_FRAME:
    case ACH_TIMEOUT:
    case ACH_CANCELED:
        return Py_BuildValue("ik", r, frame_size);

    case ACH_OVERFLOW:
    case ACH_INVALID_NAME:
    case ACH_BAD_SHM_FILE:
    case ACH_FAILED_SYSCALL:
    case ACH_CLOSED:
    case ACH_EEXIST:
    case ACH_ENOENT:
    case ACH_BUG:
    case ACH_EINVAL:
    case ACH_CORRUPT:
    case ACH_BAD_HEADER:
    case ACH_EACCES:
    case ACH_EINTR:
    case ACH_EFAULT:
    case ACH_ENOTSUP:
        return raise_error(r);
    }

    return NULL;

}
Beispiel #15
0
NPY_NO_EXPORT int
PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
                              PyArray_Descr **out_dtype, int string_type)
{
    int i, size;
    PyArray_Descr *dtype = NULL;
    PyObject *ip;
    Py_buffer buffer_view;
    /* types for sequence handling */
    PyObject ** objects;
    PyObject * seq;
    PyTypeObject * common_type;

    /* Check if it's an ndarray */
    if (PyArray_Check(obj)) {
        dtype = PyArray_DESCR((PyArrayObject *)obj);
        Py_INCREF(dtype);
        goto promote_types;
    }

    /* See if it's a python None */
    if (obj == Py_None) {
        dtype = PyArray_DescrFromType(NPY_OBJECT);
        if (dtype == NULL) {
            goto fail;
        }
        Py_INCREF(dtype);
        goto promote_types;
    }
    /* Check if it's a NumPy scalar */
    else if (PyArray_IsScalar(obj, Generic)) {
        if (!string_type) {
            dtype = PyArray_DescrFromScalar(obj);
            if (dtype == NULL) {
                goto fail;
            }
        }
        else {
            int itemsize;
            PyObject *temp;

            if (string_type == NPY_STRING) {
                if ((temp = PyObject_Str(obj)) == NULL) {
                    return -1;
                }
#if defined(NPY_PY3K)
    #if PY_VERSION_HEX >= 0x03030000
                itemsize = PyUnicode_GetLength(temp);
    #else
                itemsize = PyUnicode_GET_SIZE(temp);
    #endif
#else
                itemsize = PyString_GET_SIZE(temp);
#endif
            }
            else if (string_type == NPY_UNICODE) {
#if defined(NPY_PY3K)
                if ((temp = PyObject_Str(obj)) == NULL) {
#else
                if ((temp = PyObject_Unicode(obj)) == NULL) {
#endif
                    return -1;
                }
                itemsize = PyUnicode_GET_DATA_SIZE(temp);
#ifndef Py_UNICODE_WIDE
                itemsize <<= 1;
#endif
            }
            else {
                goto fail;
            }
            Py_DECREF(temp);
            if (*out_dtype != NULL &&
                    (*out_dtype)->type_num == string_type &&
                    (*out_dtype)->elsize >= itemsize) {
                return 0;
            }
            dtype = PyArray_DescrNewFromType(string_type);
            if (dtype == NULL) {
                goto fail;
            }
            dtype->elsize = itemsize;
        }
        goto promote_types;
    }

    /* Check if it's a Python scalar */
    dtype = _array_find_python_scalar_type(obj);
    if (dtype != NULL) {
        if (string_type) {
            int itemsize;
            PyObject *temp;

            if (string_type == NPY_STRING) {
                if ((temp = PyObject_Str(obj)) == NULL) {
                    return -1;
                }
#if defined(NPY_PY3K)
    #if PY_VERSION_HEX >= 0x03030000
                itemsize = PyUnicode_GetLength(temp);
    #else
                itemsize = PyUnicode_GET_SIZE(temp);
    #endif
#else
                itemsize = PyString_GET_SIZE(temp);
#endif
            }
            else if (string_type == NPY_UNICODE) {
#if defined(NPY_PY3K)
                if ((temp = PyObject_Str(obj)) == NULL) {
#else
                if ((temp = PyObject_Unicode(obj)) == NULL) {
#endif
                    return -1;
                }
                itemsize = PyUnicode_GET_DATA_SIZE(temp);
#ifndef Py_UNICODE_WIDE
                itemsize <<= 1;
#endif
            }
            else {
                goto fail;
            }
            Py_DECREF(temp);
            if (*out_dtype != NULL &&
                    (*out_dtype)->type_num == string_type &&
                    (*out_dtype)->elsize >= itemsize) {
                return 0;
            }
            dtype = PyArray_DescrNewFromType(string_type);
            if (dtype == NULL) {
                goto fail;
            }
            dtype->elsize = itemsize;
        }
        goto promote_types;
    }

    /* Check if it's an ASCII string */
    if (PyBytes_Check(obj)) {
        int itemsize = PyString_GET_SIZE(obj);

        /* If it's already a big enough string, don't bother type promoting */
        if (*out_dtype != NULL &&
                        (*out_dtype)->type_num == NPY_STRING &&
                        (*out_dtype)->elsize >= itemsize) {
            return 0;
        }
        dtype = PyArray_DescrNewFromType(NPY_STRING);
        if (dtype == NULL) {
            goto fail;
        }
        dtype->elsize = itemsize;
        goto promote_types;
    }

    /* Check if it's a Unicode string */
    if (PyUnicode_Check(obj)) {
        int itemsize = PyUnicode_GET_DATA_SIZE(obj);
#ifndef Py_UNICODE_WIDE
        itemsize <<= 1;
#endif

        /*
         * If it's already a big enough unicode object,
         * don't bother type promoting
         */
        if (*out_dtype != NULL &&
                        (*out_dtype)->type_num == NPY_UNICODE &&
                        (*out_dtype)->elsize >= itemsize) {
            return 0;
        }
        dtype = PyArray_DescrNewFromType(NPY_UNICODE);
        if (dtype == NULL) {
            goto fail;
        }
        dtype->elsize = itemsize;
        goto promote_types;
    }

    /* PEP 3118 buffer interface */
    if (PyObject_CheckBuffer(obj) == 1) {
        memset(&buffer_view, 0, sizeof(Py_buffer));
        if (PyObject_GetBuffer(obj, &buffer_view,
                               PyBUF_FORMAT|PyBUF_STRIDES) == 0 ||
            PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT) == 0) {

            PyErr_Clear();
            dtype = _descriptor_from_pep3118_format(buffer_view.format);
            PyBuffer_Release(&buffer_view);
            if (dtype) {
                goto promote_types;
            }
        }
        else if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_STRIDES) == 0 ||
                 PyObject_GetBuffer(obj, &buffer_view, PyBUF_SIMPLE) == 0) {

            PyErr_Clear();
            dtype = PyArray_DescrNewFromType(NPY_VOID);
            dtype->elsize = buffer_view.itemsize;
            PyBuffer_Release(&buffer_view);
            goto promote_types;
        }
        else {
            PyErr_Clear();
        }
    }

    /* The array interface */
    ip = PyArray_LookupSpecial_OnInstance(obj, "__array_interface__");
    if (ip != NULL) {
        if (PyDict_Check(ip)) {
            PyObject *typestr;
#if defined(NPY_PY3K)
            PyObject *tmp = NULL;
#endif
            typestr = PyDict_GetItemString(ip, "typestr");
#if defined(NPY_PY3K)
            /* Allow unicode type strings */
            if (PyUnicode_Check(typestr)) {
                tmp = PyUnicode_AsASCIIString(typestr);
                typestr = tmp;
            }
#endif
            if (typestr && PyBytes_Check(typestr)) {
                dtype =_array_typedescr_fromstr(PyBytes_AS_STRING(typestr));
#if defined(NPY_PY3K)
                if (tmp == typestr) {
                    Py_DECREF(tmp);
                }
#endif
                Py_DECREF(ip);
                if (dtype == NULL) {
                    goto fail;
                }
                goto promote_types;
            }
        }
        Py_DECREF(ip);
    }

    /* The array struct interface */
    ip = PyArray_LookupSpecial_OnInstance(obj, "__array_struct__");
    if (ip != NULL) {
        PyArrayInterface *inter;
        char buf[40];

        if (NpyCapsule_Check(ip)) {
            inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(ip);
            if (inter->two == 2) {
                PyOS_snprintf(buf, sizeof(buf),
                        "|%c%d", inter->typekind, inter->itemsize);
                dtype = _array_typedescr_fromstr(buf);
                Py_DECREF(ip);
                if (dtype == NULL) {
                    goto fail;
                }
                goto promote_types;
            }
        }
        Py_DECREF(ip);
    }

    /* The old buffer interface */
#if !defined(NPY_PY3K)
    if (PyBuffer_Check(obj)) {
        dtype = PyArray_DescrNewFromType(NPY_VOID);
        if (dtype == NULL) {
            goto fail;
        }
        dtype->elsize = Py_TYPE(obj)->tp_as_sequence->sq_length(obj);
        PyErr_Clear();
        goto promote_types;
    }
#endif

    /* The __array__ attribute */
    ip = PyArray_LookupSpecial_OnInstance(obj, "__array__");
    if (ip != NULL) {
        Py_DECREF(ip);
        ip = PyObject_CallMethod(obj, "__array__", NULL);
        if(ip && PyArray_Check(ip)) {
            dtype = PyArray_DESCR((PyArrayObject *)ip);
            Py_INCREF(dtype);
            Py_DECREF(ip);
            goto promote_types;
        }
        Py_XDECREF(ip);
        if (PyErr_Occurred()) {
            goto fail;
        }
    }

    /*
     * If we reached the maximum recursion depth without hitting one
     * of the above cases, and obj isn't a sequence-like object, the output
     * dtype should be either OBJECT or a user-defined type.
     *
     * Note that some libraries define sequence-like classes but want them to
     * be treated as objects, and they expect numpy to treat it as an object if
     * __len__ is not defined.
     */
    if (maxdims == 0 || !PySequence_Check(obj) || PySequence_Size(obj) < 0) {
        // clear any PySequence_Size error, which corrupts further calls to it
        PyErr_Clear();

        if (*out_dtype == NULL || (*out_dtype)->type_num != NPY_OBJECT) {
            Py_XDECREF(*out_dtype);
            *out_dtype = PyArray_DescrFromType(NPY_OBJECT);
            if (*out_dtype == NULL) {
                return -1;
            }
        }
        return 0;
    }

    /* Recursive case, first check the sequence contains only one type */
    seq = PySequence_Fast(obj, "Could not convert object to sequence");
    if (seq == NULL) {
        goto fail;
    }
    size = PySequence_Fast_GET_SIZE(seq);
    objects = PySequence_Fast_ITEMS(seq);
    common_type = size > 0 ? Py_TYPE(objects[0]) : NULL;
    for (i = 1; i < size; ++i) {
        if (Py_TYPE(objects[i]) != common_type) {
            common_type = NULL;
            break;
        }
    }

    /* all types are the same and scalar, one recursive call is enough */
    if (common_type != NULL && !string_type &&
            (common_type == &PyFloat_Type ||
/* TODO: we could add longs if we add a range check */
#if !defined(NPY_PY3K)
             common_type == &PyInt_Type ||
#endif
             common_type == &PyBool_Type ||
             common_type == &PyComplex_Type)) {
        size = 1;
    }

    /* Recursive call for each sequence item */
    for (i = 0; i < size; ++i) {
        int res = PyArray_DTypeFromObjectHelper(objects[i], maxdims - 1,
                                                out_dtype, string_type);
        if (res < 0) {
            Py_DECREF(seq);
            goto fail;
        }
        else if (res > 0) {
            Py_DECREF(seq);
            return res;
        }
    }

    Py_DECREF(seq);

    return 0;


promote_types:
    /* Set 'out_dtype' if it's NULL */
    if (*out_dtype == NULL) {
        if (!string_type && dtype->type_num == NPY_STRING) {
            Py_DECREF(dtype);
            return RETRY_WITH_STRING;
        }
        if (!string_type && dtype->type_num == NPY_UNICODE) {
            Py_DECREF(dtype);
            return RETRY_WITH_UNICODE;
        }
        *out_dtype = dtype;
        return 0;
    }
    /* Do type promotion with 'out_dtype' */
    else {
        PyArray_Descr *res_dtype = PyArray_PromoteTypes(dtype, *out_dtype);
        Py_DECREF(dtype);
        if (res_dtype == NULL) {
            return -1;
        }
        if (!string_type &&
                res_dtype->type_num == NPY_UNICODE &&
                (*out_dtype)->type_num != NPY_UNICODE) {
            Py_DECREF(res_dtype);
            return RETRY_WITH_UNICODE;
        }
        if (!string_type &&
                res_dtype->type_num == NPY_STRING &&
                (*out_dtype)->type_num != NPY_STRING) {
            Py_DECREF(res_dtype);
            return RETRY_WITH_STRING;
        }
        Py_DECREF(*out_dtype);
        *out_dtype = res_dtype;
        return 0;
    }

fail:
    Py_XDECREF(*out_dtype);
    *out_dtype = NULL;
    return -1;
}

#undef RETRY_WITH_STRING
#undef RETRY_WITH_UNICODE

/* new reference */
NPY_NO_EXPORT PyArray_Descr *
_array_typedescr_fromstr(char *c_str)
{
    PyArray_Descr *descr = NULL;
    PyObject *stringobj = PyString_FromString(c_str);

    if (stringobj == NULL) {
        return NULL;
    }
    if (PyArray_DescrConverter(stringobj, &descr) != NPY_SUCCEED) {
        Py_DECREF(stringobj);
        return NULL;
    }
    Py_DECREF(stringobj);
    return descr;
}


NPY_NO_EXPORT char *
index2ptr(PyArrayObject *mp, npy_intp i)
{
    npy_intp dim0;

    if (PyArray_NDIM(mp) == 0) {
        PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed");
        return NULL;
    }
    dim0 = PyArray_DIMS(mp)[0];
    if (check_and_adjust_index(&i, dim0, 0, NULL) < 0)
        return NULL;
    if (i == 0) {
        return PyArray_DATA(mp);
    }
    return PyArray_BYTES(mp)+i*PyArray_STRIDES(mp)[0];
}
Beispiel #16
0
int main(int argc, char **argv)
{
	Py_Initialize();

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("import os");
	PyRun_SimpleString("import time");
	PyRun_SimpleString("import io");
	PyRun_SimpleString("import socket");
	//PyRun_SimpleString("print(os.getcwd())");
	PyRun_SimpleString("sys.path.append(os.getcwd())");
	PyRun_SimpleString("import buildtcp");

	//int ret =  PyRun_SimpleFile(fp, "./buildtcp.py");
	PyObject *main_module = PyImport_AddModule("__main__");
	PyObject *target = PyImport_AddModule("buildtcp");
	if(!main_module) {
		printf("failed to PyImport_AddModule()\n");
		return 0;
	}

	PyObject *g_dict = PyModule_GetDict(main_module);
	PyObject *l_dict = PyDict_New();
	if(!l_dict) {
		printf("failed to PyDict_New()\n");
		return 0;
	}

	FILE *fp = fopen("./buildtcp.py", "r");
	//PyObject *res = PyRun_File(fp, "./buildtcp.py", Py_file_input, g_dict, l_dict);
	//PyObject *res = PyRun_File(fp, "./buildtcp.py", Py_file_input, g_dict, g_dict);
	//PyObject *res = PyRun_String("buildtcp.buildtcp()", Py_file_input, g_dict, l_dict);
	printf("PyDict_Size(g_dict):%d\n", PyDict_Size(g_dict));
	print_dict(g_dict);
	printf("PyDict_Size(l_dict):%d\n", PyDict_Size(l_dict));
	print_dict(l_dict);
	//PyObject *key = PyUnicode_FromString("buildtcp");
	//PyObject *func = PyDict_GetItem(l_dict, key);
	//PyObject* f_str_exc_type = PyObject_Repr(func);
	//PyObject* f_pyStr = PyUnicode_AsEncodedString(f_str_exc_type, "utf-8", "Error ~");
	//printf("value:%s\n", PyBytes_AsString(f_pyStr));
	//res = PyObject_Call(func, PyTuple_New(), NULL);
	//res = PyObject_CallObject(func, NULL);
	PyObject *res = PyObject_CallMethodObjArgs(target, PyUnicode_FromString("buildtcp"), NULL);
	if(!res) {
		printf("failed to PyRun_File()\n");
		return 0;
	}

	if(!PyObject_CheckBuffer(res)) {
		printf("res is not Py_buffer\n");
		return 0;
	}

	Py_buffer buf;
	int ret = PyObject_GetBuffer(res, &buf, PyBUF_SIMPLE);
	if(ret == -1) {
		printf("failed to PyObject_GetBuffer()\n");
		return 0;
	}
	hexdump("return val", buf.buf, buf.len);

	fclose(fp);
	Py_Finalize();

	return 0;
}