Skip to content

Commit

Permalink
Merge remote-tracking branch 'jianfeng/111-DirectByteBuffer-support' …
Browse files Browse the repository at this point in the history
…into 111-DirectByteBuffer-support
  • Loading branch information
devinrsmith committed Dec 29, 2023
2 parents 0e735d2 + 0e97d0c commit adeb814
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/main/c/jpy_jobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ PyObject* JObj_FromType(JNIEnv* jenv, JPy_JType* type, jobject objectRef)
array = (JPy_JArray*) obj;
array->bufferExportCount = 0;
array->buf = NULL;
} else if ((*jenv)->IsInstanceOf(jenv, objectRef, JPy_ByteBuffer_JClass)) {
} else if (JByteBuffer_Check(type)) {
JPy_JByteBufferWrapper *byteBufferWrapper;
JPy_DIAG_PRINT(JPy_DIAG_F_MEM, "JObj_FromType: (Py_TYPE(&type->typeObj)->tp_basicsize=%d), (type->javaName=%s) setting byteBufferWrapper->pyBuffer=NULL\n", Py_TYPE(&type->typeObj)->tp_basicsize, type->javaName);
byteBufferWrapper = (JPy_JByteBufferWrapper *) obj;
Expand Down Expand Up @@ -180,8 +180,6 @@ void JObj_dealloc(JPy_JObj* self)
JNIEnv* jenv;
JPy_JType* jtype;

jenv = JPy_GetJNIEnv();

JPy_DIAG_PRINT(JPy_DIAG_F_MEM, "JObj_dealloc: releasing instance of %s, self->objectRef=%p\n", Py_TYPE(self)->tp_name, self->objectRef);

jtype = (JPy_JType *)Py_TYPE(self);
Expand All @@ -192,7 +190,7 @@ void JObj_dealloc(JPy_JObj* self)
if (array->buf != NULL) {
JArray_ReleaseJavaArrayElements(array, array->javaType);
}
} else if ((*jenv)->IsInstanceOf(jenv, self->objectRef, JPy_ByteBuffer_JClass)) {
} else if (JByteBuffer_Check(jtype)) {
JPy_JByteBufferWrapper *byteBufferWrapper;
JPy_DIAG_PRINT(JPy_DIAG_F_MEM, "JObj_dealloc: (jtype->javaName=%s) assuming JPy_JByteBufferWrapper, self->objectRef=%p\n", jtype->javaName, self->objectRef);
byteBufferWrapper = (JPy_JByteBufferWrapper *) self;
Expand All @@ -203,6 +201,7 @@ void JObj_dealloc(JPy_JObj* self)
}
}

jenv = JPy_GetJNIEnv();
if (jenv != NULL) {
if (self->objectRef != NULL) {
(*jenv)->DeleteGlobalRef(jenv, self->objectRef);
Expand Down Expand Up @@ -747,7 +746,7 @@ int JType_InitSlots(JPy_JType* type)

if (isPrimitiveArray) {
typeObj->tp_basicsize = sizeof(JPy_JArray);
} else if (type == JPy_JByteBuffer) {
} else if (JByteBuffer_Check(type)) {
typeObj->tp_basicsize = sizeof(JPy_JByteBufferWrapper);
JPy_DIAG_PRINT(JPy_DIAG_F_TYPE, "JType_InitSlots: allocating space for JPy_JByteBufferWrapper\n");
} else {
Expand Down Expand Up @@ -847,3 +846,13 @@ int JType_Check(PyObject* arg)
return PyType_Check(arg) && JPY_IS_JTYPE(arg);
}

int JByteBuffer_Check(JPy_JType* type) {
while (type != NULL) {
if (type == JPy_JByteBuffer || strcmp(type->javaName, "java.nio.ByteBuffer") == 0) {
JPy_DIAG_PRINT(JPy_DIAG_F_TYPE, "JByteBuffer_Check: java ByteBuffer or its sub-type (%s) found.\n", type->javaName);
return -1;
}
type = type->superType;
}
return 0;
}
2 changes: 2 additions & 0 deletions src/main/c/jpy_jobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ JPy_JObj;

int JObj_Check(PyObject* arg);

int JByteBuffer_Check(JPy_JType* type);

PyObject* JObj_New(JNIEnv* jenv, jobject objectRef);
PyObject* JObj_FromType(JNIEnv* jenv, JPy_JType* type, jobject objectRef);

Expand Down
2 changes: 1 addition & 1 deletion src/main/c/jpy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ PyObject* JType_CreateJavaByteBufferWrapper(JNIEnv* jenv, PyObject* pyObj)
PyBuffer_Release(pyBuffer);
PyMem_Free(pyBuffer);
JPy_DELETE_LOCAL_REF(tmpByteBufferRef);
PyErr_SetString(PyExc_RuntimeError, "jpy: internal error: failed to create a read-only ByteBuffer instance.");
PyErr_SetString(PyExc_RuntimeError, "jpy: internal error: failed to create a read-only direct ByteBuffer instance.");
return NULL;
}
JPy_DELETE_LOCAL_REF(tmpByteBufferRef);
Expand Down

0 comments on commit adeb814

Please sign in to comment.