Skip to content

Commit 466dafc

Browse files
committed
add test
1 parent cea6a1e commit 466dafc

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

‎tests/test_garbage_collection.py‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import gc
22
import platform
3-
from typing import Any
3+
from typing import Any, Iterable
44
from weakref import WeakValueDictionary
55

66
import pytest
@@ -79,3 +79,42 @@ class MyModel(BaseModel):
7979
gc.collect(2)
8080

8181
assert len(cache) == 0
82+
83+
84+
@pytest.mark.xfail(
85+
condition=platform.python_implementation() == 'PyPy', reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899'
86+
)
87+
def test_gc_validator_iterator() -> None:
88+
# test for https://github.com/pydantic/pydantic/issues/9243
89+
class MyModel:
90+
iter: Iterable[int]
91+
92+
v = SchemaValidator(
93+
core_schema.model_schema(
94+
MyModel,
95+
core_schema.model_fields_schema(
96+
{'iter': core_schema.model_field(core_schema.generator_schema(core_schema.int_schema()))}
97+
),
98+
),
99+
)
100+
101+
class MyIterable:
102+
def __iter__(self):
103+
return self
104+
105+
def __next__(self):
106+
raise StopIteration()
107+
108+
cache: 'WeakValueDictionary[int, Any]' = WeakValueDictionary()
109+
110+
for _ in range(10_000):
111+
iterable = MyIterable()
112+
cache[id(iterable)] = iterable
113+
v.validate_python({'iter': iterable})
114+
del iterable
115+
116+
gc.collect(0)
117+
gc.collect(1)
118+
gc.collect(2)
119+
120+
assert len(cache) == 0

0 commit comments

Comments
 (0)