Repository navigation
Forbid direct instantiation of iterator types - #1377
Conversation
type(iter(md.keys())).__new__(t) built an uninitialised iterator whose md pointer was NULL; calling next() on it dereferenced NULL and segfaulted. The view types were given a forbidden tp_new in aio-libs#1163 for the same reason (aio-libs#1161); the iterator types share the hole. Add the same forbidden tp_new to the items/keys/values iterator specs. Adds a C-extension regression test mirroring the view one.
Confidence Score: 5/5No blocking failure remains. Direct construction was exercised for keys, items, and values iterators, and each call raised
What T-Rex did
Reviews (4): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile |
|
@aiolibsbot rr |
PR Review — Forbid direct instantiation of iterator typesThe C fix is correct and complete; one stray line in the test file silently un-marks a neighbouring test and will break the pure-Python CI leg. The C-side change is exactly right and I verified the parts that matter. The PR's dual-backend claim also checks out: pure-Python
🟡 Important
1. Missing `@` turns the marker into a no-op and un-marks the neighbouring test
|
Rebase with requested adjustmentsBranch Warning Force-push safety check — the rewrite of Commits whose patch changed in-flight and whose content could not be verified as preserved — check these survived:
Previous PR head: Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
|
@aiolibsbot rv |
|
@aiolibsbot rv |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1377 +/- ##
=======================================
Coverage 99.86% 99.86%
=======================================
Files 29 29
Lines 3756 3763 +7
Branches 279 280 +1
=======================================
+ Hits 3751 3758 +7
Misses 3 3
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@devdanzin thanks again! |
…TANTIATION (#1595) <!-- Thank you for your contribution! --> ## What do these changes do? The C view types (`_ItemsView`, `_KeysView`, `_ValuesView`) and the three iterator types blocked direct instantiation with hand-written `tp_new` slots that just raised `TypeError`. CPython has provided `Py_TPFLAGS_DISALLOW_INSTANTIATION` for exactly this since 3.10, the project minimum, so the slots and their helpers are replaced by the flag. The dead `PY_VERSION_HEX >= 3.10` guards around `Py_TPFLAGS_IMMUTABLETYPE` in the same specs are dropped too. The types were already final (no `Py_TPFLAGS_BASETYPE`); a new test locks that in. The pure-Python backend has no equivalent types, so it is unaffected. ## Are there changes in behavior for the user? Only the error text: `cannot create '..._KeysView' instances directly` became CPython's `cannot create '..._KeysView' instances`. On 3.10, `T.__new__(T)` is refused by `object.__new__`'s safety check with `object.__new__(T) is not safe` instead. ## Is it a substantial burden for the maintainers to support this? No, it removes code. ## Related issue number Follow-up to #1163 and #1377. ## Checklist - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes (N/A, private types) - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` (N/A) - [x] Add a new news fragment into the `CHANGES/` folder - [x] `make doc-spelling` passes and any new technical words are added to `docs/spelling_wordlist.txt` Drafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Tests, rebased on 27fec2d: - C extension, CPython 3.10.17: 2771 passed, 170 skipped - C extension, CPython 3.14.7t: 2758 passed, 183 skipped - Pure Python (`MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions`), 3.10.17, clean tree copy: 1301 passed, 17 skipped Lint: pre-commit on the changed files passed. `make doc-spelling` passed. Not verified locally: PyPy (whether cpyext honours `Py_TPFLAGS_DISALLOW_INSTANTIATION`). </details>
What do these changes do?
type(iter(md.keys())).__new__(t)built an uninitialised iterator whose internalmdpointer was NULL;calling
next()on it dereferenced NULL and segfaulted. The view types were given a forbiddentp_newfor exactly this reason in #1163; the iterator types share the hole. This adds the same forbidden
tp_newto the items/keys/values iterator specs. This is a C-extension-only concern (the pure-Pythoniterators are ordinary Python objects); no pure-Python change is needed.
Are there changes in behavior for the user?
Yes: directly constructing an internal iterator type now raises
TypeError: cannot create '..._keysiter' instances directlyinstead of crashing. Normal iteration isunaffected.
Is it a substantial burden for the maintainers to support this?
No. It mirrors the existing view-type fix (#1163).
Related issue number
Direct analog of #1163 (view types); same crash class as #1161. Surfaced by the analysis report #1306.
Checklist
test_iter_direct_instantiation_segfault)CONTRIBUTORS.txtin this repoCHANGES/folder