Index: /org/python/core/IOShutdownHook.java =================================================================== --- /org/python/core/IOShutdownHook.java (revision 0) +++ /org/python/core/IOShutdownHook.java (revision 0) @@ -0,0 +1,13 @@ +package org.python.core; + +import java.util.Observable; + + +public class IOShutdownHook extends Observable implements Runnable { + + public void run() { + setChanged(); + notifyObservers(this); + } + +} Index: /org/python/core/Py.java =================================================================== --- /org/python/core/Py.java (revision 3122) +++ /org/python/core/Py.java (working copy) @@ -13,7 +13,15 @@ static java.util.Hashtable frozenModules; static boolean initialized; - + public final static IOShutdownHook IO_SHUTDOWN_HOOK = new IOShutdownHook(); + static { + try { + Runtime.getRuntime().addShutdownHook(new Thread(IO_SHUTDOWN_HOOK)); + } catch (SecurityException ex) { + Py.writeError("file", "Can't register IO Shutdown Hook!"); + } + } + static class SingletonResolver implements Serializable { private String which; Index: /org/python/core/PyFile.java =================================================================== --- /org/python/core/PyFile.java (revision 3122) +++ /org/python/core/PyFile.java (working copy) @@ -2,6 +2,7 @@ package org.python.core; import java.io.*; +import java.util.*; // To do: // - readinto(array) @@ -17,11 +18,17 @@ public class PyFile extends PyObject { - private static class FileWrapper { + private static class FileWrapper implements Observer { protected boolean reading; protected boolean writing; protected boolean binary; - + private Observable obs; + + public FileWrapper(){ + Py.IO_SHUTDOWN_HOOK.addObserver(this); + this.obs = Py.IO_SHUTDOWN_HOOK; + } + void setMode(String mode) { reading = mode.indexOf('r') >= 0; writing = mode.indexOf('w') >= 0 || mode.indexOf("+") >= 0 || @@ -28,6 +35,20 @@ mode.indexOf('a') >= 0; binary = mode.indexOf('b') >= 0; } + + public void update(Observable o, Object arg) { + try { + this.flush(); + } catch (IOException ioex) { + ioex.printStackTrace(); + } + } + + protected void unsubscribe() { + obs.deleteObserver(this); + obs = null; + } + public String read(int n) throws java.io.IOException { throw new java.io.IOException("file not open for reading"); } @@ -126,8 +147,9 @@ public void close() throws java.io.IOException { istream.close(); + unsubscribe(); } - + public Object __tojava__(Class cls) throws IOException { if (InputStream.class.isAssignableFrom(cls)) return istream; @@ -136,6 +158,7 @@ } private static class OutputStreamWrapper extends FileWrapper { + private java.io.OutputStream ostream; public OutputStreamWrapper(java.io.OutputStream s) { @@ -162,6 +185,7 @@ public void close() throws java.io.IOException { ostream.close(); + super.unsubscribe(); } public Object __tojava__(Class cls) throws IOException { @@ -191,6 +215,7 @@ public void close() throws java.io.IOException { ostream.close(); istream.close(); + unsubscribe(); } public Object __tojava__(Class cls) throws IOException { @@ -219,6 +244,7 @@ public void close() throws java.io.IOException { writer.close(); + unsubscribe(); } } @@ -472,6 +498,7 @@ } file.close(); + unsubscribe(); } public void truncate(long position) throws java.io.IOException { @@ -585,6 +612,7 @@ public void close() throws java.io.IOException { file.close(); + unsubscribe(); } public void truncate(long position) throws java.io.IOException {