Closed jmao-denver closed 6 months ago
Another small wrinkle for the future; java 21 has long for capacity.
Here's a script I've been using:
import jpy
from contextlib import contextmanager
_JByteBuffer = jpy.get_type('java.nio.ByteBuffer')
_JArrowToTableConverter = jpy.get_type('io.deephaven.extensions.barrage.util.ArrowToTableConverter')
@contextmanager
def jpy_flags(flags):
orig_flags = jpy.diag.flags
jpy.diag.flags = flags
try:
yield
finally:
jpy.diag.flags = orig_flags
def buffer_protocol():
return jpy.byte_buffer(b'hello world')
def j_byte_buffer():
return _JByteBuffer.allocate(42)
def j_buffer():
# return type is java.nio.Buffer *not* ByteBuffer
# impl is ByteBuffer.allocateDirect(size)
return _JArrowToTableConverter.myBuffer(43)
def j_object():
# return type is java.lang.Object
# impl is new Object()
return _JArrowToTableConverter.newObject()
def print_info(name, x):
print(f"{name}: type(x)={type(x)}" + "\n")
print(f"{name}: x={x}" + "\n")
def create_print_del(name, fn):
my_obj = fn()
with jpy_flags(jpy.diag.F_OFF):
print_info(name, my_obj)
del my_obj
with jpy_flags(jpy.diag.F_MEM | jpy.diag.F_TYPE):
create_print_del('j_object', j_object)
create_print_del('buffer_protocol', buffer_protocol)
create_print_del('j_byte_buffer', j_byte_buffer)
create_print_del('j_buffer', j_buffer)
I've added some public static methods to ArrowToTableConverter
just because it was a place to put some java logic.
https://github.com/devinrsmith/jpy/tree/111-DirectByteBuffer-support is the branch where I've added some logging.
Fixes #111 Dependent on by https://github.com/deephaven/deephaven-core/pull/4936
Note, Java methods that receive ByteBuffer arguments are considered to be
borrowing
these buffers, notowning
them, and they are only guaranteed to be safe to access for the duration of the methods. So if these Python buffers are to be used in Java beyond these method calls, it falls to the users to keep the Python objects from being GCed after the Java method calls finish or make copies of the buffers before returning from the method calls.