Fix the IndexError message of the pure Python UnknownFieldSet by ManoharPaturi · Pull Request #30445 · protocolbuffers/protobuf · GitHub
Skip to content

Fix the IndexError message of the pure Python UnknownFieldSet - #30445

Open
ManoharPaturi wants to merge 1 commit into
protocolbuffers:mainfrom
ManoharPaturi:fix-unknown-fields-indexerror
Open

ManoharPaturi wants to merge 1 commit into
protocolbuffers:mainfrom
ManoharPaturi:fix-unknown-fields-indexerror

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

The bounds check in UnknownFieldSet.getitem in python/google/protobuf/unknown_fields.py built its error message with string attribute access rather than formatting:

raise IndexError('index %d out of range'.index)

Because '.index' evaluates to the bound method str.index of the literal, an out of range lookup surfaced a message such as

index <built-in method index of str object at 0x1016c9f30> out of range

instead of naming the index. The fix is the intended % formatting with the index value.

Testing: added testIndexErrorMessages to python/google/protobuf/internal/unknown_fields_test.py, which performs lookups at negative and positive out of range offsets and asserts the rendered message. Under the pure Python implementation the message now reads exactly "index N out of range" with the wrapped value for negative offsets; under the C++ implementation the test only asserts that an IndexError mentioning "out of range" is raised, since that side formats its own message. The full unknown_fields_test.py suite passes under PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (23 tests locally).

The bounds check in UnknownFieldSet.__getitem__ formatted its message
with '...'.index, which evaluates to the bound method str.index instead
of the requested index, so out of range lookups surfaced a message like
"index <built-in method index of str object at 0x...> out of range".
Use % formatting and add a test asserting the rendered message.
@ManoharPaturi
ManoharPaturi requested a review from a team as a code owner October 4, 2026 02:58
@ManoharPaturi
ManoharPaturi requested review from anandolee and removed request for a team October 4, 2026 02:58
@sbenzaquen sbenzaquen added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Oct 13, 2026
@github-actions github-actions Bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Oct 13, 2026

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants