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