Skip to content

Commit c05d8a6

Browse files
iritkatrieluniocto
andauthored
bpo-25872: Add unit tests for linecache and threading (GH-25913) (GH-26211)
(cherry picked from commit 115dea9) Co-authored-by: uniocto <[email protected]>
1 parent 049c412 commit c05d8a6

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Lib/test/test_linecache.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,47 @@ def raise_memoryerror(*args, **kwargs):
238238
self.assertEqual(linecache.getlines(FILENAME), lines)
239239

240240

241+
class LineCacheInvalidationTests(unittest.TestCase):
242+
def setUp(self):
243+
super().setUp()
244+
linecache.clearcache()
245+
self.deleted_file = support.TESTFN + '.1'
246+
self.modified_file = support.TESTFN + '.2'
247+
self.unchanged_file = support.TESTFN + '.3'
248+
249+
for fname in (self.deleted_file,
250+
self.modified_file,
251+
self.unchanged_file):
252+
self.addCleanup(support.unlink, fname)
253+
with open(fname, 'w', encoding='utf-8') as source:
254+
source.write(f'print("I am {fname}")')
255+
256+
self.assertNotIn(fname, linecache.cache)
257+
linecache.getlines(fname)
258+
self.assertIn(fname, linecache.cache)
259+
260+
os.remove(self.deleted_file)
261+
with open(self.modified_file, 'w', encoding='utf-8') as source:
262+
source.write('print("was modified")')
263+
264+
def test_checkcache_for_deleted_file(self):
265+
linecache.checkcache(self.deleted_file)
266+
self.assertNotIn(self.deleted_file, linecache.cache)
267+
self.assertIn(self.modified_file, linecache.cache)
268+
self.assertIn(self.unchanged_file, linecache.cache)
269+
270+
def test_checkcache_for_modified_file(self):
271+
linecache.checkcache(self.modified_file)
272+
self.assertIn(self.deleted_file, linecache.cache)
273+
self.assertNotIn(self.modified_file, linecache.cache)
274+
self.assertIn(self.unchanged_file, linecache.cache)
275+
276+
def test_checkcache_with_no_parameter(self):
277+
linecache.checkcache()
278+
self.assertNotIn(self.deleted_file, linecache.cache)
279+
self.assertNotIn(self.modified_file, linecache.cache)
280+
self.assertIn(self.unchanged_file, linecache.cache)
281+
282+
241283
if __name__ == "__main__":
242284
unittest.main()

Lib/test/test_threading.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import test.support
6-
from test.support import verbose, import_module, cpython_only
6+
from test.support import verbose, import_module, cpython_only, unlink
77
from test.support.script_helper import assert_python_ok, assert_python_failure
88

99
import random
@@ -17,6 +17,7 @@
1717
import subprocess
1818
import signal
1919
import textwrap
20+
import traceback
2021

2122
from test import lock_tests
2223
from test import support
@@ -1243,6 +1244,22 @@ def run(self):
12431244
# explicitly break the reference cycle to not leak a dangling thread
12441245
thread.exc = None
12451246

1247+
def test_multithread_modify_file_noerror(self):
1248+
# See issue25872
1249+
def modify_file():
1250+
with open(test.support.TESTFN, 'w', encoding='utf-8') as fp:
1251+
fp.write(' ')
1252+
traceback.format_stack()
1253+
1254+
self.addCleanup(unlink, test.support.TESTFN)
1255+
threads = [
1256+
threading.Thread(target=modify_file)
1257+
for i in range(100)
1258+
]
1259+
for t in threads:
1260+
t.start()
1261+
t.join()
1262+
12461263

12471264
class ThreadRunFail(threading.Thread):
12481265
def run(self):

0 commit comments

Comments
 (0)