/*NUMPY_API * Get buffer chunk from object * * this function takes a Python object which exposes the (single-segment) * buffer interface and returns a pointer to the data segment * * You should increment the reference count by one of buf->base * if you will hang on to a reference * * You only get a borrowed reference to the object. Do not free the * memory... */ NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf) { #if defined(NPY_PY3K) Py_buffer view; #else Py_ssize_t buflen; #endif buf->ptr = NULL; buf->flags = NPY_ARRAY_BEHAVED; buf->base = NULL; if (obj == Py_None) { return NPY_SUCCEED; } #if defined(NPY_PY3K) if (PyObject_GetBuffer(obj, &view, PyBUF_ANY_CONTIGUOUS|PyBUF_WRITABLE) != 0) { PyErr_Clear(); buf->flags &= ~NPY_ARRAY_WRITEABLE; if (PyObject_GetBuffer(obj, &view, PyBUF_ANY_CONTIGUOUS) != 0) { return NPY_FAIL; } } buf->ptr = view.buf; buf->len = (npy_intp) view.len; /* * XXX: PyObject_AsWriteBuffer does also this, but it is unsafe, as there is * no strict guarantee that the buffer sticks around after being released. */ PyBuffer_Release(&view); /* Point to the base of the buffer object if present */ if (PyMemoryView_Check(obj)) { buf->base = PyMemoryView_GET_BASE(obj); } #else if (PyObject_AsWriteBuffer(obj, &(buf->ptr), &buflen) < 0) { PyErr_Clear(); buf->flags &= ~NPY_ARRAY_WRITEABLE; if (PyObject_AsReadBuffer(obj, (const void **)&(buf->ptr), &buflen) < 0) { return NPY_FAIL; } } buf->len = (npy_intp) buflen; /* Point to the base of the buffer object if present */ if (PyBuffer_Check(obj)) { buf->base = ((PyArray_Chunk *)obj)->base; } #endif if (buf->base == NULL) { buf->base = obj; } return NPY_SUCCEED; }
/* * TODO: note the definition of the py_calc_z_ser() function. * (no code change required) */ static PyObject *py_calc_z_ser(PyObject *self, PyObject *args) { PyObject *q, *output; int len_q, maxiter, result = 0; Py_buffer q_buffer, output_buffer; /* Get the arguments: two arrays (two Objects) and two int */ if (!PyArg_ParseTuple(args, "OOii", &q, &output, &len_q, &maxiter)) { return NULL; } /* Attempt to extract buffer information from q */ if (PyObject_GetBuffer(q, &q_buffer, PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) { return NULL; } if (q_buffer.ndim != 1) { PyErr_SetString(PyExc_TypeError, "First arg must be a 1-dimensional array of complex"); PyBuffer_Release(&q_buffer); return NULL; } /* Attempt to extract buffer information from output_buffer */ if (PyObject_GetBuffer(output, &output_buffer, PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) { return NULL; } if (output_buffer.ndim != 1) { PyErr_SetString(PyExc_TypeError, "Second arg must be a 1-dimensional array of int"); PyBuffer_Release(&output_buffer); return NULL; } /* Check the type of items in the array */ if (strcmp(output_buffer.format, "i") != 0) { PyErr_SetString(PyExc_TypeError, "Second arg must be an array of int"); PyBuffer_Release(&output_buffer); return NULL; } /* Pass the raw buffers to the C function */ result = calc_z_ser(q_buffer.buf, output_buffer.buf, len_q, maxiter); /* Indicate we're done working with the buffers */ PyBuffer_Release(&q_buffer); PyBuffer_Release(&output_buffer); return Py_BuildValue("i", result); }
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; } } }
/* int primes_c(int, int *) */ static PyObject *py_primes_c(PyObject *self, PyObject *args) { int howmany; PyObject *bufobj; Py_buffer view; double result; /* Get the howmany param and the passed Python object (int and Object) */ if (!PyArg_ParseTuple(args, "iO", &howmany, &bufobj)) { return NULL; } /* Attempt to extract buffer information from it */ if (PyObject_GetBuffer(bufobj, &view, PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) { return NULL; } if (view.ndim != 1) { PyErr_SetString(PyExc_TypeError, "Expected a 1-dimensional array"); PyBuffer_Release(&view); return NULL; } /* Check the type of items in the array */ if (strcmp(view.format, "i") != 0) { PyErr_SetString(PyExc_TypeError, "Expected an array of int"); PyBuffer_Release(&view); return NULL; } /* Pass the raw buffer to the C function */ result = primes_c(howmany, view.buf); /* Indicate we're done working with the buffer */ PyBuffer_Release(&view); return Py_BuildValue("i", result); }
/* double avg(double*, int) */ static PyObject* py_avg(PyObject* self, PyObject* args) { PyObject* bufobj; Py_buffer view; double result; if (!PyArg_ParseTuple(args, "O", &bufobj)) { return NULL; } if (PyObject_GetBuffer(bufobj, &view, PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) { return NULL; } if (view.ndim != 1) { PyErr_SetString(PyExc_TypeError, "Expected a 1-dimensional array"); PyBuffer_Release(&view); return NULL; } if (strcmp(view.format, "d") != 0) { PyErr_SetString(PyExc_TypeError, "Expected an array of doubles"); PyBuffer_Release(&view); return NULL; } result = avg(view.buf, view.shape[0]); PyBuffer_Release(&view); return Py_BuildValue("d", result); }
static PyObject * buffer_get_buffer(BufferObject *self, PyObject *args, PyObject *kwds) { PyObject *obj; int flags = PyBUF_SIMPLE; int bufobj_flags = self->flags; char *keywords[] = {"obj", "flags", 0}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", keywords, &obj, &flags)) { return 0; } if (bufobj_flags & BUFOBJ_FILLED) { PyErr_SetString(PyExc_ValueError, "The Py_buffer struct is already filled in"); return 0; } self->flags = BUFOBJ_MUTABLE & bufobj_flags; if (!self->view_p) { self->view_p = PyMem_New(Py_buffer, 1); if (!self->view_p) { return PyErr_NoMemory(); } bufobj_flags |= BUFOBJ_MEMFREE; } if (PyObject_GetBuffer(obj, self->view_p, flags)) { if (bufobj_flags & BUFOBJ_MEMFREE) { PyMem_Free(self->view_p); self->view_p = 0; } return 0; } self->flags |= (bufobj_flags & BUFOBJ_MEMFREE) | BUFOBJ_FILLED; Py_RETURN_NONE; }
NUITKA_MAY_BE_UNUSED static bool BYTES_ADD_INCREMENTAL(PyObject **operand1, PyObject *operand2) { assert(PyBytes_CheckExact(*operand1)); assert(PyBytes_CheckExact(operand2)); // Buffer Py_buffer wb; wb.len = -1; if (PyObject_GetBuffer(operand2, &wb, PyBUF_SIMPLE) != 0) { PyErr_Format(PyExc_TypeError, "can't concat %s to %s", Py_TYPE(operand2)->tp_name, Py_TYPE(*operand1)->tp_name); return false; } Py_ssize_t oldsize = PyBytes_GET_SIZE(*operand1); if (oldsize > PY_SSIZE_T_MAX - wb.len) { PyErr_NoMemory(); PyBuffer_Release(&wb); return false; } if (_PyBytes_Resize(operand1, oldsize + wb.len) < 0) { PyBuffer_Release(&wb); return false; } memcpy(PyBytes_AS_STRING(*operand1) + oldsize, wb.buf, wb.len); PyBuffer_Release(&wb); return true; }
/* * Factory */ static PyObject * PyMemorySimpleView_FromObject(PyObject *base) { PyMemorySimpleViewObject *mview = NULL; Py_buffer view; if (Py_TYPE(base)->tp_as_buffer == NULL || Py_TYPE(base)->tp_as_buffer->bf_getbuffer == NULL) { PyErr_SetString(PyExc_TypeError, "cannot make memory view because object does " "not have the buffer interface"); return NULL; } memset(&view, 0, sizeof(Py_buffer)); if (PyObject_GetBuffer(base, &view, PyBUF_FULL_RO) < 0) return NULL; mview = (PyMemorySimpleViewObject *) PyObject_GC_New(PyMemorySimpleViewObject, &PyMemorySimpleView_Type); if (mview == NULL) { PyBuffer_Release(&view); return NULL; } memcpy(&mview->view, &view, sizeof(Py_buffer)); mview->base = base; Py_INCREF(base); PyObject_GC_Track(mview); return (PyObject *)mview; }
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_; } }
NPY_NO_EXPORT npy_bool _IsWriteable(PyArrayObject *ap) { PyObject *base=PyArray_BASE(ap); #if defined(NPY_PY3K) Py_buffer view; #else void *dummy; Py_ssize_t n; #endif /* If we own our own data, then no-problem */ if ((base == NULL) || (PyArray_FLAGS(ap) & NPY_ARRAY_OWNDATA)) { return NPY_TRUE; } /* * Get to the final base object * If it is a writeable array, then return TRUE * If we can find an array object * or a writeable buffer object as the final base object * or a string object (for pickling support memory savings). * - this last could be removed if a proper pickleable * buffer was added to Python. * * MW: I think it would better to disallow switching from READONLY * to WRITEABLE like this... */ while(PyArray_Check(base)) { if (PyArray_CHKFLAGS((PyArrayObject *)base, NPY_ARRAY_OWNDATA)) { return (npy_bool) (PyArray_ISWRITEABLE((PyArrayObject *)base)); } base = PyArray_BASE((PyArrayObject *)base); } /* * here so pickle support works seamlessly * and unpickled array can be set and reset writeable * -- could be abused -- */ if (PyString_Check(base)) { return NPY_TRUE; } #if defined(NPY_PY3K) if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) { PyErr_Clear(); return NPY_FALSE; } PyBuffer_Release(&view); #else if (PyObject_AsWriteBuffer(base, &dummy, &n) < 0) { PyErr_Clear(); return NPY_FALSE; } #endif return NPY_TRUE; }
static PyObject * pyaio_read(PyObject *dummy, PyObject *args) { int fd; Py_ssize_t numbytes, offset, ret; Pyaio_cb *aio; PyObject *callback, *return_, *buffer; Py_buffer *buffer_view; Py_XINCREF(args); if (PyArg_ParseTuple(args, "innO:set_callback", &fd, &offset, &numbytes, &callback)) { if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "parameter must be callable"); return NULL; } Py_XINCREF(callback); /* Add a reference to new callback */ } Py_XDECREF(args); aio = malloc(sizeof(Pyaio_cb)); buffer_view = &(aio->buffer_view); aio->cb = malloc(sizeof(struct aiocb)); bzero((char *) aio->cb, sizeof(struct aiocb)); /* Empty ByteArray of the requested read size INCREF*/ buffer = PyByteArray_FromStringAndSize(NULL, numbytes); /* Get the buffer view / put it in the aio struct INCREF */ PyObject_GetBuffer(buffer, buffer_view, PyBUF_CONTIG); aio->callback = callback; aio->buffer = buffer; aio->read = 1; /* Read Operation */ 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; /* EvIL */ 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_read(aio->cb); if (ret < 0) { return PyErr_SetFromErrno(PyExc_IOError); } else { return_ = Py_BuildValue("n", ret); return return_; } }
static int get_buffer(PyObject* obj, Py_buffer *buf, int force) { Py_buffer read_buf; int flags = PyBUF_ND|PyBUF_STRIDES|PyBUF_FORMAT; int ret; /* Attempt to get a writable buffer */ if (!PyObject_GetBuffer(obj, buf, flags|PyBUF_WRITABLE)) return 0; if (!force) return -1; /* Make a writable buffer from a read-only buffer */ PyErr_Clear(); if(-1 == PyObject_GetBuffer(obj, &read_buf, flags)) return -1; ret = PyBuffer_FillInfo(buf, NULL, read_buf.buf, read_buf.len, 0, flags|PyBUF_WRITABLE); PyBuffer_Release(&read_buf); return ret; }
static PyObject * pyuv__stream_write_bytes(Stream *self, PyObject *data, PyObject *callback, PyObject *send_handle) { int err; uv_buf_t buf; stream_write_ctx *ctx; Py_buffer *view; ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); return NULL; } ctx->views = ctx->viewsml; view = &ctx->views[0]; if (PyObject_GetBuffer(data, view, PyBUF_SIMPLE) != 0) { PyMem_Free(ctx); return NULL; } ctx->view_count = 1; ctx->obj = self; ctx->callback = callback; ctx->send_handle = send_handle; Py_INCREF(callback); Py_XINCREF(send_handle); buf = uv_buf_init(view->buf, view->len); if (send_handle != NULL) { ASSERT(UV_HANDLE(self)->type == UV_NAMED_PIPE); err = uv_write2(&ctx->req, (uv_stream_t *)UV_HANDLE(self), &buf, 1, (uv_stream_t *)UV_HANDLE(send_handle), pyuv__stream_write_cb); } else { err = uv_write(&ctx->req, (uv_stream_t *)UV_HANDLE(self), &buf, 1, pyuv__stream_write_cb); } if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); Py_DECREF(callback); Py_XDECREF(send_handle); PyBuffer_Release(view); PyMem_Free(ctx); return NULL; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; }
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; }
// from Python/bltinmodule.c static const char * source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy) { const char *str; Py_ssize_t size; Py_buffer view; *cmd_copy = NULL; if (PyUnicode_Check(cmd)) { cf->cf_flags |= PyCF_IGNORE_COOKIE; str = PyUnicode_AsUTF8AndSize(cmd, &size); if (str == NULL) return NULL; } else if (PyBytes_Check(cmd)) { str = PyBytes_AS_STRING(cmd); size = PyBytes_GET_SIZE(cmd); } else if (PyByteArray_Check(cmd)) { str = PyByteArray_AS_STRING(cmd); size = PyByteArray_GET_SIZE(cmd); } else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) { /* Copy to NUL-terminated buffer. */ *cmd_copy = PyBytes_FromStringAndSize( (const char *)view.buf, view.len); PyBuffer_Release(&view); if (*cmd_copy == NULL) { return NULL; } str = PyBytes_AS_STRING(*cmd_copy); size = PyBytes_GET_SIZE(*cmd_copy); } else { PyErr_Format(PyExc_TypeError, "%s() arg 1 must be a %s object", funcname, what); return NULL; } if (strlen(str) != (size_t)size) { PyErr_SetString(PyExc_ValueError, "source code string cannot contain null bytes"); Py_CLEAR(*cmd_copy); return NULL; } return str; }
PyObject * MGLContext_meth_replay(MGLContext * self, PyObject * bytecode) { if (MGLBytecode::ptr != MGLBytecode::buffer) { return 0; } memcpy(&MGLBytecode::gl, &self->gl, sizeof(GLMethods)); MGLScope_begin_core(self->default_scope); Py_buffer view = {}; PyObject_GetBuffer(bytecode, &view, PyBUF_STRIDED_RO); PyBuffer_ToContiguous(MGLBytecode::buffer, &view, view.len, 'C'); PyBuffer_Release(&view); MGLBytecode::evaluate((int)view.len); MGLScope_end_core(self->default_scope); Py_RETURN_NONE; }
static PyObject * asciistr_concat(PyObject *v, PyObject *w) { PyObject *bytes = NULL; Py_buffer view; if (PyBytes_Check(w)) { if (PyObject_GetBuffer(w, &view, PyBUF_CONTIG_RO) < 0) { return NULL; } bytes = PyBytes_FromFormat("%s%s", PyUnicode_DATA(v), (char *)view.buf); PyBuffer_Release(&view); return bytes; } return PyUnicode_Concat(v, w); }
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; }
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; }
virtual ssize_t read(void *buf, size_t size) { PyObject *args = Py_BuildValue("(n)", size); PyObject *result = PyObject_CallObject(this->readfunc, args); Py_XDECREF(args); if (result == NULL) { return -1; } Py_buffer py_buf; int buf_result = PyObject_GetBuffer(result, &py_buf, PyBUF_SIMPLE); Py_DECREF(result); if (buf_result < 0) { return -1; } memcpy(buf, py_buf.buf, py_buf.len); ssize_t read_bytes = py_buf.len; PyBuffer_Release(&py_buf); return read_bytes; }
static PyObject * s_unpack(PyObject *self, PyObject *input) { Py_buffer vbuf; PyObject *result; PyStructObject *soself = (PyStructObject *)self; assert(PyStruct_Check(self)); assert(soself->s_codes != NULL); if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) return NULL; if (vbuf.len != soself->s_size) { PyErr_Format(StructError, "unpack requires a bytes argument of length %zd", soself->s_size); PyBuffer_Release(&vbuf); return NULL; } result = s_unpack_internal(soself, vbuf.buf); PyBuffer_Release(&vbuf); return result; }
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; }
static PyObject *IORequest_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"mode", "buf", "filelike", "offset", NULL}; long long offset; int bufflags, fd; short mode; PyObject *buf, *filelike; IORequest *rv; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "hOOL", kwlist, &mode, &buf, &filelike, &offset)) return NULL; if (mode == IO_CMD_PREAD) bufflags = PyBUF_WRITABLE; else if (mode == IO_CMD_PWRITE) bufflags = PyBUF_SIMPLE; else { PyErr_SetString(PyExc_ValueError, "Invalid mode."); return NULL; } if ((fd = PyObject_AsFileDescriptor(filelike)) == -1) return NULL; if (!(rv = (IORequest*)type->tp_alloc(type, 0))) return NULL; rv->bufview.buf = NULL; if (PyObject_GetBuffer(buf, &rv->bufview, bufflags)) { Py_DECREF(rv); return NULL; } rv->iocb.aio_lio_opcode = mode; rv->iocb.aio_fildes = fd; rv->iocb.data = rv; rv->iocb.aio_reqprio = 0; rv->iocb.u.c.buf = rv->bufview.buf; rv->iocb.u.c.nbytes = rv->bufview.len; rv->iocb.u.c.offset = offset; rv->iocb.u.c.flags = IOCB_FLAG_RESFD; return (void*) rv; }
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; }
static int mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) { CHECK_VALID(-1); if (!is_writable(self)) return -1; if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); Py_ssize_t v; if (i == -1 && PyErr_Occurred()) return -1; if (i < 0) i += self->size; if (i < 0 || i >= self->size) { PyErr_SetString(PyExc_IndexError, "mmap index out of range"); return -1; } if (value == NULL) { PyErr_SetString(PyExc_TypeError, "mmap doesn't support item deletion"); return -1; } if (!PyIndex_Check(value)) { PyErr_SetString(PyExc_TypeError, "mmap item value must be an int"); return -1; } v = PyNumber_AsSsize_t(value, PyExc_TypeError); if (v == -1 && PyErr_Occurred()) return -1; if (v < 0 || v > 255) { PyErr_SetString(PyExc_ValueError, "mmap item value must be " "in range(0, 256)"); return -1; } self->data[i] = (char) v; return 0; } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelen; Py_buffer vbuf; if (PySlice_Unpack(item, &start, &stop, &step) < 0) { return -1; } slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step); if (value == NULL) { PyErr_SetString(PyExc_TypeError, "mmap object doesn't support slice deletion"); return -1; } if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) return -1; if (vbuf.len != slicelen) { PyErr_SetString(PyExc_IndexError, "mmap slice assignment is wrong size"); PyBuffer_Release(&vbuf); return -1; } if (slicelen == 0) { } else if (step == 1) { memcpy(self->data + start, vbuf.buf, slicelen); } else { Py_ssize_t cur, i; for (cur = start, i = 0; i < slicelen; cur += step, i++) { self->data[cur] = ((char *)vbuf.buf)[i]; } } PyBuffer_Release(&vbuf); return 0; } else { PyErr_SetString(PyExc_TypeError, "mmap indices must be integer"); return -1; } }
static int memorysimpleview_getbuffer(PyMemorySimpleViewObject *self, Py_buffer *view, int flags) { return PyObject_GetBuffer(self->base, view, flags); }
/* * Implement mapping assignment sub-script for the type. */ static int sipVoidPtr_ass_subscript(PyObject *self, PyObject *key, PyObject *value) { sipVoidPtrObject *v; Py_ssize_t start, size; #if PY_VERSION_HEX >= 0x02060000 Py_buffer value_view; #else Py_ssize_t value_size; void *value_ptr; #endif if (check_rw(self) < 0 || check_size(self) < 0) return -1; v = (sipVoidPtrObject *)self; if (PyIndex_Check(key)) { start = PyNumber_AsSsize_t(key, PyExc_IndexError); if (start == -1 && PyErr_Occurred()) return -1; if (start < 0) start += v->size; if (check_index(self, start) < 0) return -1; size = 1; } else if (PySlice_Check(key)) { Py_ssize_t stop, step; if (sipConvertFromSliceObject(key, v->size, &start, &stop, &step, &size) < 0) return -1; if (step != 1) { PyErr_SetNone(PyExc_NotImplementedError); return -1; } } else { bad_key(key); return -1; } #if PY_VERSION_HEX >= 0x02060000 if (PyObject_GetBuffer(value, &value_view, PyBUF_CONTIG_RO) < 0) return -1; /* We could allow any item size... */ if (value_view.itemsize != 1) { PyErr_Format(PyExc_TypeError, "'%s' must have an item size of 1", Py_TYPE(value_view.obj)->tp_name); PyBuffer_Release(&value_view); return -1; } if (check_slice_size(size, value_view.len) < 0) { PyBuffer_Release(&value_view); return -1; } memmove((char *)v->voidptr + start, value_view.buf, size); PyBuffer_Release(&value_view); #else if ((value_size = get_value_data(value, &value_ptr)) < 0) return -1; if (check_slice_size(size, value_size) < 0) return -1; memmove((char *)v->voidptr + start, value_ptr, size); #endif return 0; }
static PyObject * pyuv__stream_write_sequence(Stream *self, PyObject *data, PyObject *callback, PyObject *send_handle) { int err; stream_write_ctx *ctx; PyObject *data_fast, *item; Py_ssize_t i, j, buf_count; data_fast = PySequence_Fast(data, "data must be an iterable"); if (data_fast == NULL) return NULL; buf_count = PySequence_Fast_GET_SIZE(data_fast); if (buf_count > INT_MAX) { PyErr_SetString(PyExc_ValueError, "iterable is too long"); Py_DECREF(data_fast); return NULL; } if (buf_count == 0) { PyErr_SetString(PyExc_ValueError, "iterable is empty"); Py_DECREF(data_fast); return NULL; } ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); Py_DECREF(data_fast); return NULL; } ctx->views = ctx->viewsml; if (buf_count > ARRAY_SIZE(ctx->viewsml)) ctx->views = PyMem_Malloc(sizeof(Py_buffer) * buf_count); if (!ctx->views) { PyErr_NoMemory(); PyMem_Free(ctx); Py_DECREF(data_fast); return NULL; } ctx->view_count = buf_count; { STACK_ARRAY(uv_buf_t, bufs, buf_count); for (i = 0; i < buf_count; i++) { item = PySequence_Fast_GET_ITEM(data_fast, i); if (PyObject_GetBuffer(item, &ctx->views[i], PyBUF_SIMPLE) != 0) goto error; bufs[i].base = ctx->views[i].buf; bufs[i].len = ctx->views[i].len; } ctx->obj = self; ctx->callback = callback; ctx->send_handle = send_handle; Py_INCREF(callback); Py_XINCREF(send_handle); if (send_handle != NULL) { ASSERT(UV_HANDLE(self)->type == UV_NAMED_PIPE); err = uv_write2(&ctx->req, (uv_stream_t *)UV_HANDLE(self), bufs, buf_count, (uv_stream_t *)UV_HANDLE(send_handle), pyuv__stream_write_cb); } else { err = uv_write(&ctx->req, (uv_stream_t *)UV_HANDLE(self), bufs, buf_count, pyuv__stream_write_cb); } } if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); Py_DECREF(callback); Py_XDECREF(send_handle); goto error; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; error: for (j = 0; j < i; j++) PyBuffer_Release(&ctx->views[j]); if (ctx->views != ctx->viewsml) PyMem_Free(ctx->views); PyMem_Free(ctx); Py_XDECREF(data_fast); return NULL; }
PyObject * MGLContext_texture_array(MGLContext * self, PyObject * args) { int width; int height; int layers; int components; PyObject * data; int alignment; const char * dtype; Py_ssize_t dtype_size; int args_ok = PyArg_ParseTuple( args, "(III)IOIs#", &width, &height, &layers, &components, &data, &alignment, &dtype, &dtype_size ); if (!args_ok) { return 0; } if (components < 1 || components > 4) { MGLError_Set("the components must be 1, 2, 3 or 4"); return 0; } if (alignment != 1 && alignment != 2 && alignment != 4 && alignment != 8) { MGLError_Set("the alignment must be 1, 2, 4 or 8"); return 0; } if (dtype_size != 2) { MGLError_Set("invalid dtype"); return 0; } MGLDataType * data_type = from_dtype(dtype); if (!data_type) { MGLError_Set("invalid dtype"); return 0; } int expected_size = width * components * data_type->size; expected_size = (expected_size + alignment - 1) / alignment * alignment; expected_size = expected_size * height * layers; Py_buffer buffer_view; if (data != Py_None) { int get_buffer = PyObject_GetBuffer(data, &buffer_view, PyBUF_SIMPLE); if (get_buffer < 0) { MGLError_Set("data (%s) does not support buffer interface", Py_TYPE(data)->tp_name); return 0; } } else { buffer_view.len = expected_size; buffer_view.buf = 0; } if (buffer_view.len != expected_size) { MGLError_Set("data size mismatch %d != %d", buffer_view.len, expected_size); if (data != Py_None) { PyBuffer_Release(&buffer_view); } return 0; } int pixel_type = data_type->gl_type; int base_format = data_type->base_format[components]; int internal_format = data_type->internal_format[components]; const GLMethods & gl = self->gl; gl.ActiveTexture(GL_TEXTURE0 + self->default_texture_unit); MGLTextureArray * texture = (MGLTextureArray *)MGLTextureArray_Type.tp_alloc(&MGLTextureArray_Type, 0); texture->texture_obj = 0; gl.GenTextures(1, (GLuint *)&texture->texture_obj); if (!texture->texture_obj) { MGLError_Set("cannot create texture"); Py_DECREF(texture); return 0; } gl.BindTexture(GL_TEXTURE_2D_ARRAY, texture->texture_obj); gl.PixelStorei(GL_PACK_ALIGNMENT, alignment); gl.PixelStorei(GL_UNPACK_ALIGNMENT, alignment); gl.TexImage3D(GL_TEXTURE_2D_ARRAY, 0, internal_format, width, height, layers, 0, base_format, pixel_type, buffer_view.buf); gl.TexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl.TexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (data != Py_None) { PyBuffer_Release(&buffer_view); } texture->width = width; texture->height = height; texture->layers = layers; texture->components = components; texture->data_type = data_type; texture->min_filter = GL_LINEAR; texture->mag_filter = GL_LINEAR; texture->repeat_x = true; texture->repeat_y = true; texture->anisotropy = 1.0; Py_INCREF(self); texture->context = self; Py_INCREF(texture); PyObject * result = PyTuple_New(2); PyTuple_SET_ITEM(result, 0, (PyObject *)texture); PyTuple_SET_ITEM(result, 1, PyLong_FromLong(texture->texture_obj)); return result; }
PyObject * MGLTextureArray_write(MGLTextureArray * self, PyObject * args) { PyObject * data; PyObject * viewport; int alignment; int args_ok = PyArg_ParseTuple( args, "OOI", &data, &viewport, &alignment ); if (!args_ok) { return 0; } if (alignment != 1 && alignment != 2 && alignment != 4 && alignment != 8) { MGLError_Set("the alignment must be 1, 2, 4 or 8"); return 0; } int x = 0; int y = 0; int z = 0; int width = self->width; int height = self->height; int layers = self->layers; Py_buffer buffer_view; if (viewport != Py_None) { if (Py_TYPE(viewport) != &PyTuple_Type) { MGLError_Set("the viewport must be a tuple not %s", Py_TYPE(viewport)->tp_name); return 0; } if (PyTuple_GET_SIZE(viewport) == 6) { x = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 0)); y = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 1)); z = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 2)); width = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 3)); height = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 4)); layers = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 5)); } else if (PyTuple_GET_SIZE(viewport) == 3) { width = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 0)); height = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 1)); layers = PyLong_AsLong(PyTuple_GET_ITEM(viewport, 2)); } else { MGLError_Set("the viewport size %d is invalid", PyTuple_GET_SIZE(viewport)); return 0; } if (PyErr_Occurred()) { MGLError_Set("wrong values in the viewport"); return 0; } } int expected_size = width * self->components * self->data_type->size; expected_size = (expected_size + alignment - 1) / alignment * alignment; expected_size = expected_size * height * layers; int pixel_type = self->data_type->gl_type; int format = self->data_type->base_format[self->components]; if (Py_TYPE(data) == &MGLBuffer_Type) { MGLBuffer * buffer = (MGLBuffer *)data; const GLMethods & gl = self->context->gl; gl.BindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer->buffer_obj); gl.ActiveTexture(GL_TEXTURE0 + self->context->default_texture_unit); gl.BindTexture(GL_TEXTURE_2D_ARRAY, self->texture_obj); gl.PixelStorei(GL_PACK_ALIGNMENT, alignment); gl.PixelStorei(GL_UNPACK_ALIGNMENT, alignment); gl.TexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, x, y, z, width, height, layers, format, pixel_type, 0); gl.BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } else { int get_buffer = PyObject_GetBuffer(data, &buffer_view, PyBUF_SIMPLE); if (get_buffer < 0) { MGLError_Set("data (%s) does not support buffer interface", Py_TYPE(data)->tp_name); return 0; } if (buffer_view.len != expected_size) { MGLError_Set("data size mismatch %d != %d", buffer_view.len, expected_size); if (data != Py_None) { PyBuffer_Release(&buffer_view); } return 0; } const GLMethods & gl = self->context->gl; gl.ActiveTexture(GL_TEXTURE0 + self->context->default_texture_unit); gl.BindTexture(GL_TEXTURE_2D_ARRAY, self->texture_obj); gl.PixelStorei(GL_PACK_ALIGNMENT, alignment); gl.PixelStorei(GL_UNPACK_ALIGNMENT, alignment); gl.TexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, x, y, z, width, height, layers, format, pixel_type, buffer_view.buf); PyBuffer_Release(&buffer_view); } Py_RETURN_NONE; }