Repository navigation
Forbid instantiating views and iterators with Py_TPFLAGS_DISALLOW_INSTANTIATION - #1595
Merged
asvetlov merged 1 commit intoSep 25, 2026
Conversation
…TANTIATION Replace the custom tp_new slots that raised TypeError with the type flag CPython provides for this since 3.10, and drop the dead 3.10 version guards around Py_TPFLAGS_IMMUTABLETYPE. Add a test that the types stay final.
asvetlov
marked this pull request as ready for review
September 25, 2026 20:35
Contributor
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1595 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 41 41
Lines 7856 7867 +11
Branches 652 653 +1
=========================================
+ Hits 7856 7867 +11
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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
The C view types (
_ItemsView,_KeysView,_ValuesView) and the three iterator types blocked direct instantiation with hand-writtentp_newslots that just raisedTypeError. CPython has providedPy_TPFLAGS_DISALLOW_INSTANTIATIONfor exactly this since 3.10, the project minimum, so the slots and their helpers are replaced by the flag. The deadPY_VERSION_HEX >= 3.10guards aroundPy_TPFLAGS_IMMUTABLETYPEin 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 directlybecame CPython'scannot create '..._KeysView' instances. On 3.10,T.__new__(T)is refused byobject.__new__'s safety check withobject.__new__(T) is not safeinstead.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
CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Tests, rebased on 27fec2d:
MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions), 3.10.17, clean tree copy: 1301 passed, 17 skippedLint: pre-commit on the changed files passed.
make doc-spellingpassed.Not verified locally: PyPy (whether cpyext honours
Py_TPFLAGS_DISALLOW_INSTANTIATION).