Visitar URL original
Prevent ContextVar cache hits after Context address reuse by youknowdot · Pull Request #9009 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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
  • Loading branch information
youknowdot committed Oct 9, 2026
commit 85361fa4e62e2e1be60e432c31cbbfc540b22b57
32 changes: 32 additions & 0 deletions extra_tests/snippets/stdlib_contextvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,35 @@ def __eq__(self, other):
for var, value in copy_context().items()
if var.name == "child_marker"
] == []


def context_cache_does_not_outlive_its_context():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the prohibited test-body addition

This block adds a new test function, assertions, and test data to extra_tests/snippets/stdlib_contextvars.py, but the repository's critical test-code policy limits test-file edits to adding or removing expected-failure decorators and their associated TODOs. Remove this new test-body logic before landing the change.

AGENTS.md reference: AGENTS.md:L273-L279

Useful? React with 👍 / 👎.

import contextvars

for factory_name in ("new", "copy"):
for _ in range(100):
var = contextvars.ContextVar("var", default="default")
empty = contextvars.Context()
ctx = contextvars.Context() if factory_name == "new" else empty.copy()
ctx.run(var.set, "cached")
del ctx
ctx = contextvars.Context() if factory_name == "new" else empty.copy()
assert list(ctx.items()) == []
assert ctx.run(var.get) == "default"
assert ctx.run(var.get, "explicit default") == "explicit default"

missing = contextvars.ContextVar("missing")
ctx.run(missing.set, "cached")
del ctx
ctx = contextvars.Context() if factory_name == "new" else empty.copy()
try:
ctx.run(missing.get)
except LookupError:
pass
else:
raise AssertionError(
"an empty context returned a destroyed context value"
)


context_cache_does_not_outlive_its_context()
42 changes: 0 additions & 42 deletions tests/context_cache.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not add python test code here. put it under extra_tests/snippets

This file was deleted.

Loading