Example #1
0
PyObject *
string_count(PyStringObject *self, PyObject *args)
{
    PyObject *sub_obj;
    const char *str = PyString_AS_STRING(self), *sub;
    Py_ssize_t sub_len;
    Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;

    if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end))
        return NULL;

    if (PyString_Check(sub_obj)) {
        sub = PyString_AS_STRING(sub_obj);
        sub_len = PyString_GET_SIZE(sub_obj);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(sub_obj)) {
        Py_ssize_t count;
        count = PyUnicode_Count((PyObject *)self, sub_obj, start, end);
        if (count == -1)
            return NULL;
        else
            return PyInt_FromSsize_t(count);
    }
#endif
    else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
        return NULL;

    ADJUST_INDICES(start, end, PyString_GET_SIZE(self));

    return PyInt_FromSsize_t(
        stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
        );
}
Example #2
0
PyObject* string_rsplit(PyStringObject* self, PyObject* args) {
    Py_ssize_t len = PyString_GET_SIZE(self), n;
    Py_ssize_t maxsplit = -1;
    const char* s = PyString_AS_STRING(self), *sub;
    PyObject* subobj = Py_None;

    if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
        return NULL;
    if (maxsplit < 0)
        maxsplit = PY_SSIZE_T_MAX;
    if (subobj == Py_None)
        return stringlib_rsplit_whitespace((PyObject*)self, s, len, maxsplit);
    if (PyString_Check(subobj)) {
        sub = PyString_AS_STRING(subobj);
        n = PyString_GET_SIZE(subobj);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(subobj))
        return PyUnicode_RSplit((PyObject*)self, subobj, maxsplit);
#endif
    else if (PyObject_AsCharBuffer(subobj, &sub, &n))
        return NULL;

    return stringlib_rsplit((PyObject*)self, s, len, sub, n, maxsplit);
}
Example #3
0
PyObject *
PyNumber_Int(PyObject *o)
{
	PyNumberMethods *m;
	const char *buffer;
	int buffer_len;

	if (o == NULL)
		return null_error();
	if (PyInt_Check(o)) {
		Py_INCREF(o);
		return o;
	}
	if (PyString_Check(o))
		return int_from_string(PyString_AS_STRING(o), 
				       PyString_GET_SIZE(o));
	if (PyUnicode_Check(o))
		return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
					 PyUnicode_GET_SIZE(o),
					 10);
	m = o->ob_type->tp_as_number;
	if (m && m->nb_int)
		return m->nb_int(o);
	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
		return int_from_string((char*)buffer, buffer_len);

	return type_error("object can't be converted to int");
}
Example #4
0
string_find_internal(PyStringObject *self, PyObject *args, int dir)
{
    PyObject *subobj;
    const char *sub;
    Py_ssize_t sub_len;
    Py_ssize_t start=0, end=PY_SSIZE_T_MAX;

    if (!stringlib_parse_args_finds("find/rfind/index/rindex",
                                    args, &subobj, &start, &end))
        return -2;

    if (PyString_Check(subobj)) {
        sub = PyString_AS_STRING(subobj);
        sub_len = PyString_GET_SIZE(subobj);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(subobj))
        return PyUnicode_Find(
            (PyObject *)self, subobj, start, end, dir);
#endif
    else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
        /* XXX - the "expected a character buffer object" is pretty
           confusing for a non-expert.  remap to something else ? */
        return -2;

    if (dir > 0)
        return stringlib_find_slice(
            PyString_AS_STRING(self), PyString_GET_SIZE(self),
            sub, sub_len, start, end);
    else
        return stringlib_rfind_slice(
            PyString_AS_STRING(self), PyString_GET_SIZE(self),
            sub, sub_len, start, end);
}
Example #5
0
PyObject *
PyNumber_Long(PyObject *o)
{
	PyNumberMethods *m;
	const char *buffer;
	int buffer_len;

	if (o == NULL)
		return null_error();
	if (PyLong_Check(o)) {
		Py_INCREF(o);
		return o;
	}
	if (PyString_Check(o))
		/* need to do extra error checking that PyLong_FromString() 
		 * doesn't do.  In particular long('9.5') must raise an
		 * exception, not truncate the float.
		 */
		return long_from_string(PyString_AS_STRING(o),
					PyString_GET_SIZE(o));
	if (PyUnicode_Check(o))
		/* The above check is done in PyLong_FromUnicode(). */
		return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
					  PyUnicode_GET_SIZE(o),
					  10);
	m = o->ob_type->tp_as_number;
	if (m && m->nb_long)
		return m->nb_long(o);
	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
		return long_from_string(buffer, buffer_len);

	return type_error("object can't be converted to long");
}
Example #6
0
	static PyObject *nodes_getitem(PyObject *self,PyObject *attr){
		Py_ssize_t size;
		const char *str;
		PyObject_AsCharBuffer(attr, &str, &size);
		std::string s{str};
		//wcstombs( str8, str, wcslen(str) );
		PyMem_Free((void*)str);
		return ab_manager_resolve(self, s.c_str());
	}
