diff options
Diffstat (limited to 'tests/samplebinding/bytearray_bufferprotocol.cpp')
-rw-r--r-- | tests/samplebinding/bytearray_bufferprotocol.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/samplebinding/bytearray_bufferprotocol.cpp b/tests/samplebinding/bytearray_bufferprotocol.cpp new file mode 100644 index 000000000..b3a6a83f4 --- /dev/null +++ b/tests/samplebinding/bytearray_bufferprotocol.cpp @@ -0,0 +1,27 @@ +#if PY_VERSION_HEX < 0x03000000 +// ByteArray buffer protocol functions +// See: http://www.python.org/dev/peps/pep-3118/ +extern "C" { +static Py_ssize_t SbkByteArray_segcountproc(PyObject* self, Py_ssize_t* lenp) +{ + if (lenp) + *lenp = self->ob_type->tp_as_sequence->sq_length(self); + return 1; +} +static Py_ssize_t SbkByteArray_readbufferproc(PyObject* self, Py_ssize_t segment, void** ptrptr) +{ + if (segment || !Shiboken::Object::isValid(self)) + return -1; + + ByteArray* cppSelf = Shiboken::Converter<ByteArray*>::toCpp(self); + *ptrptr = reinterpret_cast<void*>(const_cast<char*>(cppSelf->data())); + return cppSelf->size(); +} +PyBufferProcs SbkByteArrayBufferProc = { + /*bf_getreadbuffer*/ &SbkByteArray_readbufferproc, + /*bf_getwritebuffer*/ (writebufferproc)&SbkByteArray_readbufferproc, + /*bf_getsegcount*/ &SbkByteArray_segcountproc, + /*bf_getcharbuffer*/ (charbufferproc)&SbkByteArray_readbufferproc +}; +} +#endif |