Skip to content

gh-162618: Remove collected members after WeakSet set operations - #162690

Open
RealBhupesh wants to merge 1 commit into
python:mainfrom
RealBhupesh:fix/weakset-collected-set-operations
Open

RealBhupesh wants to merge 1 commit into
python:mainfrom
RealBhupesh:fix/weakset-collected-set-operations

Conversation

@RealBhupesh

@RealBhupesh RealBhupesh commented Nov 26, 2026 •

Copy link
Copy Markdown

Fixes #162618.

WeakSet.intersection_update() can install right-hand weakrefs without removal callbacks. symmetric_difference_update() can stage weakrefs whose temporary referents die before the destination is updated. Both paths can leave dead entries counted by len() but absent from iteration.

Keep selected referents alive until callback-bearing refs are installed:

  • Intersection stages only matches and preserves the existing lazy early stop.
  • Symmetric difference stages the first referent for each equal key before toggling membership.
  • Staged strong references are released in finally; regression tests cover collection, equal-but-distinct members, temporary iterables, duplicate inputs and subclass return types.

Compatibility notes: intersection still selects the right-hand referent for equal iterable members, but now replaces the private data set object. XOR toggle order can differ with dictionary staging. Subclasses relying on private backing-set identity and other side-effectful hash/equality behavior beyond the checked cases remain unverified.

Validation

On Linux x86-64, fresh CPython 3.16.0a2+dev builds from 572ec16e51c5247db38dce85193d83c11a5d280a:

  • Base plus new tests: five assertion failures, including both symmetric-difference entry points. The equal-representative XOR control already passed.
  • Patched WeakSet and weakref suites: 202 tests, 4 skipped, success.
  • Combined focused and adjacent run: 11 test files, 3,587 tests, 84 skipped, success.
  • Separate verification accounted for all 564 default-suite files as passed or skipped across batches and individual retries. Execution interruptions required retries; this was not one uninterrupted run. test_httpservers failed once in a batch whose original failure output was not retained. Its cause is unknown; an isolated rerun passed.
  • Additional probes checked lazy consumption, failed-input state, self operations, subclass dispatch, equal-member selection, set-oracle comparisons and collection during equality.
  • make patchcheck, whitespace and patch-applicability checks passed.

Limits: default resources only, 15 optional build modules unavailable, Linux only, no debug/refleak or free-threaded build. No suite-wide aggregate test count was retained for the full default-suite batches. Scoped test counts above come from their individual run logs.

AI tools assisted implementation and testing. The patch was checked separately before submission. This is not a claim of personal manual authorship or a claim that every build configuration has passed.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T15:20:16.989753Z 258e2a4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WeakSet &= and ^= leave dead references behind

1 participant