Example #7
0
// Pyston change: don't use varags calling convention
// PyObject* string_replace(PyStringObject *self, PyObject *args)
PyObject* string_replace(PyStringObject *self, PyObject *from, PyObject* to, PyObject** args)
{
    PyObject* _count = args[0];
    Py_ssize_t count = -1;
    const char *from_s, *to_s;
    Py_ssize_t from_len, to_len;

    // Pyston change: don't use varags calling convention
    // if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
    //    return NULL;
    if (_count && !PyArg_ParseSingle(_count, 3, "replace", "n", &count))
        return NULL;

    if (PyString_Check(from)) {
        from_s = PyString_AS_STRING(from);
        from_len = PyString_GET_SIZE(from);
    }
#ifdef Py_USING_UNICODE
    if (PyUnicode_Check(from))
        return PyUnicode_Replace((PyObject *)self,
                                 from, to, count);
#endif
    else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
        return NULL;

    if (PyString_Check(to)) {
        to_s = PyString_AS_STRING(to);
        to_len = PyString_GET_SIZE(to);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(to))
        return PyUnicode_Replace((PyObject *)self,
                                 from, to, count);
#endif
    else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
        return NULL;

    return (PyObject *)replace((PyStringObject *) self,
                               from_s, from_len,
                               to_s, to_len, count);
}
Example #8
0
int uwsgi_response_subhandler_wsgi(struct wsgi_request *wsgi_req) {

	PyObject *pychunk = NULL;
	const char* chunk = NULL;
	Py_ssize_t chunk_size = 0;

	// return or yield ?
	PyObject *async_result = wsgi_req->async_result;

	if (PyObject_AsCharBuffer(async_result,	&chunk, &chunk_size)<0) {
		PyErr_Clear();

		// is one element list
		if (PyList_Check(async_result) &&
			PyList_GET_SIZE(async_result)==1) {
			PyObject *elem = PyList_GET_ITEM(async_result, 0);
			if (PyObject_AsCharBuffer(elem, &chunk, &chunk_size)<0) {
				PyErr_Clear();
			}
		}
	}

	if(chunk) {
		UWSGI_RELEASE_GIL
		uwsgi_response_write_body_do(wsgi_req, (char*)chunk, chunk_size);
		UWSGI_GET_GIL
		uwsgi_py_check_write_errors {
			uwsgi_py_write_exception(wsgi_req);
		}
		goto clear;
	}

	if (wsgi_req->sendfile_obj == async_result) {
		if (wsgi_req->sendfile_fd >= 0) {
			UWSGI_RELEASE_GIL
			uwsgi_response_sendfile_do(wsgi_req, wsgi_req->sendfile_fd, 0, 0);
			UWSGI_GET_GIL
		}
		// we do not have an iterable, check for read() method
		else if (PyObject_HasAttrString(async_result, "read")) {
Example #9
0
static PyObject *
patches(PyObject *self, PyObject *args)
{
	PyObject *text, *bins, *result;
	struct flist *patch;
	const char *in;
	char *out;
	Py_ssize_t len, outlen, inlen;

	if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
		return NULL;

	len = PyList_Size(bins);
	if (!len) {
		/* nothing to do */
		Py_INCREF(text);
		return text;
	}

	if (PyObject_AsCharBuffer(text, &in, &inlen))
		return NULL;

	patch = fold(bins, 0, len);
	if (!patch)
		return NULL;

	outlen = calcsize(inlen, patch);
	if (outlen < 0) {
		result = NULL;
		goto cleanup;
	}
	result = PyBytes_FromStringAndSize(NULL, outlen);
	if (!result) {
		result = NULL;
		goto cleanup;
	}
	out = PyBytes_AsString(result);
	if (!apply(out, in, inlen, patch)) {
		Py_DECREF(result);
		result = NULL;
	}
cleanup:
	lfree(patch);
	return result;
}
Example #10
0
static PyObject *
newIobject(PyObject *s) {
  Iobject *self;
  char *buf;
  Py_ssize_t size;

  if (PyObject_AsCharBuffer(s, (const char **)&buf, &size) != 0)
      return NULL;

  self = PyObject_New(Iobject, &Itype);
  if (!self) return NULL;
  Py_INCREF(s);
  self->buf=buf;
  self->string_size=size;
  self->pbuf=s;
  self->pos=0;
  
  return (PyObject*)self;
}
Example #11
0
static PyObject* splitbuf_iternext(PyObject *self)
{
    splitbuf_state *s = (splitbuf_state *)self;
    PyObject *bufused;
    PyObject *bufpeekobj;
    const unsigned char *bufpeekbytes;
    Py_ssize_t bufpeeklen;
    int ofs, bits;
    int level;

    printf("ITERATION\n");
    bufused = PyObject_CallMethod(s->bufobj, "used", NULL);
    if (bufused == NULL)
        return NULL;
    bufpeekobj = PyObject_CallMethod(s->bufobj, "peek", "O", bufused);
    Py_DECREF(bufused);
    // BUG IS HERE, BUFPEEKBYTES MAY NOT BE DEFINED
    if (bufpeekbytes == NULL)
        return NULL;
    if (PyObject_AsCharBuffer(
            bufpeekobj, ((const char **)&bufpeekbytes), &bufpeeklen) == -1)
        return NULL;

    int bits_ = -1;
    ofs = bupsplit_find_ofs(bufpeekbytes, bufpeeklen, &bits_);
    bits = bits_;

    Py_DECREF(bufpeekbytes);
    if (ofs) {
        PyObject *tmp = PyObject_CallMethod(s->bufobj, "eat", "i", ofs);
        if (tmp == NULL)
            return NULL;
        Py_DECREF(tmp);
        level = (bits - s->basebits) / 5;
        // THIS LINE BELOW TRIGGERS BUG
        //printf("(ignore me) %d\n", bits);
        memcpy(s->prevbuf, bufpeekbytes, ofs);
        return Py_BuildValue("i", level);
    }

    return NULL;
}
Example #12
0
/* recursively generate a patch of all bins between start and end */
static struct flist *fold(PyObject *bins, Py_ssize_t start, Py_ssize_t end)
{
	Py_ssize_t len, blen;
	const char *buffer;

	if (start + 1 == end) {
		/* trivial case, output a decoded list */
		PyObject *tmp = PyList_GetItem(bins, start);
		if (!tmp)
			return NULL;
		if (PyObject_AsCharBuffer(tmp, &buffer, &blen))
			return NULL;
		return decode(buffer, blen);
	}

	/* divide and conquer, memory management is elsewhere */
	len = (end - start) / 2;
	return combine(fold(bins, start, start + len),
		       fold(bins, start + len, end));
}
Example #13
0
	Object to_object(PyObject *obj){
		if (PyBool_Check(obj))
			return AB::to_object( obj == Py_True );
		if (PyFloat_Check(obj))
			return AB::to_object( PyFloat_AsDouble(obj) );
		if (PyLong_Check(obj))
			return AB::to_object( (int)PyLong_AsLong(obj) );
		if (PyInt_Check(obj))
			return AB::to_object( (int)PyInt_AsLong(obj) );
		if (PyUnicode_Check(obj)){
			Py_ssize_t size;
			const char *str;
			PyObject_AsCharBuffer(obj, &str, &size);
			std::string str8{str};
			PyMem_Free((void*)str);

			return AB::to_object( str8 );
		}
		
		throw(AB::object_not_convertible( obj->ob_type->tp_name, "Object"));
	}
Example #14
0
PyObject *
string_rpartition(PyStringObject *self, PyObject *sep_obj)
{
    const char *sep;
    Py_ssize_t sep_len;

    if (PyString_Check(sep_obj)) {
        sep = PyString_AS_STRING(sep_obj);
        sep_len = PyString_GET_SIZE(sep_obj);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(sep_obj))
        return PyUnicode_RPartition((PyObject *) self, sep_obj);
#endif
    else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
        return NULL;

    return stringlib_rpartition(
        (PyObject*) self,
        PyString_AS_STRING(self), PyString_GET_SIZE(self),
        sep_obj, sep, sep_len
        );
}
Example #15
0
int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
{
    int rc = SQLITE_OK;
    long longval;
#ifdef HAVE_LONG_LONG
    PY_LONG_LONG longlongval;
#endif
    const char* buffer;
    char* string;
    Py_ssize_t buflen;
    PyObject* stringval;

    if (parameter == Py_None) {
        rc = sqlite3_bind_null(self->st, pos);
    } else if (PyInt_Check(parameter)) {
        longval = PyInt_AsLong(parameter);
        rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
#ifdef HAVE_LONG_LONG
    } else if (PyLong_Check(parameter)) {
        longlongval = PyLong_AsLongLong(parameter);
        /* in the overflow error case, longlongval is -1, and an exception is set */
        rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
#endif
    } else if (PyFloat_Check(parameter)) {
        rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
    } else if (PyBuffer_Check(parameter)) {
        if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
            rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
        } else {
            PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
            rc = -1;
        }
    } else if PyString_Check(parameter) {
        string = PyString_AsString(parameter);
        rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
    } else if PyUnicode_Check(parameter) {
Example #16
0
PyCodeObject *
PyCode_New(int argcount, int nlocals, int stacksize, int flags,
		   PyObject *code, PyObject *consts, PyObject *names,
		   PyObject *varnames, PyObject *freevars, PyObject *cellvars,
		   PyObject *filename, PyObject *name, int firstlineno,
		   PyObject *lnotab)
{

//	Types:
//	PyObject *code			something supporting buffer
//	PyObject *consts		Tuple of PyObject
//	PyObject *names			Tuple of PyString
//	PyObject *varnames		Tuple of PyString
//	PyObject *freevars		Tuple of PyString
//	PyObject *cellvars		Tuple of PyString
//	PyObject *filename		PyString
//	PyObject *name			PyString
//	PyObject *lnotab		PyString

//	PyCodeObject *co;
	/* Check argument types */
	if (argcount < 0 || nlocals < 0 ||
		code == NULL ||
		consts == NULL || !PyTuple_Check(consts) ||
		names == NULL || !PyTuple_Check(names) ||
		varnames == NULL || !PyTuple_Check(varnames) ||
		freevars == NULL || !PyTuple_Check(freevars) ||
		cellvars == NULL || !PyTuple_Check(cellvars) ||
		name == NULL || !PyString_Check(name) ||
		filename == NULL || !PyString_Check(filename) ||
		lnotab == NULL || !PyString_Check(lnotab) ||
		!PyObject_CheckReadBuffer(code)) {
		PyErr_BadInternalCall();
		return NULL;
	}

	env(NULL);
	//Try to get code from a buffer into a jstring...
	char* code_cstr;
	Py_ssize_t i;
	jstring code_jstr;
	if (PyObject_AsCharBuffer(code, &code_cstr, &i) == 0)
	{
		char code_cstr0[i+1]; //code_cstr might not be null-terminated
		strcpy(code_cstr0, code_cstr);
		code_jstr = (*env)->NewStringUTF(env, code_cstr0);
	} else
	{
		PyErr_BadInternalCall();
		return NULL;
	}

//	jobject jConsts = NULL;
//	if (PyTuple_GET_SIZE(consts))
//	{
//		(*env)->NewObjectArray(env, PyTuple_GET_SIZE(consts), pyObjectClass, NULL);
//		for (i = 0; i < PyTuple_GET_SIZE(consts); ++i)
//			(*env)->SetObjectArrayElement(env, jConsts, i,
//				JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(consts, i)));
//	}
	pyTuple2jArray(consts, pyObjectClass, jConsts);

//	jobject jNames = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(names), pyStringClass, NULL);
//	for (i = 0; i < PyTuple_GET_SIZE(names); ++i)
//		(*env)->SetObjectArrayElement(env, jNames, i,
//			JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(names, i)));
	pyTuple2jArray(names, pyStringClass, jNames);

//	jobject jVarnames = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(varnames), pyStringClass, NULL);
//	for (i = 0; i < PyTuple_GET_SIZE(varnames); ++i)
//		(*env)->SetObjectArrayElement(env, jVarnames, i,
//			JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(varnames, i)));
	pyTuple2jArray(varnames, pyStringClass, jVarnames);

//	jobject jFreevars = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(freevars), pyStringClass, NULL);
//	for (i = 0; i < PyTuple_GET_SIZE(freevars); ++i)
//		(*env)->SetObjectArrayElement(env, jFreevars, i,
//			JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(freevars, i)));
	pyTuple2jArray(freevars, pyStringClass, jFreevars);

//	jobject jCellvars = (*env)->NewObjectArray(env, PyTuple_GET_SIZE(cellvars), pyStringClass, NULL);
//	for (i = 0; i < PyTuple_GET_SIZE(cellvars); ++i)
//		(*env)->SetObjectArrayElement(env, jCellvars, i,
//			JyNI_JythonPyObject_FromPyObject(PyTuple_GET_ITEM(cellvars, i)));
	pyTuple2jArray(cellvars, pyStringClass, jCellvars);

	jobject jFilename = JyNI_JythonPyObject_FromPyObject(filename);
	jobject jName = JyNI_JythonPyObject_FromPyObject(name);
	jobject jLnotab = JyNI_JythonPyObject_FromPyObject(lnotab);

	jobject result = (*env)->NewObject(env, pyBytecodeClass, pyBytecodeConstructor,
		argcount, nlocals, stacksize, flags, code_jstr, jConsts, jNames, jVarnames,
		jFilename, jName, firstlineno, jLnotab, jCellvars, jFreevars);

	return (PyCodeObject*) JyNI_PyObject_FromJythonPyObject(result);

//	intern_strings(names);
//	intern_strings(varnames);
//	intern_strings(freevars);
//	intern_strings(cellvars);
	/* Intern selected string constants */
//	for (i = PyTuple_Size(consts); --i >= 0; ) {
//		PyObject *v = PyTuple_GetItem(consts, i);
//		if (!PyString_Check(v))
//			continue;
//		if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
//			continue;
//		PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
//	}
//	co = PyObject_NEW(PyCodeObject, &PyCode_Type);
//	if (co != NULL) {
//		co->co_argcount = argcount;
//		co->co_nlocals = nlocals;
//		co->co_stacksize = stacksize;
//		co->co_flags = flags;
//		Py_INCREF(code);
//		co->co_code = code;
//		Py_INCREF(consts);
//		co->co_consts = consts;
//		Py_INCREF(names);
//		co->co_names = names;
//		Py_INCREF(varnames);
//		co->co_varnames = varnames;
//		Py_INCREF(freevars);
//		co->co_freevars = freevars;
//		Py_INCREF(cellvars);
//		co->co_cellvars = cellvars;
//		Py_INCREF(filename);
//		co->co_filename = filename;
//		Py_INCREF(name);
//		co->co_name = name;
//		co->co_firstlineno = firstlineno;
//		Py_INCREF(lnotab);
//		co->co_lnotab = lnotab;
//		co->co_zombieframe = NULL;
//		co->co_weakreflist = NULL;
//	}
//	return co;
}
Example #17
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);
}
Example #18
0
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
	const char *s, *start;
	char *end;
	double x=0.0, y=0.0, z;
	int got_re=0, got_im=0, got_bracket=0, done=0;
	int digit_or_dot;
	int sw_error=0;
	int sign;
	char buffer[256]; /* For errors */
#ifdef Py_USING_UNICODE
	char s_buffer[256];
#endif
	Py_ssize_t len;

	if (PyString_Check(v)) {
		s = PyString_AS_STRING(v);
		len = PyString_GET_SIZE(v);
	}
#ifdef Py_USING_UNICODE
	else if (PyUnicode_Check(v)) {
		if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
			PyErr_SetString(PyExc_ValueError,
				 "complex() literal too large to convert");
			return NULL;
		}
		if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
					    PyUnicode_GET_SIZE(v),
					    s_buffer,
					    NULL))
			return NULL;
		s = s_buffer;
		len = strlen(s);
	}
