def testRefs(self): # This routine will exercise each of the clean up paths once fixture = JClass("jpype.common.Fixture")() def f(): # Create a proxy to test the proxy path @JImplements("java.util.function.Supplier") class MySupplier(object): @JOverride def get(self): # Send a Python exc to trigger Python ref path raise RuntimeError("foo") try: u = MySupplier() fixture.callSupplier(u) except RuntimeError as ex: pass f() # Force a direct byffer and then trash it b = bytearray([1, 2, 3]) _jpype.convertToDirectBuffer(b) # Then force a GC to clean it up jpype.java.lang.System.gc()
def convertToDirectBuffer(obj): __doc__ = '''Efficiently convert all array.array types, string and unicode to java.nio.Buffer objects.''' memoryview_of_obj = _mem_view(obj) if memoryview_of_obj.readonly: raise ValueError("Memoryview must be writable for wrapping in a byte buffer") return _jpype.convertToDirectBuffer(memoryview_of_obj)
def convertToDirectBuffer(obj): __doc__ = '''Efficiently convert all array.array and numpy ndarray types, string and unicode to java.nio.Buffer objects.''' memoryview_of_obj = memoryview(obj) if memoryview_of_obj.readonly: raise ValueError( "Memoryview must be writable for wrapping in a byte buffer") return _jpype.convertToDirectBuffer(memoryview_of_obj)
def testConvertToDirectBufferExc(self): with self.assertRaisesRegex(TypeError, "buffer support"): _jpype.convertToDirectBuffer(1) with self.assertRaisesRegex(BufferError, "not writable"): _jpype.convertToDirectBuffer(bytes([1, 2, 3]))
def testConvertToDirectBufferFault(self): _jpype.fault("PyJPModule_convertToDirectByteBuffer") with self.assertRaisesRegex(SystemError, "fault"): _jpype.convertToDirectBuffer(1)
def convertToDirectBuffer(obj): '''Efficiently convert all array.array types, string and unicode to java.nio.Buffer objects.''' return _jpype.convertToDirectBuffer(obj)