[Jython-checkins] jython (2.5): Fix for http://bugs.jython.org/issue1640 (cStringIO does not complain on

alan.kennedy jython-checkins at python.org
Wed Mar 21 00:32:50 CET 2012


http://hg.python.org/jython/rev/64e2170c655e
changeset:   6442:64e2170c655e
branch:      2.5
parent:      6426:2923c6c259f9
user:        Alan Kennedy <alan at xhaus.com>
date:        Tue Mar 20 23:29:25 2012 +0000
summary:
  Fix for http://bugs.jython.org/issue1640 (cStringIO does not complain on getvalue after close)

files:
  Lib/test/test_StringIO_jy.py          |  15 +++++++++++++++
  src/org/python/modules/cStringIO.java |   6 ++++++
  2 files changed, 21 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_StringIO_jy.py b/Lib/test/test_StringIO_jy.py
--- a/Lib/test/test_StringIO_jy.py
+++ b/Lib/test/test_StringIO_jy.py
@@ -28,9 +28,24 @@
         f.write("uvwxyz")
         self.assertEqual(f.getvalue(), 'abcdef\x00\x00\x00\x00uvwxyz')
 
+class TestGetValueAfterClose(unittest.TestCase):
+
+    # This test, or something like it, should be really be pushed upstream
+    def test_getvalue_after_close(self):
+        f = cStringIO.StringIO('hello')
+        f.getvalue()
+        f.close()
+        try:
+            f.getvalue()
+        except ValueError:
+            pass
+        else:
+            self.fail("cStringIO.StringIO: getvalue() after close() should have raised ValueError")
+
 def test_main():
     test_support.run_unittest(TestUnicodeInput)
     test_support.run_unittest(TestWrite)
+    test_support.run_unittest(TestGetValueAfterClose)
 
 if __name__ == '__main__':
     test_main()
diff --git a/src/org/python/modules/cStringIO.java b/src/org/python/modules/cStringIO.java
--- a/src/org/python/modules/cStringIO.java
+++ b/src/org/python/modules/cStringIO.java
@@ -106,6 +106,11 @@
          */
         public void close() {
             closed = true;
+            // No point in zeroing the buf, because it won't be reused.
+            // buf is a final variable, so can't set to null.
+            // Therefore, just leave it and let it be GC'ed when the enclosing object is GC'ed
+            // Or remove the final declaration
+            // buf = null;
         }
 
 
@@ -402,6 +407,7 @@
          * @return      the contents of the StringIO.
          */
         public synchronized PyString getvalue() {
+            _complain_ifclosed();
             return new PyString(buf.toString());
         }
 

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


More information about the Jython-checkins mailing list