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

Avoid raising OverflowError in len() when __len__() returns negative large value #74025

Closed
Tracked by #74019
serhiy-storchaka opened this issue Mar 17, 2017 · 5 comments
Assignees
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 29839
Nosy @warsaw, @terryjreedy, @vstinner, @serhiy-storchaka, @orenmn
PRs
  • bpo-29839: Raise ValueError rather than OverflowError in len() for negative values. #701
  • 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 2017-04-16.07:05:36.437>
    created_at = <Date 2017-03-17.19:52:14.485>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Avoid raising OverflowError in len() when __len__() returns negative large value'
    updated_at = <Date 2017-04-16.07:05:36.434>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2017-04-16.07:05:36.434>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2017-04-16.07:05:36.437>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2017-03-17.19:52:14.485>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29839
    keywords = []
    message_count = 5.0
    messages = ['289779', '289782', '290109', '291740', '291742']
    nosy_count = 5.0
    nosy_names = ['barry', 'terry.reedy', 'vstinner', 'serhiy.storchaka', 'Oren Milman']
    pr_nums = ['701']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29839'
    versions = ['Python 3.7']

    @serhiy-storchaka
    Copy link
    Member Author

    For now len() raises ValueError if __len__() returns small negative integer and OverflowError if __len__() returns large negative integer.

    >>> class NegativeLen:
    ...     def __len__(self):
    ...         return -10
    ... 
    >>> len(NegativeLen())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: __len__() should return >= 0
    >>> class HugeNegativeLen:
    ...     def __len__(self):
    ...         return -sys.maxsize-10
    ... 
    >>> len(HugeNegativeLen())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: cannot fit 'int' into an index-sized integer

    Proposed patch makes it always raising ValueError.

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Mar 17, 2017
    @warsaw
    Copy link
    Member

    warsaw commented Mar 17, 2017

    I was going to say that this is an API change, but given that without this, folks would have to catch both exceptions and now only have to catch one of them, it isn't.

    @terryjreedy
    Copy link
    Member

    https://docs.python.org/3/library/functions.html#len * does not specify the exception. In such cases, we occasionally change exception in x.y.0 releases without prior notice other than News and What's New. Also, I think unnecessarily exposing a compile-switch dependent internal detail, as now, is almost a bug. So +1 to applying in 3.7

    • The doc does not specify that 'length' cannot be non-negative. Perhaps it should, so no-one will think that they can hijack '__len__' to return something that is not a length, as usually understood.

    @serhiy-storchaka serhiy-storchaka self-assigned this Apr 7, 2017
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset baf9f29 by Serhiy Storchaka in branch 'master':
    bpo-29839: Raise ValueError rather than OverflowError in len() for negative values. (#701)
    baf9f29

    @serhiy-storchaka
    Copy link
    Member Author

    The doc does not specify that 'length' cannot be non-negative.

    It does. https://docs.python.org/3/reference/datamodel.html#object.\_\_len__

    .. method:: object.__len__(self)

    Called to implement the built-in function :func:`len`. Should return the length
    of the object, an integer ``>=`` 0.

    @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
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants