[Jython-checkins] jython: Fix PythonInterpreter such that it supports AutoCloseable, Closeable

jim.baker jython-checkins at python.org
Sat Jun 28 03:28:21 CEST 2014


http://hg.python.org/jython/rev/3cf75a71b464
changeset:   7330:3cf75a71b464
user:        Jim Baker <jim.baker at rackspace.com>
date:        Fri Jun 27 17:10:57 2014 -0600
summary:
  Fix PythonInterpreter such that it supports AutoCloseable, Closeable

files:
  src/org/python/jsr223/PyScriptEngine.java  |   6 ++++-
  src/org/python/util/PythonInterpreter.java |  12 +++++++++-
  2 files changed, 16 insertions(+), 2 deletions(-)


diff --git a/src/org/python/jsr223/PyScriptEngine.java b/src/org/python/jsr223/PyScriptEngine.java
--- a/src/org/python/jsr223/PyScriptEngine.java
+++ b/src/org/python/jsr223/PyScriptEngine.java
@@ -17,7 +17,7 @@
 import javax.script.SimpleBindings;
 import org.python.util.PythonInterpreter;
 
-public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
+public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable, AutoCloseable {
 
     private final PythonInterpreter interp;
     private final ScriptEngineFactory factory;
@@ -231,4 +231,8 @@
             return PyScriptEngine.this.eval(code, ctx);
         }
     }
+
+    public void close() {
+        interp.close();
+    }
 }
diff --git a/src/org/python/util/PythonInterpreter.java b/src/org/python/util/PythonInterpreter.java
--- a/src/org/python/util/PythonInterpreter.java
+++ b/src/org/python/util/PythonInterpreter.java
@@ -1,5 +1,6 @@
 package org.python.util;
 
+import java.io.Closeable;
 import java.io.Reader;
 import java.io.StringReader;
 import java.util.Properties;
@@ -25,7 +26,7 @@
  * The PythonInterpreter class is a standard wrapper for a Jython interpreter
  * for embedding in a Java application.
  */
-public class PythonInterpreter {
+public class PythonInterpreter implements AutoCloseable, Closeable {
 
     // Defaults if the interpreter uses thread-local state
     protected PySystemState systemState;
@@ -42,6 +43,8 @@
 
     protected CompilerFlags cflags = new CompilerFlags();
 
+    private volatile boolean closed = false;
+
     /**
      * Initializes the Jython runtime. This should only be called
      * once, before any other Python objects (including
@@ -365,4 +368,11 @@
         threadLocals.remove();
         sys.cleanup();
     }
+
+    public void close() {
+        if (!closed) {
+            closed = true;
+            cleanup();
+        }
+    }
 }

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list