#endif
	else if (PyObject_AsCharBuffer(v, &s, &len)) {
		PyErr_SetString(PyExc_TypeError,
				"complex() arg is not a string");
		return NULL;
	}

	/* position on first nonblank */
	start = s;
	while (*s && isspace(Py_CHARMASK(*s)))
		s++;
	if (s[0] == '\0') {
		PyErr_SetString(PyExc_ValueError,
				"complex() arg is an empty string");
		return NULL;
	}
	if (s[0] == '(') {
		/* Skip over possible bracket from repr(). */
		got_bracket = 1;
		s++;
		while (*s && isspace(Py_CHARMASK(*s)))
			s++;
	}

	z = -1.0;
	sign = 1;
	do {

		switch (*s) {

		case '\0':
			if (s-start != len) {
				PyErr_SetString(
					PyExc_ValueError,
					"complex() arg contains a null byte");
				return NULL;
			}
			if(!done) sw_error=1;
			break;

		case ')':
			if (!got_bracket || !(got_re || got_im)) {
				sw_error=1;
				break;
			}
			got_bracket=0;
			done=1;
			s++;
			while (*s && isspace(Py_CHARMASK(*s)))
				s++;
			if (*s) sw_error=1;
			break;

		case '-':
			sign = -1;
				/* Fallthrough */
		case '+':
			if (done)  sw_error=1;
			s++;
			if  (  *s=='\0'||*s=='+'||*s=='-'||*s==')'||
			       isspace(Py_CHARMASK(*s))  )  sw_error=1;
			break;

		case 'J':
		case 'j':
			if (got_im || done) {
				sw_error = 1;
				break;
			}
			if  (z<0.0) {
				y=sign;
			}
			else{
				y=sign*z;
			}
			got_im=1;
			s++;
			if  (*s!='+' && *s!='-' )
				done=1;
			break;

		default:
			if (isspace(Py_CHARMASK(*s))) {
				while (*s && isspace(Py_CHARMASK(*s)))
					s++;
				if (*s && *s != ')')
					sw_error=1;
				else
					done = 1;
				break;
			}
			digit_or_dot =
				(*s=='.' || isdigit(Py_CHARMASK(*s)));
			if  (done||!digit_or_dot) {
				sw_error=1;
				break;
			}
			errno = 0;
			PyFPE_START_PROTECT("strtod", return 0)
				z = PyOS_ascii_strtod(s, &end) ;
			PyFPE_END_PROTECT(z)
				if (errno != 0) {
					PyOS_snprintf(buffer, sizeof(buffer),
					  "float() out of range: %.150s", s);
					PyErr_SetString(
						PyExc_ValueError,
						buffer);
					return NULL;
				}
			s=end;
			if  (*s=='J' || *s=='j') {

				break;
			}
			if  (got_re) {
				sw_error=1;
				break;
			}

				/* accept a real part */
			x=sign*z;
			got_re=1;
			if  (got_im)  done=1;
			z = -1.0;
			sign = 1;
			break;

		}  /* end of switch  */

	} while (s - start < len && !sw_error);

	if (sw_error || got_bracket) {
		PyErr_SetString(PyExc_ValueError,
				"complex() arg is a malformed string");
		return NULL;
	}

	return complex_subtype_from_doubles(type, x, y);
}
Example #19
0
/* This is a hacked version of Python's fileobject.c:file_writelines(). */
static PyObject *
fcgi_Stream_writelines(fcgi_Stream *self, PyObject *seq)
{
#define CHUNKSIZE 1000
    FCGX_Stream *s;
    PyObject *list, *line;
    PyObject *it;   /* iter(seq) */
    PyObject *result;
    int i, j, index, len, nwritten, islist;

    fcgi_Stream_Check();

    s = *(self->s);

    result = NULL;
    list = NULL;
    islist = PyList_Check(seq);
    if  (islist)
        it = NULL;
    else {
        it = PyObject_GetIter(seq);
        if (it == NULL) {
            PyErr_SetString(PyExc_TypeError,
                "writelines() requires an iterable argument");
            return NULL;
        }
        /* From here on, fail by going to error, to reclaim "it". */
        list = PyList_New(CHUNKSIZE);
        if (list == NULL)
            goto error;
    }

    /* Strategy: slurp CHUNKSIZE lines into a private list,
       checking that they are all strings, then write that list
       without holding the interpreter lock, then come back for more. */
    for (index = 0; ; index += CHUNKSIZE) {
        if (islist) {
            Py_XDECREF(list);
            list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
            if (list == NULL)
                goto error;
            j = PyList_GET_SIZE(list);
        }
        else {
            for (j = 0; j < CHUNKSIZE; j++) {
                line = PyIter_Next(it);
                if (line == NULL) {
                    if (PyErr_Occurred())
                        goto error;
                    break;
                }
                PyList_SetItem(list, j, line);
            }
        }
        if (j == 0)
            break;

        /* Check that all entries are indeed strings. If not,
           apply the same rules as for file.write() and
           convert the results to strings. This is slow, but
           seems to be the only way since all conversion APIs
           could potentially execute Python code. */
        for (i = 0; i < j; i++) {
            PyObject *v = PyList_GET_ITEM(list, i);
            if (!PyString_Check(v)) {
                    const char *buffer;
                    int len;
                if (PyObject_AsReadBuffer(v,
                          (const void**)&buffer,
                                &len) ||
                     PyObject_AsCharBuffer(v,
                               &buffer,
                               &len)) {
                    PyErr_SetString(PyExc_TypeError,
            "writelines() argument must be a sequence of strings");
                    goto error;
                }
                line = PyString_FromStringAndSize(buffer,
                                  len);
                if (line == NULL)
                    goto error;
                Py_DECREF(v);
                PyList_SET_ITEM(list, i, line);
            }
        }

        /* Since we are releasing the global lock, the
           following code may *not* execute Python code. */
        Py_BEGIN_ALLOW_THREADS
        errno = 0;
        for (i = 0; i < j; i++) {
                line = PyList_GET_ITEM(list, i);
            len = PyString_GET_SIZE(line);
            nwritten = FCGX_PutStr(PyString_AS_STRING(line), len, s);
            if (nwritten != len) {
                Py_BLOCK_THREADS
                if (nwritten < 0) {
                    PyErr_SetString(PyExc_IOError, "Write failed");
                } else {
                    char msgbuf[256];
                    PyOS_snprintf(msgbuf, sizeof(msgbuf),
                          "Write failed, wrote %d of %d bytes",
                          nwritten, len);
                    PyErr_SetString(PyExc_IOError, msgbuf);
                }
                goto error;
            }
        }
        Py_END_ALLOW_THREADS

        if (j < CHUNKSIZE)
            break;
    }
static PyObject *
dmtx_decode(PyObject *self, PyObject *arglist, PyObject *kwargs)
{
   int count=0;
   int found=0;
   int width;
   int height;
   int gap_size = DmtxUndefined;
   int max_count = DmtxUndefined;
   int timeout = DmtxUndefined;
   int shape = DmtxUndefined;
   int deviation = DmtxUndefined;
   int threshold = DmtxUndefined;
   int shrink = 1;
   int corrections = DmtxUndefined;
   int min_edge = DmtxUndefined;
   int max_edge = DmtxUndefined;

   PyObject *dataBuf = NULL;
   Py_ssize_t dataLen;
   PyObject *context = Py_None;
   PyObject *output = PyList_New(0);

   DmtxTime dmtx_timeout;
   DmtxImage *img;
   DmtxDecode *dec;
   DmtxRegion *reg;
   DmtxMessage *msg;
   DmtxVector2 p00, p10, p11, p01;
   const char *pxl; /* Input image buffer */

   static char *kwlist[] = { "width", "height", "data", "gap_size",
                             "max_count", "context", "timeout", "shape",
                             "deviation", "threshold", "shrink", "corrections",
                             "min_edge", "max_edge", NULL };

   /* Parse out the options which are applicable */
   PyObject *filtered_kwargs;
   filtered_kwargs = PyDict_New();
   count = 3; /* Skip the first 3 keywords as they are sent in arglist */
   while(kwlist[count]){
      if(PyDict_GetItemString(kwargs, kwlist[count])) {
         PyDict_SetItemString(filtered_kwargs, kwlist[count],
               PyDict_GetItemString(kwargs, kwlist[count]));
      }
      count++;
   }

   /* Get parameters from Python for libdmtx */
   if(!PyArg_ParseTupleAndKeywords(arglist, filtered_kwargs, "iiOi|iOiiiiiiii",
         kwlist, &width, &height, &dataBuf, &gap_size, &max_count, &context,
         &timeout, &shape, &deviation, &threshold, &shrink, &corrections,
         &min_edge, &max_edge)) {
      PyErr_SetString(PyExc_TypeError, "decode takes at least 3 arguments");
      return NULL;
   }

   Py_INCREF(context);

   /* Reset timeout for each new page */
   if(timeout != DmtxUndefined)
      dmtx_timeout = dmtxTimeAdd(dmtxTimeNow(), timeout);

   if(dataBuf == NULL) {
      PyErr_SetString(PyExc_TypeError, "Interleaved bitmapped data in buffer missing");
      return NULL;
   }

   PyObject_AsCharBuffer(dataBuf, &pxl, &dataLen);

   img = dmtxImageCreate((unsigned char *)pxl, width, height, DmtxPack24bppRGB);
   if(img == NULL)
      return NULL;

   dec = dmtxDecodeCreate(img, shrink);
   if(dec == NULL) {
      dmtxImageDestroy(&img);
      return NULL;
   }

   if(gap_size != DmtxUndefined)
      dmtxDecodeSetProp(dec, DmtxPropScanGap, gap_size);

   if(shape != DmtxUndefined)
      dmtxDecodeSetProp(dec, DmtxPropSymbolSize, shape);

   if(deviation != DmtxUndefined)
      dmtxDecodeSetProp(dec, DmtxPropSquareDevn, deviation);

   if(threshold != DmtxUndefined)
      dmtxDecodeSetProp(dec, DmtxPropEdgeThresh, threshold);

   if(min_edge != DmtxUndefined)
      dmtxDecodeSetProp(dec, DmtxPropEdgeMin, min_edge);

   if(max_edge != DmtxUndefined)
      dmtxDecodeSetProp(dec, DmtxPropEdgeMax, max_edge);

   for(count=1; ;count++) {
      Py_BEGIN_ALLOW_THREADS
      if(timeout == DmtxUndefined)
         reg = dmtxRegionFindNext(dec, NULL);
      else
         reg = dmtxRegionFindNext(dec, &dmtx_timeout);
      Py_END_ALLOW_THREADS

      /* Finished file or ran out of time before finding another region */
      if(reg == NULL)
         break;

      msg = dmtxDecodeMatrixRegion(dec, reg, corrections);
      if(msg != NULL) {
         p00.X = p00.Y = p10.Y = p01.X = 0.0;
         p10.X = p01.Y = p11.X = p11.Y = 1.0;
         dmtxMatrix3VMultiplyBy(&p00, reg->fit2raw);
         dmtxMatrix3VMultiplyBy(&p10, reg->fit2raw);
         dmtxMatrix3VMultiplyBy(&p11, reg->fit2raw);
         dmtxMatrix3VMultiplyBy(&p01, reg->fit2raw);

         PyList_Append(output, Py_BuildValue("s#((ii)(ii)(ii)(ii))", msg->output, msg->outputIdx,
               (int)((shrink * p00.X) + 0.5), height - 1 - (int)((shrink * p00.Y) + 0.5),
               (int)((shrink * p10.X) + 0.5), height - 1 - (int)((shrink * p10.Y) + 0.5),
               (int)((shrink * p11.X) + 0.5), height - 1 - (int)((shrink * p11.Y) + 0.5),
               (int)((shrink * p01.X) + 0.5), height - 1 - (int)((shrink * p01.Y) + 0.5)));

         Py_INCREF(output);
         dmtxMessageDestroy(&msg);
         found++;
      }

      dmtxRegionDestroy(&reg);

      /* Stop if we've reached maximium count */
      if(max_count != DmtxUndefined)
         if(found >= max_count) break;
   }

   dmtxDecodeDestroy(&dec);
   dmtxImageDestroy(&img);
   Py_DECREF(context);
   if(output == NULL) {
      Py_INCREF(Py_None);
      return Py_None;
   }

   return output;
}
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
    const char *s, *start;
    char *end;
    double x=0.0, y=0.0, z;
    int got_bracket=0;
    char s_buffer[256];
    Py_ssize_t len;

    if (PyUnicode_Check(v)) {
        if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
            PyErr_SetString(PyExc_ValueError,
                     "complex() literal too large to convert");
            return NULL;
        }
        if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
                                    PyUnicode_GET_SIZE(v),
                                    s_buffer,
                                    NULL))
            return NULL;
        s = s_buffer;
        len = strlen(s);
    }
    else if (PyObject_AsCharBuffer(v, &s, &len)) {
        PyErr_SetString(PyExc_TypeError,
                        "complex() arg is not a string");
        return NULL;
    }

    /* position on first nonblank */
    start = s;
    while (Py_ISSPACE(*s))
        s++;
    if (*s == '(') {
        /* Skip over possible bracket from repr(). */
        got_bracket = 1;
        s++;
        while (Py_ISSPACE(*s))
            s++;
    }

    /* a valid complex string usually takes one of the three forms:

         <float>                  - real part only
         <float>j                 - imaginary part only
         <float><signed-float>j   - real and imaginary parts

       where <float> represents any numeric string that's accepted by the
       float constructor (including 'nan', 'inf', 'infinity', etc.), and
       <signed-float> is any string of the form <float> whose first
       character is '+' or '-'.

       For backwards compatibility, the extra forms

         <float><sign>j
         <sign>j
         j

       are also accepted, though support for these forms may be removed from
       a future version of Python.
    */

    /* first look for forms starting with <float> */
    z = PyOS_string_to_double(s, &end, NULL);
    if (z == -1.0 && PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_ValueError))
            PyErr_Clear();
        else
            return NULL;
    }
    if (end != s) {
        /* all 4 forms starting with <float> land here */
        s = end;
        if (*s == '+' || *s == '-') {
            /* <float><signed-float>j | <float><sign>j */
            x = z;
            y = PyOS_string_to_double(s, &end, NULL);
            if (y == -1.0 && PyErr_Occurred()) {
                if (PyErr_ExceptionMatches(PyExc_ValueError))
                    PyErr_Clear();
                else
                    return NULL;
            }
            if (end != s)
                /* <float><signed-float>j */
                s = end;
            else {
                /* <float><sign>j */
                y = *s == '+' ? 1.0 : -1.0;
                s++;
            }
            if (!(*s == 'j' || *s == 'J'))
                goto parse_error;
            s++;
        }
        else if (*s == 'j' || *s == 'J') {
            /* <float>j */
            s++;
            y = z;
        }
        else
            /* <float> */
            x = z;
    }
    else {
        /* not starting with <float>; must be <sign>j or j */
        if (*s == '+' || *s == '-') {
            /* <sign>j */
            y = *s == '+' ? 1.0 : -1.0;
            s++;
        }
        else
            /* j */
            y = 1.0;
        if (!(*s == 'j' || *s == 'J'))
            goto parse_error;
        s++;
    }

    /* trailing whitespace and closing bracket */
    while (Py_ISSPACE(*s))
        s++;
    if (got_bracket) {
        /* if there was an opening parenthesis, then the corresponding
           closing parenthesis should be right here */
        if (*s != ')')
            goto parse_error;
        s++;
        while (Py_ISSPACE(*s))
            s++;
    }

    /* we should now be at the end of the string */
    if (s-start != len)
        goto parse_error;

    return complex_subtype_from_doubles(type, x, y);

  parse_error:
    PyErr_SetString(PyExc_ValueError,
                    "complex() arg is a malformed string");
    return NULL;
}
// Convert a Python unicode/string/bytes object to a character string encoded
// according to the given encoding.  Update the object with a new reference to
// the object that owns the data.
const char *qpycore_encode(PyObject **s, QCoreApplication::Encoding encoding)
{
    PyObject *obj = *s;
    const char *es = 0;
    SIP_SSIZE_T sz;

    if (PyUnicode_Check(obj))
    {
        if (encoding == QCoreApplication::UnicodeUTF8)
        {
            obj = PyUnicode_AsUTF8String(obj);
        }
        else
        {
            QTextCodec *codec = QTextCodec::codecForTr();

            if (codec)
            {
                // Use the Qt codec to get to a byte string, and then to a
                // Python object.
                QString qs = qpycore_PyObject_AsQString(obj);
                QByteArray ba = codec->fromUnicode(qs);

#if PY_MAJOR_VERSION >= 3
                obj = PyBytes_FromStringAndSize(ba.constData(), ba.size());
#else
                obj = PyString_FromStringAndSize(ba.constData(), ba.size());
#endif
            }
            else
            {
                obj = PyUnicode_AsLatin1String(obj);
            }
        }

        if (obj)
        {
#if PY_MAJOR_VERSION >= 3
            es = PyBytes_AS_STRING(obj);
#else
            es = PyString_AS_STRING(obj);
#endif
        }
    }
#if PY_MAJOR_VERSION >= 3
    else if (PyBytes_Check(obj))
    {
        es = PyBytes_AS_STRING(obj);
        Py_INCREF(obj);
    }
#else
    else if (PyString_Check(obj))
    {
        es = PyString_AS_STRING(obj);
        Py_INCREF(obj);
    }
#endif
    else if (PyObject_AsCharBuffer(obj, &es, &sz) >= 0)
    {
        Py_INCREF(obj);
    }

    if (es)
    {
        *s = obj;
    }
    else
    {
        PyErr_Format(PyExc_UnicodeEncodeError,
                "unable to convert '%s' to requested encoding",
                Py_TYPE(*s)->tp_name);
    }

    return es;
}