[Python-checkins] bpo-25872: Add unit tests for linecache and threading (GH-25913) (GH-26211)

iritkatriel webhook-mailer at python.org
Tue May 18 10:26:07 EDT 2021


https://github.com/python/cpython/commit/c05d8a6b67785450b1fec0d30fe26d5478bc4f0b
commit: c05d8a6b67785450b1fec0d30fe26d5478bc4f0b
branch: 3.9
author: Irit Katriel <iritkatriel at yahoo.com>
committer: iritkatriel <iritkatriel at yahoo.com>
date: 2021-05-18T15:25:38+01:00
summary:

bpo-25872: Add unit tests for linecache and threading (GH-25913) (GH-26211)

(cherry picked from commit 115dea9e2602b96b63390f00cc880e90c433efa2)

Co-authored-by: uniocto <serit142sa33go at gmail.com>

files:
M Lib/test/test_linecache.py
M Lib/test/test_threading.py

diff --git a/Lib/test/test_linecache.py b/Lib/test/test_linecache.py
index 375d9c42137ba2..f5691163b8ec56 100644
--- a/Lib/test/test_linecache.py
+++ b/Lib/test/test_linecache.py
@@ -238,5 +238,47 @@ def raise_memoryerror(*args, **kwargs):
         self.assertEqual(linecache.getlines(FILENAME), lines)
 
 
+class LineCacheInvalidationTests(unittest.TestCase):
+    def setUp(self):
+        super().setUp()
+        linecache.clearcache()
+        self.deleted_file = support.TESTFN + '.1'
+        self.modified_file = support.TESTFN + '.2'
+        self.unchanged_file = support.TESTFN + '.3'
+
+        for fname in (self.deleted_file,
+                      self.modified_file,
+                      self.unchanged_file):
+            self.addCleanup(support.unlink, fname)
+            with open(fname, 'w', encoding='utf-8') as source:
+                source.write(f'print("I am {fname}")')
+
+            self.assertNotIn(fname, linecache.cache)
+            linecache.getlines(fname)
+            self.assertIn(fname, linecache.cache)
+
+        os.remove(self.deleted_file)
+        with open(self.modified_file, 'w', encoding='utf-8') as source:
+            source.write('print("was modified")')
+
+    def test_checkcache_for_deleted_file(self):
+        linecache.checkcache(self.deleted_file)
+        self.assertNotIn(self.deleted_file, linecache.cache)
+        self.assertIn(self.modified_file, linecache.cache)
+        self.assertIn(self.unchanged_file, linecache.cache)
+
+    def test_checkcache_for_modified_file(self):
+        linecache.checkcache(self.modified_file)
+        self.assertIn(self.deleted_file, linecache.cache)
+        self.assertNotIn(self.modified_file, linecache.cache)
+        self.assertIn(self.unchanged_file, linecache.cache)
+
+    def test_checkcache_with_no_parameter(self):
+        linecache.checkcache()
+        self.assertNotIn(self.deleted_file, linecache.cache)
+        self.assertNotIn(self.modified_file, linecache.cache)
+        self.assertIn(self.unchanged_file, linecache.cache)
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 67e061e8aa63bc..a57085b75d58ad 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -3,7 +3,7 @@
 """
 
 import test.support
-from test.support import verbose, import_module, cpython_only
+from test.support import verbose, import_module, cpython_only, unlink
 from test.support.script_helper import assert_python_ok, assert_python_failure
 
 import random
@@ -17,6 +17,7 @@
 import subprocess
 import signal
 import textwrap
+import traceback
 
 from test import lock_tests
 from test import support
@@ -1243,6 +1244,22 @@ def run(self):
         # explicitly break the reference cycle to not leak a dangling thread
         thread.exc = None
 
+    def test_multithread_modify_file_noerror(self):
+        # See issue25872
+        def modify_file():
+            with open(test.support.TESTFN, 'w', encoding='utf-8') as fp:
+                fp.write(' ')
+                traceback.format_stack()
+
+        self.addCleanup(unlink, test.support.TESTFN)
+        threads = [
+            threading.Thread(target=modify_file)
+            for i in range(100)
+        ]
+        for t in threads:
+            t.start()
+            t.join()
+
 
 class ThreadRunFail(threading.Thread):
     def run(self):



More information about the Python-checkins mailing list