Skip to content

Do not swallow errors raised while sizing an update argument - #1517

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/multidict-issue-1306-beaa44
Sep 22, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:claude/multidict-issue-1306-beaa44

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

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

  • I think the code is well written
  • Unit tests for the changes exist
  • 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
  • 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.

Agent run details (optional, for reviewers)

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.

_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.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 22, 2026
@asvetlov
asvetlov marked this pull request as ready for review September 22, 2026 19:21
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 22, 2026 19:21
@codspeed

codspeed Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 298 untouched benchmarks


Comparing asvetlov:claude/multidict-issue-1306-beaa44 (d8a8e35) with master (3906932)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

Safe to merge.

Reviews (2) · Last reviewed commit: "Cover subclass construction and reword t..."

Comment thread CHANGES/1517.bugfix.rst Resolved
Comment thread tests/test_multidict.py Resolved
@greptile-apps

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

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3906932) to head (d8a8e35).

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     
Flag Coverage Δ
CI-GHA 100.00% <100.00%> (ø)
pytest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@asvetlov
asvetlov merged commit d647f3e into aio-libs:master Sep 22, 2026
61 checks passed
@asvetlov
asvetlov deleted the claude/multidict-issue-1306-beaa44 branch September 22, 2026 19:55
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