[Python-checkins] r81428 - python/trunk/Lib/test/test_linecache.py

benjamin.peterson python-checkins at python.org
Fri May 21 23:16:12 CEST 2010


Author: benjamin.peterson
Date: Fri May 21 23:16:12 2010
New Revision: 81428

Log:
use addCleanup

Modified:
   python/trunk/Lib/test/test_linecache.py

Modified: python/trunk/Lib/test/test_linecache.py
==============================================================================
--- python/trunk/Lib/test/test_linecache.py	(original)
+++ python/trunk/Lib/test/test_linecache.py	Fri May 21 23:16:12 2010
@@ -81,39 +81,36 @@
 
     def test_checkcache(self):
         getline = linecache.getline
-        try:
-            # Create a source file and cache its contents
-            source_name = support.TESTFN + '.py'
-            with open(source_name, 'w') as source:
-                source.write(SOURCE_1)
-            getline(source_name, 1)
-
-            # Keep a copy of the old contents
-            source_list = []
-            with open(source_name) as source:
-                for index, line in enumerate(source):
-                    self.assertEquals(line, getline(source_name, index + 1))
-                    source_list.append(line)
+        # Create a source file and cache its contents
+        source_name = support.TESTFN + '.py'
+        self.addCleanup(test_support.unlink, source_name)
+        with open(source_name, 'w') as source:
+            source.write(SOURCE_1)
+        getline(source_name, 1)
+
+        # Keep a copy of the old contents
+        source_list = []
+        with open(source_name) as source:
+            for index, line in enumerate(source):
+                self.assertEquals(line, getline(source_name, index + 1))
+                source_list.append(line)
 
-            with open(source_name, 'w') as source:
-                source.write(SOURCE_2)
+        with open(source_name, 'w') as source:
+            source.write(SOURCE_2)
 
-            # Try to update a bogus cache entry
-            linecache.checkcache('dummy')
+        # Try to update a bogus cache entry
+        linecache.checkcache('dummy')
 
-            # Check that the cache matches the old contents
-            for index, line in enumerate(source_list):
+        # Check that the cache matches the old contents
+        for index, line in enumerate(source_list):
+            self.assertEquals(line, getline(source_name, index + 1))
+
+        # Update the cache and check whether it matches the new source file
+        linecache.checkcache(source_name)
+        with open(source_name) as source:
+            for index, line in enumerate(source):
                 self.assertEquals(line, getline(source_name, index + 1))
-
-            # Update the cache and check whether it matches the new source file
-            linecache.checkcache(source_name)
-            with open(source_name) as source:
-                for index, line in enumerate(source):
-                    self.assertEquals(line, getline(source_name, index + 1))
-                    source_list.append(line)
-
-        finally:
-            support.unlink(source_name)
+                source_list.append(line)
 
 def test_main():
     support.run_unittest(LineCacheTests)


More information about the Python-checkins mailing list