Skip to content

Fix sum() rejecting a str or bytes start before getting the iterator - #1417

Open
deronnax wants to merge 1 commit into
oracle:masterfrom
deronnax:fix/sum-iter-before-start
Open

deronnax wants to merge 1 commit into
oracle:masterfrom
deronnax:fix/sum-iter-before-start

Conversation

@deronnax

@deronnax deronnax commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Description

Part of #1360 (item 4).

SumFunctionNode.sum (BuiltinFunctions.java#L1877-L1886) rejects a str, bytes or bytearray start before it calls PyObjectGetIter. CPython's builtin_sum_impl gets the iterator first (bltinmodule.c#L2519-L2536), so an error from iter() wins.

AS-IS

>>> class RaisingIter:
...     def __iter__(self): raise KeyError('iter')
>>> sum(RaisingIter(), 'x')
TypeError: sum() can't sum strings [use ''.join(seq) instead]
>>> sum(RaisingIter(), b'x')
TypeError: sum() can't sum bytes [use b''.join(seq) instead]

TO-BE

>>> sum(RaisingIter(), 'x')
KeyError: 'iter'
>>> sum(RaisingIter(), b'x')
KeyError: 'iter'
>>> sum([], 'x')
TypeError: sum() can't sum strings [use ''.join(seq) instead]

(matching CPython 3.12 and 3.13)

Changes

  • Get the iterator before checking start in SumFunctionNode.sum.
  • Add a test to tests/test_sum.py.

Testing

  • mx graalpytest test_sum test_list test_tuple test_bytes: 219 passed.
  • mx graalpytest --tagged test_list test_tuple test_builtin test_bytes test_userlist: 528 passed.
  • The new test also passes on CPython 3.12, 3.13 and 3.15.

Model: Claude Opus 5.5 — effort: high

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Dec 4, 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

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant