Skip to content

Commit 85361fa

Browse files
committed
Move ContextVar cache regression into the contextvars snippet
Preserve the fresh and copied Context lifetime checks, defaults and missing-value assertions while removing the redundant Rust interpreter wrapper. Assisted-by: Codex:model-version-unavailable
1 parent 15cb422 commit 85361fa

2 files changed

Lines changed: 32 additions & 42 deletions

File tree

‎extra_tests/snippets/stdlib_contextvars.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,35 @@ def __eq__(self, other):
8484
for var, value in copy_context().items()
8585
if var.name == "child_marker"
8686
] == []
87+
88+
89+
def context_cache_does_not_outlive_its_context():
90+
import contextvars
91+
92+
for factory_name in ("new", "copy"):
93+
for _ in range(100):
94+
var = contextvars.ContextVar("var", default="default")
95+
empty = contextvars.Context()
96+
ctx = contextvars.Context() if factory_name == "new" else empty.copy()
97+
ctx.run(var.set, "cached")
98+
del ctx
99+
ctx = contextvars.Context() if factory_name == "new" else empty.copy()
100+
assert list(ctx.items()) == []
101+
assert ctx.run(var.get) == "default"
102+
assert ctx.run(var.get, "explicit default") == "explicit default"
103+
104+
missing = contextvars.ContextVar("missing")
105+
ctx.run(missing.set, "cached")
106+
del ctx
107+
ctx = contextvars.Context() if factory_name == "new" else empty.copy()
108+
try:
109+
ctx.run(missing.get)
110+
except LookupError:
111+
pass
112+
else:
113+
raise AssertionError(
114+
"an empty context returned a destroyed context value"
115+
)
116+
117+
118+
context_cache_does_not_outlive_its_context()

‎tests/context_cache.rs‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)