[Python-checkins] r60984 - in python/trunk: Lib/test/test_file.py Misc/NEWS Objects/fileobject.c

georg.brandl python-checkins at python.org
Sat Feb 23 16:11:18 CET 2008


Author: georg.brandl
Date: Sat Feb 23 16:11:18 2008
New Revision: 60984

Modified:
   python/trunk/Lib/test/test_file.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/fileobject.c
Log:
#2067: file.__exit__() now calls subclasses' close() method.


Modified: python/trunk/Lib/test/test_file.py
==============================================================================
--- python/trunk/Lib/test/test_file.py	(original)
+++ python/trunk/Lib/test/test_file.py	Sat Feb 23 16:11:18 2008
@@ -322,12 +322,28 @@
         finally:
             os.unlink(TESTFN)
 
+class FileSubclassTests(unittest.TestCase):
+
+    def testExit(self):
+        # test that exiting with context calls subclass' close
+        class C(file):
+            def __init__(self, *args):
+                self.subclass_closed = False
+                file.__init__(self, *args)
+            def close(self):
+                self.subclass_closed = True
+                file.close(self)
+
+        with C(TESTFN, 'w') as f:
+            pass
+        self.failUnless(f.subclass_closed)
+
 
 def test_main():
     # Historically, these tests have been sloppy about removing TESTFN.
     # So get rid of it no matter what.
     try:
-        run_unittest(AutoFileTests, OtherFileTests)
+        run_unittest(AutoFileTests, OtherFileTests, FileSubclassTests)
     finally:
         if os.path.exists(TESTFN):
             os.unlink(TESTFN)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Feb 23 16:11:18 2008
@@ -12,7 +12,9 @@
 Core and builtins
 -----------------
 
-- Patch #1759: Backport of PEP 3129 class decorators
+- Issue #2067: file.__exit__() now calls subclasses' close() method.
+
+- Patch #1759: Backport of PEP 3129 class decorators.
 
 - Issue #1881: An internal parser limit has been increased. Also see 
   issue 215555 for a discussion.

Modified: python/trunk/Objects/fileobject.c
==============================================================================
--- python/trunk/Objects/fileobject.c	(original)
+++ python/trunk/Objects/fileobject.c	Sat Feb 23 16:11:18 2008
@@ -1660,9 +1660,9 @@
 }
 
 static PyObject *
-file_exit(PyFileObject *f, PyObject *args)
+file_exit(PyObject *f, PyObject *args)
 {
-	PyObject *ret = file_close(f);
+	PyObject *ret = PyObject_CallMethod(f, "close", NULL);
 	if (!ret)
 		/* If error occurred, pass through */
 		return NULL;


More information about the Python-checkins mailing list