Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

itertools.count() falls back to fast (integer) mode when step rounds to 1 #72206

Closed
StyXman mannequin opened this issue Sep 8, 2016 · 6 comments
Closed

itertools.count() falls back to fast (integer) mode when step rounds to 1 #72206

StyXman mannequin opened this issue Sep 8, 2016 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@StyXman
Copy link
Mannequin

StyXman mannequin commented Sep 8, 2016

BPO 28019
Nosy @rhettinger, @facundobatista, @serhiy-storchaka
Files
  • fix_28019.diff
  • fix_28019.diff
  • itertools_count_truncated_step.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2016-09-10.07:11:34.570>
    created_at = <Date 2016-09-08.12:48:32.348>
    labels = ['type-bug', 'library']
    title = 'itertools.count() falls back to fast (integer) mode when step rounds to 1'
    updated_at = <Date 2016-09-10.07:11:34.569>
    user = 'https://bugs.python.org/StyXman'

    bugs.python.org fields:

    activity = <Date 2016-09-10.07:11:34.569>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-09-10.07:11:34.570>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2016-09-08.12:48:32.348>
    creator = 'StyXman'
    dependencies = []
    files = ['44466', '44467', '44471']
    hgrepos = []
    issue_num = 28019
    keywords = ['patch']
    message_count = 6.0
    messages = ['275007', '275016', '275025', '275038', '275082', '275583']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'facundobatista', 'StyXman', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28019'
    versions = ['Python 3.5', 'Python 3.6']

    @StyXman
    Copy link
    Mannequin Author

    StyXman mannequin commented Sep 8, 2016

    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

    @StyXman StyXman mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 8, 2016
    @StyXman
    Copy link
    Mannequin Author

    StyXman mannequin commented Sep 8, 2016

    Here's a first approach on fixing this bug. I'm not sure how to handle the case where step=1.0.

    @facundobatista
    Copy link
    Member

    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).

    @rhettinger rhettinger self-assigned this Sep 8, 2016
    @StyXman
    Copy link
    Mannequin Author

    StyXman mannequin commented Sep 8, 2016

    New patch following comments from SilentGhost.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 10, 2016

    New changeset b23f963a7b45 by Serhiy Storchaka in branch '3.5':
    Issue bpo-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 bpo-28019: Backported additional tests for itertools.count().
    https://hg.python.org/cpython/rev/74667320778e

    New changeset 51dfab4f28e7 by Serhiy Storchaka in branch 'default':
    Issue bpo-28019: itertools.count() no longer rounds non-integer step in range
    https://hg.python.org/cpython/rev/51dfab4f28e7

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants