本文整理汇总了C++中PyBuffer_Release函数的典型用法代码示例。如果您正苦于以下问题:C++ PyBuffer_Release函数的具体用法?C++ PyBuffer_Release怎么用?C++ PyBuffer_Release使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyBuffer_Release函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mmap_write_method
static PyObject *
mmap_write_method(mmap_object *self,
PyObject *args)
{
Py_buffer data;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "y*:write", &data))
return(NULL);
if (!is_writable(self)) {
PyBuffer_Release(&data);
return NULL;
}
if (self->pos > self->size || self->size - self->pos < data.len) {
PyBuffer_Release(&data);
PyErr_SetString(PyExc_ValueError, "data out of range");
return NULL;
}
memcpy(&self->data[self->pos], data.buf, data.len);
self->pos += data.len;
PyBuffer_Release(&data);
return PyLong_FromSsize_t(data.len);
}
示例2: mmap_write_method
static PyObject *
mmap_write_method(mmap_object *self,
PyObject *args)
{
Py_buffer data;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "y*:write", &data))
return(NULL);
if (!is_writable(self)) {
PyBuffer_Release(&data);
return NULL;
}
if ((self->pos + data.len) > self->size) {
PyErr_SetString(PyExc_ValueError, "data out of range");
PyBuffer_Release(&data);
return NULL;
}
memcpy(self->data + self->pos, data.buf, data.len);
self->pos = self->pos + data.len;
PyBuffer_Release(&data);
Py_INCREF(Py_None);
return Py_None;
}
示例3: BYTES_ADD_INCREMENTAL
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;
}
示例4: PyObject_GetBuffer
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;
}
}
}
示例5: Stream_func_try_write
static PyObject *
Stream_func_try_write(Stream *self, PyObject *args)
{
int err;
uv_buf_t buf;
Py_buffer view;
RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);
if (!PyArg_ParseTuple(args, PYUV_BYTES"*:try_write", &view)) {
return NULL;
}
buf = uv_buf_init(view.buf, view.len);
err = uv_try_write((uv_stream_t *)UV_HANDLE(self), &buf, 1);
if (err < 0) {
RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self));
PyBuffer_Release(&view);
return NULL;
}
PyBuffer_Release(&view);
return PyInt_FromLong((long)err);
}
示例6: PyBytes_FromStringAndSize
static PyObject *Sponge_decrypt(PyObject *obj, PyObject *args)
{
SpongeObject *self = (SpongeObject *)obj;
PyObject *res;
PyObject *dataobj;
void *resdata;
Py_buffer buf;
if (!PyArg_ParseTuple(args, "O", &dataobj))
return NULL;
if (!get_buffer(dataobj, &buf))
return NULL;
/* allocate result object */
res = PyBytes_FromStringAndSize(NULL, buf.len);
if (!res) {
PyBuffer_Release(&buf);
return NULL;
}
/* get internal buffer */
resdata = PyBytes_AsString(res);
if (!resdata) {
PyBuffer_Release(&buf);
Py_CLEAR(res);
return NULL;
}
keccak_decrypt(&self->md, resdata, buf.buf, buf.len);
PyBuffer_Release(&buf);
return res;
}
示例7: py_avg
/* 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);
}
示例8: PyErr_SetString
/* 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);
}
示例9: PyErr_Format
static PyObject *verify(RSAObject *self, PyObject *args)
{
Py_buffer digest, signature;
int result = 0;
PyObject *result_obj = NULL;
#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTuple(args, "y*y*:verify", &digest, &signature))
return NULL;
#else
if (!PyArg_ParseTuple(args, "s*s*:verify", &digest, &signature))
return NULL;
#endif
if (digest.len != SHA_DIGEST_LENGTH)
{
PyErr_Format(PyExc_ValueError, "digest should be %i bytes",
SHA_DIGEST_LENGTH);
goto cleanup;
}
result = RSA_verify(NID_sha1, digest.buf, digest.len,
signature.buf, signature.len, self->rsa);
result_obj = PyBool_FromLong(result);
cleanup:
PyBuffer_Release(&digest);
PyBuffer_Release(&signature);
return result_obj;
}
示例10: PyBuffer_Release
static PyObject *convert(PyObject *self, PyObject *args) {
Py_buffer inBuf, outBuf;
short row, col, r, g, b, w, h, rd, gd, bd;
unsigned char *rgbPtr, *yPtr, y;
signed char *uPtr, *vPtr, u, v;
if(!PyArg_ParseTuple(args, "s*s*hh", &inBuf, &outBuf, &w, &h))
return NULL;
if(w & 31) w += 32 - (w & 31); // Round up width to multiple of 32
if(h & 15) h += 16 - (h & 15); // Round up height to multiple of 16
rgbPtr = outBuf.buf;
yPtr = inBuf.buf;
uPtr = (signed char *)&yPtr[w * h];
vPtr = &uPtr[(w * h) >> 2];
w >>= 1; // 2 columns processed per iteration
for(row=0; row<h; row++) {
for(col=0; col<w; col++) {
// U, V (and RGB deltas) updated on even columns
u = uPtr[col] - 128;
v = vPtr[col] - 128;
rd = (359 * v) >> 8;
gd = ((183 * v) + (88 * u)) >> 8;
bd = (454 * u) >> 8;
// Even column
y = *yPtr++;
r = y + rd;
g = y - gd;
b = y + bd;
*rgbPtr++ = (r > 255) ? 255 : (r < 0) ? 0 : r;
*rgbPtr++ = (g > 255) ? 255 : (g < 0) ? 0 : g;
*rgbPtr++ = (b > 255) ? 255 : (b < 0) ? 0 : b;
// Odd column
y = *yPtr++;
r = y + rd;
g = y - gd;
b = y + bd;
*rgbPtr++ = (r > 255) ? 255 : (r < 0) ? 0 : r;
*rgbPtr++ = (g > 255) ? 255 : (g < 0) ? 0 : g;
*rgbPtr++ = (b > 255) ? 255 : (b < 0) ? 0 : b;
}
if(row & 1) {
uPtr += w;
vPtr += w;
}
}
PyBuffer_Release(&inBuf);
PyBuffer_Release(&outBuf);
Py_INCREF(Py_None);
return Py_None;
}
示例11: Billiard_read
static PyObject *
Billiard_read(PyObject *self, PyObject *args)
{
int fd;
Py_buffer view;
Py_ssize_t buflen, recvlen = 0;
char *buf = NULL;
Py_ssize_t n = 0;
if (!PyArg_ParseTuple(args, "iw*|n", &fd, &view, &recvlen))
return NULL;
buflen = view.len;
buf = view.buf;
if (recvlen < 0) {
PyBuffer_Release(&view);
PyErr_SetString(PyExc_ValueError, "negative len for read");
return NULL;
}
if (recvlen == 0) {
recvlen = buflen;
}
if (buflen < recvlen) {
PyBuffer_Release(&view);
PyErr_SetString(PyExc_ValueError,
"Buffer too small for requested bytes");
return NULL;
}
if (buflen < 0 || buflen == 0) {
errno = EINVAL;
goto bail;
}
// Requires Python 2.7
//if (!_PyVerify_fd(fd)) goto bail;
Py_BEGIN_ALLOW_THREADS
n = read(fd, buf, recvlen);
Py_END_ALLOW_THREADS
if (n < 0) goto bail;
PyBuffer_Release(&view);
return PyInt_FromSsize_t(n);
bail:
PyBuffer_Release(&view);
return PyErr_SetFromErrno(PyExc_OSError);
}
示例12: PyErr_SetString
/*
* 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);
}
示例13: mmap_gfind
static PyObject *
mmap_gfind(mmap_object *self,
PyObject *args,
int reverse)
{
Py_ssize_t start = self->pos;
Py_ssize_t end = self->size;
Py_buffer view;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, reverse ? "y*|nn:rfind" : "y*|nn:find",
&view, &start, &end)) {
return NULL;
} else {
const char *p, *start_p, *end_p;
int sign = reverse ? -1 : 1;
const char *needle = view.buf;
Py_ssize_t len = view.len;
if (start < 0)
start += self->size;
if (start < 0)
start = 0;
else if (start > self->size)
start = self->size;
if (end < 0)
end += self->size;
if (end < 0)
end = 0;
else if (end > self->size)
end = self->size;
start_p = self->data + start;
end_p = self->data + end;
for (p = (reverse ? end_p - len : start_p);
(p >= start_p) && (p + len <= end_p); p += sign) {
Py_ssize_t i;
for (i = 0; i < len && needle[i] == p[i]; ++i)
/* nothing */;
if (i == len) {
PyBuffer_Release(&view);
return PyLong_FromSsize_t(p - self->data);
}
}
PyBuffer_Release(&view);
return PyLong_FromLong(-1);
}
}
示例14: DCStaticTriangleMeshShapeInit
static int DCStaticTriangleMeshShapeInit(DCStaticTriangleMeshShape *self, PyObject *args, PyObject *kwds)
{
DKObject<DKStaticTriangleMeshShape> shape = NULL;
if (self->shape == NULL)
{
Py_buffer vertexBuffer, indexBuffer;
int use16bit = 0;
int rebuildIndex = 1;
float welding = 0.0f;
char* kwlist[] = { "vertexBuffer", "indexBuffer", "use16bitIndex", "rebuildIndex", "weldingThreshold", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y*y*|ppf", kwlist,
&vertexBuffer, &indexBuffer, &use16bit, &rebuildIndex, &welding))
return -1;
DKVector3* vertices = reinterpret_cast<DKVector3*>(vertexBuffer.buf);
size_t numVerts = vertexBuffer.len / sizeof(DKVector3);
if (use16bit)
{
unsigned int* indices = reinterpret_cast<unsigned int*>(indexBuffer.buf);
size_t numIndices = indexBuffer.len / sizeof(unsigned int);
if (numVerts > 0 && numIndices > 0)
{
shape = DKOBJECT_NEW DKStaticTriangleMeshShape(vertices, numVerts, indices, numIndices, DKAABox(), rebuildIndex != 0, welding);
}
}
else
{
unsigned int* indices = reinterpret_cast<unsigned int*>(indexBuffer.buf);
size_t numIndices = indexBuffer.len / sizeof(unsigned int);
if (numVerts > 0 && numIndices > 0)
{
shape = DKOBJECT_NEW DKStaticTriangleMeshShape(vertices, numVerts, indices, numIndices, DKAABox(), rebuildIndex != 0, welding);
}
}
PyBuffer_Release(&vertexBuffer);
PyBuffer_Release(&indexBuffer);
if (shape == NULL)
{
PyErr_SetString(PyExc_ValueError, "invalid argument.");
return -1;
}
self->shape = shape;
}
self->base.shape = self->shape;
return DCConcaveShapeTypeObject()->tp_init((PyObject*)self, args, kwds);
}
示例15: PyBlosc_decompress
static PyObject *
PyBlosc_decompress(PyObject *self, PyObject *args)
{
Py_buffer view;
PyObject *result_str;
void *input, *output;
size_t nbytes, cbytes;
char *format;
/* Accept some kind of input */
#if PY_MAJOR_VERSION <= 2
/* s* : bytes like object including unicode and anything that supports
* the buffer interface */
format = "s*:decompress";
#elif PY_MAJOR_VERSION >= 3
/* y* :bytes like object EXCLUDING unicode and anything that supports
* the buffer interface. This is the recommended way to accept binary
* data in Python 3. */
format = "y*:decompress";
#endif
if (!PyArg_ParseTuple(args, format, &view))
return NULL;
cbytes = view.len;
input = view.buf;
/* fetch the uncompressed size into nbytes */
if (!get_nbytes(input, cbytes, &nbytes)){
PyBuffer_Release(&view);
return NULL;
}
/* Book memory for the result */
if (!(result_str = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes))){
PyBuffer_Release(&view);
return NULL;
}
output = PyBytes_AS_STRING(result_str);
/* do decompression */
if (!decompress_helper(input, nbytes, output)){
Py_XDECREF(result_str);
PyBuffer_Release(&view);
return NULL;
}
PyBuffer_Release(&view);
return result_str;
}