classification
Title: itertools.count() falls back to fast (integer) mode when step rounds to 1
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: StyXman, facundobatista, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-09-08 12:48 by StyXman, last changed 2016-09-10 07:11 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
fix_28019.diff StyXman, 2016-09-08 14:22 review
fix_28019.diff StyXman, 2016-09-08 15:08 review
itertools_count_truncated_step.patch serhiy.storchaka, 2016-09-08 17:57 review
Messages (6)
msg275007 - (view) Author: Marcos Dione (StyXman) * Date: 2016-09-08 12:48
If the `step` parameter for `itertools.count()` rounds to 1 (f.i., 1.1, 1.437643, 1.99999), then it fallsback to fast (integer) mode and increases the counter by 1. Here's an example:

Python 3.6.0a4+ (default:ddc95a9bc2e0+, Sep  8 2016, 14:46:19)
>>> import itertools
>>> for i in itertools.count(1, step=1.5):
...     print(i)
...     if i > 10:
...         break
1
2
3
4
5
6
7
8
9
10
11
msg275016 - (view) Author: Marcos Dione (StyXman) * Date: 2016-09-08 14:22
Here's a first approach on fixing this bug. I'm not sure how to handle the case where step=1.0.
msg275025 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2016-09-08 14:43
I think the fix nails it; all the problem was that the "fast" mode was wrongly detected, and all the problems (counting badly, or a bad repr, etc) is a problem after setting cnt into PY_SSIZE_T_MAX.

IIUC there is nothing special to do when step=1.0, as later on the original objects are used (they don't suffer a wrong cast to integer anymore).
msg275038 - (view) Author: Marcos Dione (StyXman) * Date: 2016-09-08 15:08
New patch following comments from SilentGhost.
msg275082 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-08 17:57
PyLong_AsLong() not just converts Python integer to C long, but it also converts Python float to Python integer (with the lost of the fractional part). We need to check that the value is Python integer.

Here is a patch that fixes this issue and few other bugs.
msg275583 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-09-10 06:54
New changeset b23f963a7b45 by Serhiy Storchaka in branch '3.5':
Issue #28019: itertools.count() no longer rounds non-integer step in range
https://hg.python.org/cpython/rev/b23f963a7b45

New changeset 74667320778e by Serhiy Storchaka in branch '2.7':
Issue #28019: Backported additional tests for itertools.count().
https://hg.python.org/cpython/rev/74667320778e

New changeset 51dfab4f28e7 by Serhiy Storchaka in branch 'default':
Issue #28019: itertools.count() no longer rounds non-integer step in range
https://hg.python.org/cpython/rev/51dfab4f28e7
History
Date User Action Args
2016-09-10 07:11:34serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2016-09-10 06:54:45python-devsetnosy: + python-dev
messages: + msg275583
2016-09-08 20:51:43rhettingersetassignee: rhettinger -> serhiy.storchaka
2016-09-08 17:57:08serhiy.storchakasetfiles: + itertools_count_truncated_step.patch
versions: - Python 3.3, Python 3.4
nosy: + serhiy.storchaka

messages: + msg275082

stage: commit review -> patch review
2016-09-08 16:08:23StyXmansetversions: + Python 3.3, Python 3.4
2016-09-08 15:43:00SilentGhostsetstage: patch review -> commit review
2016-09-08 15:08:25StyXmansetfiles: + fix_28019.diff

messages: + msg275038
2016-09-08 14:59:26rhettingersetassignee: rhettinger
2016-09-08 14:43:21facundobatistasetnosy: + facundobatista
messages: + msg275025
2016-09-08 14:34:26SilentGhostsetstage: patch review
versions: - Python 3.3, Python 3.4
2016-09-08 14:25:15StyXmansetversions: + Python 3.3, Python 3.4
2016-09-08 14:22:46StyXmansetfiles: + fix_28019.diff
keywords: + patch
messages: + msg275016
2016-09-08 12:56:03facundobatistasetversions: + Python 3.5
2016-09-08 12:51:07serhiy.storchakasetnosy: + rhettinger
2016-09-08 12:48:32StyXmancreate