8000 bpo-25872: Add unit tests for linecache and threading (GH-25913) · python/cpython@115dea9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 115dea9

Browse files
authored
bpo-25872: Add unit tests for linecache and threading (GH-25913)
1 parent 834498e commit 115dea9

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
@@ -239,5 +239,47 @@ def raise_memoryerror(*args, **kwargs):
239239
self.assertEqual(linecache.getlines(FILENAME), lines)
240240

241241

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

Lib/test/test_threading.py

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

55
import test.support
66
from test.support import threading_helper
7-
from test.support import verbose, cpython_only
7+
from test.support import verbose, cpython_only, os_helper
88
from test.support.import_helper import import_module
99
from test.support.script_helper import assert_python_ok, assert_python_failure
1010

@@ -19,6 +19,7 @@
1919
import subprocess
2020
import signal
2121
import textwrap
22+
import traceback
2223

2324
from unittest import mock
2425
from test import lock_tests
@@ -1345,6 +1346,22 @@ def run(self):
13451346
# explicitly break the reference cycle to not leak a dangling thread
13461347
thread.exc = None
13471348

1349+
def test_multithread_modify_file_noerror(self):
1350+
# See issue25872
1351+
def modify_file():
1352+
with open(os_helper.TESTFN, 'w', encoding='utf-8') as fp:
1353+
fp.write(' ')
1354+
traceback.format_stack()
1355+
1356+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
1357+
threads = [
1358+
threading.Thread(target=modify_file)
1359+
for i in range(100)
1360+
]
1361+
for t in threads:
1362+
t.start()
1363+
t.join()
1364+
13481365

13491366
class ThreadRunFail(threading.Thread):
13501367
def run(self):

0 commit comments

Comments
 (0)
0