1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*****************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
See NOTICE file for details.
*****************************************************************************/
#include "jpype.h"
#include "pyjp.h"
#include "jp_buffer.h"
#include "jp_buffertype.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct PyJPBuffer
{
PyObject_HEAD
JPBuffer *m_Buffer;
} ;
static void PyJPBuffer_dealloc(PyJPBuffer *self)
{
JP_PY_TRY("PyJPBuffer_dealloc");
delete self->m_Buffer;
Py_TYPE(self)->tp_free(self);
JP_PY_CATCH(); // GCOV_EXCL_LINE
}
static PyObject *PyJPBuffer_repr(PyJPBuffer *self)
{
JP_PY_TRY("PyJPBuffer_repr");
return PyUnicode_FromFormat("<java buffer '%s'>", Py_TYPE(self)->tp_name);
JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
}
static void PyJPBuffer_releaseBuffer(PyJPBuffer *self, Py_buffer *view)
{
JP_PY_TRY("PyJPBufferPrimitive_releaseBuffer");
JP_PY_CATCH(); // GCOVR_EXCL_LINE
}
int PyJPBuffer_getBuffer(PyJPBuffer *self, Py_buffer *view, int flags)
{
JP_PY_TRY("PyJPBufferPrimitive_getBuffer");
JPJavaFrame frame = JPJavaFrame::outer();
if (self->m_Buffer == nullptr)
JP_RAISE(PyExc_ValueError, "Null buffer"); // GCOVR_EXCL_LINE
try
{
JPBuffer *buffer = self->m_Buffer;
if (!buffer->isValid())
{
PyErr_SetString(PyExc_BufferError, "Java buffer is not direct");
return -1;
}
if (buffer->isReadOnly() && (flags & PyBUF_WRITEABLE) == PyBUF_WRITEABLE)
{
PyErr_SetString(PyExc_BufferError, "Java buffer is not writable");
return -1;
}
*view = buffer->getView();
// If strides are not requested and this is a slice then fail
if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES)
{
if (view->strides[0] != view->itemsize)
JP_RAISE(PyExc_BufferError, "slices required strides");
view->strides = nullptr;
}
// If shape is not requested
if ((flags & PyBUF_ND) != PyBUF_ND)
{
view->shape = nullptr;
}
// If format is not requested
if ((flags & PyBUF_FORMAT) != PyBUF_FORMAT)
view->format = nullptr;
// Okay all successful so reference the parent object
view->obj = (PyObject*) self;
Py_INCREF(view->obj);
return 0;
} catch (JPypeException &ex) // GCOVR_EXCL_LINE
{
(void) ex;
// GCOVR_EXCL_START
PyJPBuffer_releaseBuffer(self, view);
// We are only allowed to raise BufferError
PyErr_SetString(PyExc_BufferError, "Java buffer view failed");
return -1;
// GCOVR_EXCL_STOP
}
JP_PY_CATCH(-1); // GCOVR_EXCL_LINE
}
static PyType_Slot bufferSlots[] = {
{ Py_tp_dealloc, (void*) PyJPBuffer_dealloc},
{ Py_tp_repr, (void*) PyJPBuffer_repr},
#if PY_VERSION_HEX >= 0x03090000
{ Py_bf_getbuffer, (void*) PyJPBuffer_getBuffer},
{ Py_bf_releasebuffer, (void*) PyJPBuffer_releaseBuffer},
#endif
{0}
};
#if PY_VERSION_HEX < 0x03090000
static PyBufferProcs directBuffer = {
(getbufferproc) & PyJPBuffer_getBuffer,
(releasebufferproc) & PyJPBuffer_releaseBuffer
};
#endif
PyTypeObject *PyJPBuffer_Type = nullptr;
static PyType_Spec bufferSpec = {
"_jpype._JBuffer",
sizeof (PyJPBuffer),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
bufferSlots
};
#ifdef __cplusplus
}
#endif
void PyJPBuffer_initType(PyObject * module)
{
JPPyObject tuple = JPPyTuple_Pack(PyJPObject_Type);
PyJPBuffer_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&bufferSpec, tuple.get());
#if PY_VERSION_HEX < 0x03090000
PyJPBuffer_Type->tp_as_buffer = &directBuffer;
#endif
JP_PY_CHECK();
PyModule_AddObject(module, "_JBuffer", (PyObject*) PyJPBuffer_Type);
JP_PY_CHECK();
}
JPPyObject PyJPBuffer_create(JPJavaFrame &frame, PyTypeObject *type, const JPValue& value)
{
JPPyObject obj = JPPyObject::call(type->tp_alloc(type, 0));
((PyJPBuffer*) obj.get())->m_Buffer = new JPBuffer(value);
PyJPValue_assignJavaSlot(frame, obj.get(), value);
return obj;
}
|