Skip to content

Fix itertools.count() keeping an int subclass start with step 1 - #1358

Open
jseop-lim wants to merge 2 commits into
oracle:masterfrom
jseop-lim:fix/itertools-count-exact-int-start
Open

jseop-lim wants to merge 2 commits into
oracle:masterfrom
jseop-lim:fix/itertools-count-exact-int-start

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Part of #1291 (item 13).

CountNode.construct stores start as passed. CPython's itertools_count_impl keeps an int start that fits in Py_ssize_t as a C integer when the step is an int equal to 1, so it produces exact ints.

AS-IS

>>> c = itertools.count(True)
>>> c
count(True)
>>> next(c)
True

TO-BE

>>> c = itertools.count(True)
>>> c
count(1)
>>> next(c)
1

(matching CPython)

Changes

  • In construct, when start and step are int instances, step == 1 and start fits in a long, store start as a long. The step is then stored as an exact int 1 too, so the iteration never calls a subclass's __add__ or __radd__. count(True, 2) still yields True.
  • Add a test suite to tests/test_itertools.py.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest on test_itertools.py (the new test fails on the unpatched build and passes with the fix) and the tagged test_itertools (124 passed). mx python-checkcopyrights, mx eclipseformat and mx checkstyle are clean. This touches the constructor that #1262 changes, so whichever lands second needs a rebase.

count(True) kept the bool, so its repr was count(True) and the first
value was True. CPython keeps an int start with step 1 as a C integer
and produces exact ints.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
With an exact start, adding a step that overrides __radd__ called the
override. CPython ignores the step object in this mode.

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