Index: Objects/fileobject.c =================================================================== --- Objects/fileobject.c (revision 60728) +++ Objects/fileobject.c (working copy) @@ -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; Index: Lib/test/test_file.py =================================================================== --- Lib/test/test_file.py (revision 60728) +++ Lib/test/test_file.py (working copy) @@ -322,12 +322,29 @@ finally: os.unlink(TESTFN) +class FileSubclassTests(unittest.TestCase): + def setUp(self): + class C(file): + def __init__(self, *args): + self.subclass_closed = False + file.__init__(self, *args) + def close(self): + self.subclass_closed = True + self.C = C + + def testExit(self): + # test that exiting context calls subclass' close + with self.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)