Commits

Anonymous committed d31b335

Issue 7431: use TESTFN in test_linecache instead of trying to create a
file in the Lib/test directory, which might be read-only for the
user running the tests.

  • Participants
  • Parent commits 1fa1854
  • Branches legacy-trunk

Comments (0)

Files changed (2)

File Lib/test/test_linecache.py

         getline = linecache.getline
         try:
             # Create a source file and cache its contents
-            source_name = os.path.join(TEST_PATH, 'linecache_test.py')
-            source = open(source_name, 'w')
-            source.write(SOURCE_1)
-            source.close()
-            getline(source_name, 1)
+            source_name = support.TESTFN + '.py'
+            with open(source_name, 'w') as source:
+                source.write(SOURCE_1)
+                source.close()
+                getline(source_name, 1)
 
-            # Keep a copy of the old contents
-            source_list = []
-            source = open(source_name)
-            for index, line in enumerate(source):
-                self.assertEquals(line, getline(source_name, index + 1))
-                source_list.append(line)
-            source.close()
+                # Keep a copy of the old contents
+                source_list = []
+                source = open(source_name)
+                for index, line in enumerate(source):
+                    self.assertEquals(line, getline(source_name, index + 1))
+                    source_list.append(line)
+                source.close()
 
-            source = open(source_name, 'w')
-            source.write(SOURCE_2)
-            source.close()
+                source = open(source_name, 'w')
+                source.write(SOURCE_2)
+                source.close()
 
-            # 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):
-                self.assertEquals(line, getline(source_name, index + 1))
+                # 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)
-            source = open(source_name)
-            for index, line in enumerate(source):
-                self.assertEquals(line, getline(source_name, index + 1))
-                source_list.append(line)
-            source.close()
+                # Update the cache and check whether it matches the new source file
+                linecache.checkcache(source_name)
+                source = open(source_name)
+                for index, line in enumerate(source):
+                    self.assertEquals(line, getline(source_name, index + 1))
+                    source_list.append(line)
 
         finally:
-            try:
-                source.close()
-            finally:
-                support.unlink(source_name)
+            support.unlink(source_name)
 
 def test_main():
     support.run_unittest(LineCacheTests)
 Tests
 -----
 
+- Issue #7431: use TESTFN in test_linecache instead of trying to create a 
+  file in the Lib/test directory, which might be read-only for the
+  user running the tests.
+
 - Issue #7324: add a sanity check to regrtest argument parsing to
   catch the case of an option with no handler.