Skip to content

Forbid direct instantiation of iterator types#1377

Open
devdanzin wants to merge 4 commits into
aio-libs:masterfrom
devdanzin:fix/iterator-forbidden-new
Open

Forbid direct instantiation of iterator types#1377
devdanzin wants to merge 4 commits into
aio-libs:masterfrom
devdanzin:fix/iterator-forbidden-new

Conversation

@devdanzin

Copy link
Copy Markdown

What do these changes do?

type(iter(md.keys())).__new__(t) built an uninitialised iterator whose internal md pointer was NULL;
calling next() on it dereferenced NULL and segfaulted. The view types were given a forbidden tp_new
for exactly this reason in #1163; the iterator types share the hole. This adds the same forbidden
tp_new to the items/keys/values iterator specs. This is a C-extension-only concern (the pure-Python
iterators 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 directly instead of crashing. Normal iteration is
unaffected.

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

  • I think the code is well written
  • Unit tests for the changes exist (test_iter_direct_instantiation_segfault)
  • Documentation reflects the changes (no public API change)
  • N/A — no CONTRIBUTORS.txt in this repo
  • Add a new news fragment into the CHANGES/ folder

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.
@devdanzin
devdanzin requested a review from webknjaz as a code owner July 24, 2026 14:06
@devdanzin
devdanzin requested a review from asvetlov as a code owner July 24, 2026 14:06
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jul 24, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 242 untouched benchmarks


Comparing devdanzin:fix/iterator-forbidden-new (7ee49d1) with master (e71a593)

Open in CodSpeed

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

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant