| 1 |
import gc |
| 2 |
import linecache |
| 3 |
from textwrap import dedent |
| 4 |
import weakref |
| 5 |
|
| 6 |
def test_linecache_race(): |
| 7 |
|
| 8 |
|
| 9 |
def gen_func(n): |
| 10 |
func_code = dedent(""" |
| 11 |
def func(): |
| 12 |
pass |
| 13 |
""") |
| 14 |
g = {} |
| 15 |
exec(func_code, g, g) |
| 16 |
func = g['func'] |
| 17 |
|
| 18 |
filename = f"<generated-{n}>" |
| 19 |
linecache.cache[filename] = (len(func_code), None, func_code.splitlines(True), filename) |
| 20 |
|
| 21 |
def cleanup_linecache(filename): |
| 22 |
def _cleanup(): |
| 23 |
if filename in linecache.cache: |
| 24 |
del linecache.cache[filename] |
| 25 |
return _cleanup |
| 26 |
|
| 27 |
weakref.finalize(func, cleanup_linecache(filename)) |
| 28 |
|
| 29 |
return func |
| 30 |
|
| 31 |
|
| 32 |
for n in range(10_000): |
| 33 |
func = gen_func(n) |
| 34 |
del func |
| 35 |
linecache.checkcache() |
| 36 |
if n % 1000 == 0: |
| 37 |
|
| 38 |
pass |