Repository navigation
Do not swallow non-TypeError exceptions in feature-detection fallbacks - #1382
Merged
Merged
Conversation
Several probes cleared *every* pending exception when a fallback path was taken: view richcompare and items-view __contains__ measuring len(other), items-view __contains__ computing the identity, MultiDict.__eq__ probing keys(), and extend()/ctor probing items(). A MemoryError or KeyboardInterrupt raised by the user object was silently discarded. Guard each with PyErr_ExceptionMatches() so only the expected TypeError/AttributeError is cleared and anything else propagates -- the same pattern the sibling sites in views.h already use. Adds a C-extension regression test.
…drop unused ignore)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1382 +/- ##
=======================================
Coverage 99.86% 99.86%
=======================================
Files 29 29
Lines 3710 3728 +18
Branches 275 275
=======================================
+ Hits 3705 3723 +18
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:
|
asvetlov
approved these changes
Sep 5, 2026
asvetlov
reviewed
Sep 5, 2026
Contributor
Confidence Score: 5/5Safe to merge: the changed C-extension fallback paths preserve compatibility behavior and propagate unexpected exceptions. No blocking failure remains.
What T-Rex did
Reviews (2): Last reviewed commit: "Update tests/test_multidict.py" | Re-trigger Greptile |
asvetlov
reviewed
Sep 5, 2026
asvetlov
reviewed
Sep 5, 2026
Member
|
@devdanzin thanks! |
asvetlov
enabled auto-merge (squash)
September 5, 2026 15:39
5 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 22, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? `_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 simply went ahead with no size estimate. The fallback is now guarded with `PyErr_ExceptionMatches()`, so only the `TypeError` from an unusable `__length_hint__` is ignored. That is the same pattern the sibling probes picked up in #1382. `PyObject_LengthHint()` already absorbs a missing `__len__` or `__length_hint__` internally, so the only `TypeError` that reaches this branch is "`__length_hint__` must be an integer"; a broken hint costs the preallocation estimate and nothing else, so it stays ignored. C-extension only. The pure-Python backend reaches `__len__` through a plain truth test and already propagates, so this closes a divergence rather than creating one. Plain `MultiDict(arg)` is unaffected on either backend: its vectorcall path never probes the argument's length, so only `extend()`, `update()`, `merge()` and subclass construction ever reached the cleared branch. ## Are there changes in behavior for the user? Yes, in an edge case. `extend()`, `update()`, `merge()` and subclass construction now propagate a `MemoryError` or `KeyboardInterrupt` raised by the argument's `__len__` instead of swallowing it. Arguments without a usable 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 - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes (no public API change) - [ ] N/A, no `CONTRIBUTORS.txt` in this repo - [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 (Opus 5); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Behaviour of an argument whose `__len__` raises `MemoryError`, before and after, on both backends: | | `MultiDict(x)` | subclass ctor | extend/update/merge | | --- | --- | --- | --- | | C ext, before | ok | swallowed | swallowed | | C ext, after | ok | raises | raises | | pure Python | raises | raises | raises | The remaining `MultiDict(x)` column is a separate, pre-existing difference: `_multidict_vectorcall_impl()` never calls `PyObject_LengthHint()`, so there is no exception to swallow there. It is not an error-handling bug and is left alone here. Tests, CPython 3.13.12: - C extension build, `pytest tests -q`: 2198 passed, 21 skipped. - Pure-Python build, installed with `MULTIDICT_NO_EXTENSIONS=1` into a clean copy of the tree and run with `--no-c-extensions`: 1097 passed, 0 failed, 15 skipped. The same run on unmodified `master` gives 1083 passed, 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_exceptions` and `test_subclass_construction_does_not_swallow_exceptions` fail there on `cs-c` and `ci-c` and pass on the `-py` variants, so they do guard the behaviour 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. </details>
6 tasks done
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?
Several feature-detection fallbacks cleared every pending exception when a fallback path was taken:
view richcompare and items-view
__contains__measuringlen(other), items-view__contains__computing the identity,
MultiDict.__eq__probingkeys(), andextend()/constructor probingitems(). AMemoryErrororKeyboardInterruptraised by the user object was silently discarded. Eachis now guarded with
PyErr_ExceptionMatches()so only the expectedTypeError/AttributeErroriscleared and anything else propagates — the same pattern the sibling sites in
views.halready use.C-extension-only; the pure-Python backend propagates naturally.
Are there changes in behavior for the user?
Yes (edge case): an exception other than
TypeError/AttributeError(notablyMemoryError,KeyboardInterrupt) raised by an object being probed now propagates instead of being swallowed. Thenormal fallbacks (a non-sized or non-mapping object) are unchanged.
Is it a substantial burden for the maintainers to support this?
No. It applies the module's existing guard pattern to the few sites that missed it.
Related issue number
Findings from the C-extension analysis report #1306.
Checklist
test_non_typeerror_exceptions_are_not_swallowed)CONTRIBUTORS.txtin this repoCHANGES/folder