]> granicus.if.org Git - python/commitdiff
#2067: file.__exit__() now calls subclasses' close() method.
authorGeorg Brandl <georg@python.org>
Sat, 23 Feb 2008 15:11:18 +0000 (15:11 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 23 Feb 2008 15:11:18 +0000 (15:11 +0000)
Lib/test/test_file.py
Misc/NEWS
Objects/fileobject.c

index 73cb5b2412780a695befde5047cf61e7719d85f6..3ae460c97698b9a42cb05fe95226849779067bda 100644 (file)
@@ -322,12 +322,28 @@ class OtherFileTests(unittest.TestCase):
         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)
index c1172de5c6a335693b781863497f4723e1282da8..e1fc418afbe7ad673634096bf1889c70853ee74f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,7 +12,9 @@ What's New in Python 2.6 alpha 1?
 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.
index eb05cdad51e79c86d6628a66400956174814c783..932b7dc74da50d3c3f1aad797eed160367b7de91 100644 (file)
@@ -1660,9 +1660,9 @@ file_self(PyFileObject *f)
 }
 
 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;