/* The function is not static and not hidden as we use ctypes to test it. */ PyObject * typecast_BINARY_cast(const char *s, Py_ssize_t l, PyObject *curs) { chunkObject *chunk = NULL; PyObject *res = NULL; char *buffer = NULL; Py_ssize_t len; if (s == NULL) { Py_RETURN_NONE; } if (s[0] == '\\' && s[1] == 'x') { /* This is a buffer escaped in hex format: libpq before 9.0 can't * parse it and we can't detect reliably the libpq version at runtime. * So the only robust option is to parse it ourselves - luckily it's * an easy format. */ if (NULL == (buffer = psycopg_parse_hex(s, l, &len))) { goto exit; } } else { /* This is a buffer in the classic bytea format. So we can handle it * to the PQunescapeBytea to have it parsed, right? ...Wrong. We * could, but then we'd have to record whether buffer was allocated by * Python or by the libpq to dispose it properly. Furthermore the * PQunescapeBytea interface is not the most brilliant as it wants a * null-terminated string even if we have known its length thus * requiring a useless memcpy and strlen. * So we'll just have our better integrated parser, let's finish this * story. */ if (NULL == (buffer = psycopg_parse_escape(s, l, &len))) { goto exit; } } chunk = (chunkObject *) PyObject_New(chunkObject, &chunkType); if (chunk == NULL) goto exit; /* **Transfer** ownership of buffer's memory to the chunkObject: */ chunk->base = buffer; buffer = NULL; chunk->len = (Py_ssize_t)len; #if PY_MAJOR_VERSION < 3 if ((res = PyBuffer_FromObject((PyObject *)chunk, 0, chunk->len)) == NULL) goto exit; #else if ((res = PyMemoryView_FromObject((PyObject*)chunk)) == NULL) goto exit; #endif exit: Py_XDECREF((PyObject *)chunk); PyMem_Free(buffer); return res; }
static PyObject * buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) { PyObject *ob; int offset = 0; int size = Py_END_OF_BUFFER; if (!_PyArg_NoKeywords("buffer()", kw)) return NULL; if (!PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size)) return NULL; return PyBuffer_FromObject(ob, offset, size); }
static PyObject* GetDataBuffer(Cursor* cur, Py_ssize_t iCol) { PyObject* str = GetDataString(cur, iCol); if (str == Py_None) return str; PyObject* buffer = 0; if (str) { buffer = PyBuffer_FromObject(str, 0, PyString_GET_SIZE(str)); Py_DECREF(str); // If no buffer, release it. If buffer, the buffer owns it. } return buffer; }
static PyObject * buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) { PyObject *ob; Py_ssize_t offset = 0; Py_ssize_t size = Py_END_OF_BUFFER; if (PyErr_WarnPy3k("buffer() not supported in 3.x", 1) < 0) return NULL; if (!_PyArg_NoKeywords("buffer()", kw)) return NULL; if (!PyArg_ParseTuple(args, "O|nn:buffer", &ob, &offset, &size)) return NULL; return PyBuffer_FromObject(ob, offset, size); }
//Free the buffer given by grab_image //Expects free_image(handle, buffer) //Returns 0 for success static PyObject *free_image(PyObject *self, PyObject *args){ PyObject *obj, *buffer; int dev; struct image_cap *image_struct; //Get the buffer object from the arguments if (!PyArg_ParseTuple(args, "iO", &dev, &obj)){ PyErr_SetString(PyExc_TypeError, "Invalid arguments to free_image"); return NULL; } //Make sure it's a valid buffer object if (!PyBuffer_Check(obj)){ PyErr_SetString(PyExc_TypeError, "Invalid argument: not a PyBuffer"); return NULL; } //Convert the object to a buffer object buffer = PyBuffer_FromObject(obj, 0, Py_END_OF_BUFFER); //This shouldn't be an error if the object passes the previous check if (buffer->ob_type->tp_as_buffer->bf_getreadbuffer == NULL){ PyErr_SetString(PyExc_TypeError, "Invalid argument: not a readable PyBuffer"); return NULL; } //Create an image_cap structure image_struct = malloc(sizeof(struct image_cap)); //This call puts the size of PyBuffer object into image_struct.size //and sets the image_struct->data pointer to the beginning of the //PyBuffer's buffer image_struct->size = (int)(buffer->ob_type->tp_as_buffer->bf_getreadbuffer)\ (buffer, 0, &(image_struct->data)); image_struct->handle = dev; if (Cfree_image(image_struct)){ free(image_struct); PyErr_SetString(PyExc_IOError, "Error in C function call"); return NULL; } return PyInt_FromLong(0L); }
static PyObject * xpyb_resize_obj(PyObject *self, PyObject *args) { xpybProtobj *obj; Py_ssize_t size; PyObject *buf; if (!PyArg_ParseTuple(args, "O!n", &xpybProtobj_type, &obj, &size)) return NULL; buf = PyBuffer_FromObject(obj->buf, 0, size); if (buf == NULL) return NULL; Py_CLEAR(obj->buf); obj->buf = buf; Py_RETURN_NONE; }
static PyObject *sockRecv(sock_t *self, PyObject *args) { int flags = 0; void *buf; int buflen; PyObject *bbuf, *sbuf; if (!PyArg_ParseTuple(args, "|i", &flags)) return NULL; buflen = nn_recv(self->sock, &buf, NN_MSG, flags); if (buflen >= 0){ sbuf = PyString_FromStringAndSize(buf, buflen); bbuf = PyBuffer_FromObject(sbuf, 0, Py_END_OF_BUFFER); Py_DECREF(sbuf); nn_freemsg(buf); return bbuf; }else{ ERR; return NULL; } }
static int xpybList_init(xpybList *self, PyObject *args, PyObject *kw) { static char *kwlist[] = { "parent", "offset", "length", "type", "size", NULL }; Py_ssize_t i, datalen, cur, offset, length, size = -1; PyObject *parent, *type, *obj, *arglist; const char *data; if (!PyArg_ParseTupleAndKeywords(args, kw, "OnnO|n", kwlist, &parent, &offset, &length, &type, &size)) return -1; self->list = PyList_New(0); if (self->list == NULL) return -1; if (PyObject_AsReadBuffer(parent, (const void **)&data, &datalen) < 0) return -1; if (size > 0 && length * size + offset > datalen) { PyErr_Format(xpybExcept_base, "Protocol object buffer too short " "(expected %zd got %zd).", length * size + offset, datalen); return -1; } cur = offset; for (i = 0; i < length; i++) { if (PyString_CheckExact(type)) { obj = xpybList_build(type, length, data + cur); if (obj == NULL) return -1; cur += size; } else if (size > 0) { arglist = Py_BuildValue("(Onn)", parent, cur, size); obj = PyEval_CallObject(type, arglist); Py_DECREF(arglist); if (obj == NULL) return -1; cur += size; } else { arglist = Py_BuildValue("(On)", parent, cur); obj = PyEval_CallObject(type, arglist); Py_DECREF(arglist); if (obj == NULL) return -1; datalen = PySequence_Size(obj); if (datalen < 0) return -1; cur += datalen; } if (PyList_Append(self->list, obj) < 0) return -1; Py_DECREF(obj); } self->buf = PyBuffer_FromObject(parent, offset, cur - offset); if (self->buf == NULL) return -1; return 0; }