[Jython-checkins] jython: Let PyBuffer support try-with-resources.
jeff.allen
jython-checkins at python.org
Sat Jun 14 14:02:18 CEST 2014
http://hg.python.org/jython/rev/100892193c90
changeset: 7291:100892193c90
parent: 7289:1f517f1e5a08
user: Jeff Allen <ja.py at farowl.co.uk>
date: Fri Jun 13 23:36:56 2014 +0100
summary:
Let PyBuffer support try-with-resources.
Now extends AutoCloseable. Also adds Java test.
files:
src/org/python/core/PyBuffer.java | 6 +-
src/org/python/core/buffer/BaseBuffer.java | 5 +
tests/java/org/python/core/PyBufferTest.java | 29 +++++++++-
3 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/src/org/python/core/PyBuffer.java b/src/org/python/core/PyBuffer.java
--- a/src/org/python/core/PyBuffer.java
+++ b/src/org/python/core/PyBuffer.java
@@ -5,7 +5,7 @@
* the counterpart of the CPython <code>Py_buffer</code> struct. Several concrete types implement
* this interface in order to provide tailored support for different storage organisations.
*/
-public interface PyBuffer extends PyBUF, BufferProtocol {
+public interface PyBuffer extends PyBUF, BufferProtocol, AutoCloseable {
/*
* The different behaviours required as the actual structure of the buffer changes (from one
@@ -183,6 +183,10 @@
*/
void release();
+ /** An alias for {@link #release()} to satisfy {@link AutoCloseable}. */
+ @Override
+ void close();
+
/**
* True only if the buffer has been released with (the required number of calls to)
* {@link #release()} or some equivalent operation. The consumer may be sharing the reference
diff --git a/src/org/python/core/buffer/BaseBuffer.java b/src/org/python/core/buffer/BaseBuffer.java
--- a/src/org/python/core/buffer/BaseBuffer.java
+++ b/src/org/python/core/buffer/BaseBuffer.java
@@ -522,6 +522,11 @@
}
@Override
+ public void close() {
+ release();
+ }
+
+ @Override
public boolean isReleased() {
return exports <= 0;
}
diff --git a/tests/java/org/python/core/PyBufferTest.java b/tests/java/org/python/core/PyBufferTest.java
--- a/tests/java/org/python/core/PyBufferTest.java
+++ b/tests/java/org/python/core/PyBufferTest.java
@@ -701,8 +701,13 @@
Set<PyBuffer> uniqueBuffers = new HashSet<PyBuffer>();
+ // Test a balanced sequence of acquire and release using try-with-resources
for (BufferTestPair test : buffersToRead) {
- // Test a pattern of acquire and release with one more release than acquire
+ doTestTryWithResources(test);
+ }
+
+ // Now test a pattern of acquire and release with one more release than acquire
+ for (BufferTestPair test : buffersToRead) {
doTestRelease(test);
uniqueBuffers.add(test.view);
}
@@ -718,6 +723,28 @@
doTestGetAfterRelease(test);
}
}
+
+ }
+
+ /**
+ * Exercise try-with-resources on one BufferTestPair.
+ */
+ private void doTestTryWithResources(BufferTestPair test) {
+
+ if (verbosity > 0) {
+ System.out.println("try with resources: " + test);
+ }
+ int flags = PyBUF.STRIDES | PyBUF.FORMAT;
+ BufferProtocol sub = test.subject;
+
+ // The object will be exporting test.view and N other views we don't know about
+ try (PyBuffer c = sub.getBuffer(flags)) { // = N+1 exports
+ try (PyBuffer b = sub.getBuffer(PyBUF.FULL_RO); PyBuffer d =c.getBuffer(flags)) {
+ checkExporting(sub);// = N+3 exports
+ }
+ checkExporting(sub); // = N+1 exports
+ }
+ checkExporting(sub); // = N export
}
/**
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list