[Python-checkins] cpython: Close handler opened in test.

vinay.sajip python-checkins at python.org
Fri May 6 08:19:48 CEST 2011


http://hg.python.org/cpython/rev/b122745b4e22
changeset:   69866:b122745b4e22
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Fri May 06 07:19:41 2011 +0100
summary:
  Close handler opened in test.

files:
  Lib/test/test_logging.py |  48 ++++++++++++++-------------
  1 files changed, 25 insertions(+), 23 deletions(-)


diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3484,29 +3484,31 @@
     def test_rollover(self):
         fh = logging.handlers.TimedRotatingFileHandler(self.fn, 'S',
                                                        backupCount=1)
-        r = logging.makeLogRecord({'msg': 'testing'})
-        fh.emit(r)
-        self.assertLogFile(self.fn)
-        time.sleep(1.0)
-        fh.emit(r)
-        now = datetime.datetime.now()
-        prevsec = now - datetime.timedelta(seconds=1)
-        earlier = now - datetime.timedelta(seconds=2)
-        fn1 = self.fn + prevsec.strftime(".%Y-%m-%d_%H-%M-%S")
-        fn2 = self.fn + earlier.strftime(".%Y-%m-%d_%H-%M-%S")
-        success = os.path.exists(fn1) or os.path.exists(fn2)
-        if not success:
-            # print additional diagnostic information
-            print('Neither %s nor %s exists' % (fn1, fn2), file=sys.stderr)
-            dirname = os.path.dirname(fn1)
-            files = os.listdir(dirname)
-            files = [f for f in files if f.startswith('test_logging-2-')]
-            print('matching files: %s' % files, file=sys.stderr)
-        self.assertTrue(success)
-        for fn in (fn1, fn2):
-            if os.path.exists(fn):
-                self.rmfiles.append(fn)
-
+        try:
+            r = logging.makeLogRecord({'msg': 'testing'})
+            fh.emit(r)
+            self.assertLogFile(self.fn)
+            time.sleep(1.0)
+            fh.emit(r)
+            now = datetime.datetime.now()
+            prevsec = now - datetime.timedelta(seconds=1)
+            earlier = now - datetime.timedelta(seconds=2)
+            fn1 = self.fn + prevsec.strftime(".%Y-%m-%d_%H-%M-%S")
+            fn2 = self.fn + earlier.strftime(".%Y-%m-%d_%H-%M-%S")
+            success = os.path.exists(fn1) or os.path.exists(fn2)
+            if not success:
+                # print additional diagnostic information
+                print('Neither %s nor %s exists' % (fn1, fn2), file=sys.stderr)
+                dirname = os.path.dirname(fn1)
+                files = os.listdir(dirname)
+                files = [f for f in files if f.startswith('test_logging-2-')]
+                print('matching files: %s' % files, file=sys.stderr)
+            self.assertTrue(success)
+            for fn in (fn1, fn2):
+                if os.path.exists(fn):
+                    self.rmfiles.append(fn)
+        finally:
+            fh.close()
 
     def test_invalid(self):
         assertRaises = self.assertRaises

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


More information about the Python-checkins mailing list