Avoid caching parso objects, fixes #1723

This commit is contained in:
Dave Halter
2021-01-14 00:29:34 +01:00
parent 44d77523b3
commit b9067ccdbb
6 changed files with 66 additions and 23 deletions

View File

@@ -1,5 +1,9 @@
import gc
from pathlib import Path
from jedi import parser_utils
from parso import parse
from parso.cache import parser_cache
from parso.python import tree
import pytest
@@ -67,3 +71,21 @@ def test_get_signature(code, signature):
if node.type == 'simple_stmt':
node = node.children[0]
assert parser_utils.get_signature(node) == signature
def test_parser_cache_clear(Script):
"""
If parso clears its cache, Jedi should not keep those resources, they
should be freed.
"""
script = Script("a = abs\na", path=Path(__file__).parent / 'parser_cache_test_foo.py')
script.complete()
module_id = id(script._module_node)
del parser_cache[script._inference_state.grammar._hashed][script.path]
del script
import jedi
jedi.parser_utils.get_cached_parent_scope.__closure__[0].cell_contents.clear()
gc.collect()
assert module_id not in [id(m) for m in gc.get_referrers(tree.Module)]