Skip to content

Fix itertools.groupby grouper returning a Java null when the key's __eq__ advances it - #1325

Open
taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH1276-groupby-eq-advances-grouper
Open

taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH1276-groupby-eq-advances-grouper

Conversation

@taxor03

@taxor03 taxor03 commented Nov 19, 2026

Copy link
Copy Markdown

Description

Fixes #1276

When the key's __eq__ advances the same group iterator while the grouper compares keys (the case from bpo-30347), the nested next() takes the current value and sets it to null, and the outer next() then returned that Java null. Using it crashed the process with a NullPointerException. CPython's _grouper_next reads currvalue after the comparison too, and returns NULL without an exception when it is gone, which ends the iteration (itertoolsmodule.c#L725-L734).

AS-IS

>>> # the program from #1276
0
java.lang.NullPointerException: Null receiver values are not supported by libraries.

TO-BE

0
None
done

(matching CPython)

Changes

  • In GrouperBuiltins.NextNode, treat a null current value after the key comparison as exhaustion.
  • Add test_groupby_eq_advances_grouper to tests/test_itertools.py: the outer next() returns its default and the nested one gets the next value, as in CPython 3.12, 3.13 and 3.14.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest test_itertools (9 passed) and --tagged test_itertools (124 passed). The program from #1276 prints 0, None, done like CPython. The new test crashes the 25.6.0 dev build with the NullPointerException. mx checkstyle --primary and mx eclipseformat --primary report nothing.

…s `__eq__` advances it

Signed-off-by: Sungmin Park <ryan040@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 19, 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.

[Bug]: itertools.groupby group iterator re-entered from the key's __eq__ returns a Java null, which crashes with NullPointerException

1 participant