Repository navigation
Do not swallow errors raised while sizing an update argument - #1517
Merged
asvetlov merged 2 commits intoSep 22, 2026
Merged
Conversation
_multidict_extend_parse_args() cleared every pending exception when PyObject_LengthHint() failed on the positional argument, so a MemoryError or KeyboardInterrupt raised by that object's __len__ was discarded and the update went ahead with no size estimate. Guard the fallback with PyErr_ExceptionMatches() so only the TypeError from an unusable __length_hint__ is ignored, the same pattern the sibling probes already use. The pure-Python backend already propagates here, since it reaches __len__ through a plain truth test, so this removes a divergence for extend(), update(), merge() and subclass construction. Plain MultiDict(arg) is unaffected either way: its vectorcall path never probes the argument's length.
asvetlov
marked this pull request as ready for review
September 22, 2026 19:21
Contributor
|
This comment has been minimized.
This comment has been minimized.
Subclass construction is the one sizing entry point the new tests missed: it goes through tp_init() instead of the constructor vectorcall, so it estimates the argument size and previously swallowed the error. Add a case for it, parametrised over both backends. Reword the news fragment to lead with a past-tense verb, per the CHANGES convention.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1517 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 38 38
Lines 6381 6406 +25
Branches 579 579
=========================================
+ Hits 6381 6406 +25
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?
_multidict_extend_parse_args()cleared every pending exception whenPyObject_LengthHint()failed on the positional argument, so aMemoryErrororKeyboardInterruptraised by that object's__len__was discarded and the update simply went ahead with no size estimate.
The fallback is now guarded with
PyErr_ExceptionMatches(), so only theTypeErrorfrom an unusable__length_hint__is ignored. That is thesame pattern the sibling probes picked up in #1382.
PyObject_LengthHint()already absorbs a missing__len__or__length_hint__internally, so the onlyTypeErrorthat reaches thisbranch is "
__length_hint__must be an integer"; a broken hint coststhe preallocation estimate and nothing else, so it stays ignored.
C-extension only. The pure-Python backend reaches
__len__through aplain truth test and already propagates, so this closes a divergence
rather than creating one. Plain
MultiDict(arg)is unaffected on eitherbackend: its vectorcall path never probes the argument's length, so only
extend(),update(),merge()and subclass construction ever reachedthe cleared branch.
Are there changes in behavior for the user?
Yes, in an edge case.
extend(),update(),merge()and subclassconstruction now propagate a
MemoryErrororKeyboardInterruptraisedby the argument's
__len__instead of swallowing it. Arguments without ausable length, the common case for a generator, are unchanged.
Is it a substantial burden for the maintainers to support this?
No. It applies the module's existing guard pattern to the one site that
still missed it.
Related issue number
Findings from the C-extension analysis report #1306. This was the last
open item from that report; everything else in it is already fixed on
master, so #1306 can probably be closed once this lands.
Checklist
CONTRIBUTORS.txtin this repoCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Opus 5); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Behaviour of an argument whose
__len__raisesMemoryError, beforeand after, on both backends:
MultiDict(x)The remaining
MultiDict(x)column is a separate, pre-existingdifference:
_multidict_vectorcall_impl()never callsPyObject_LengthHint(), so there is no exception to swallow there. It isnot an error-handling bug and is left alone here.
Tests, CPython 3.13.12:
pytest tests -q: 2198 passed, 21 skipped.MULTIDICT_NO_EXTENSIONS=1into aclean copy of the tree and run with
--no-c-extensions: 1097 passed,0 failed, 15 skipped. The same run on unmodified
mastergives 1083passed, 0 failed, 15 skipped; the difference is exactly the new
pure-Python cases.
The new tests were checked against the unpatched parent commit built as
a C extension:
test_sizing_argument_does_not_swallow_exceptionsandtest_subclass_construction_does_not_swallow_exceptionsfail there oncs-candci-cand pass on the-pyvariants, so they do guard thebehaviour rather than passing vacuously.
New tests are parametrised over both backends, 28 cases. Checked the
coverage report for uncovered lines inside the new test bodies; an
unreachable
__iter__on the__len__-raising stub was dropped because__len__raises first.Lint:
pre-commit run --files ...on the touched files, ruff check,ruff format, clang-format, MyPy 3.13 and 3.11 all pass.
make doc-spelling: build succeeded.