GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid raising OverflowError in len() when __len__() returns negative large value · Issue #74025 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Avoid raising OverflowError in len() when __len__() returns negative large value#74025
Copy link
Assignees
serhiy-storchaka
Labels
3.7 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Mar 17, 2017
Issue body actions
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']

    Activity

    1. serhiy-storchaka commented on Mar 17, 2017

      @serhiy-storchaka
      serhiy-storchaka
      on Mar 17, 2017
      MemberAuthor
      More actions

      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.

    2. serhiy-storchaka
      added
      3.7 (EOL)end of lifeend of life
      interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
      type-featureA feature request or enhancementA feature request or enhancement
      on Mar 17, 2017
    3. warsaw commented on Mar 17, 2017

      @warsaw
      warsaw
      on Mar 17, 2017
      Member
      More actions

      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.

    4. terryjreedy commented on Mar 24, 2017

      @terryjreedy
      terryjreedy
      on Mar 24, 2017
      Member
      More actions

      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.
    5. serhiy-storchaka
      self-assigned this
      on Apr 7, 2017
    6. serhiy-storchaka commented on Apr 16, 2017

      @serhiy-storchaka
      serhiy-storchaka
      on Apr 16, 2017
      MemberAuthor
      More actions

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

    7. serhiy-storchaka commented on Apr 16, 2017

      @serhiy-storchaka
      serhiy-storchaka
      on Apr 16, 2017
      MemberAuthor
      More actions

      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.

    8. serhiy-storchaka
      closed this as completedon Apr 16, 2017
    9. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @serhiy-storchaka
      serhiy-storchaka

    Labels

    3.7 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.