Skip to content

Replace OverflowError with ValueError when calculating length of range objects > PY_SIZE_MAX #80733

Closed

Description

@tonybaloney
BPO 36552
Nosy @serhiy-storchaka, @zooba, @tonybaloney, @tonybaloney
PRs
  • bpo-36552: Change OverflowError to a ValueError for range objects > PY_SIZE_MAX #12720
  • 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/tonybaloney'
    closed_at = <Date 2019-05-09.05:59:19.404>
    created_at = <Date 2019-04-08.04:38:16.568>
    labels = ['3.8', 'library']
    title = 'Replace OverflowError with ValueError when calculating length of range objects > PY_SIZE_MAX'
    updated_at = <Date 2019-05-09.05:59:19.403>
    user = 'https://github.com/tonybaloney'

    bugs.python.org fields:

    activity = <Date 2019-05-09.05:59:19.403>
    actor = 'serhiy.storchaka'
    assignee = 'anthonypjshaw'
    closed = True
    closed_date = <Date 2019-05-09.05:59:19.404>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2019-04-08.04:38:16.568>
    creator = 'anthony shaw'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36552
    keywords = ['patch']
    message_count = 3.0
    messages = ['339589', '339592', '340080']
    nosy_count = 4.0
    nosy_names = ['serhiy.storchaka', 'steve.dower', 'anthonypjshaw', 'anthony shaw']
    pr_nums = ['12720']
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue36552'
    versions = ['Python 3.8']

    Activity

    1. tonybaloney commented on Apr 8, 2019

      tonybaloneymannequin
      MannequinAuthor

      When calculating length of range() objects that have an r->length > PY_SIZE_MAX, the underlying PyLong_AsSsize_t() function will raise an OverflowError:

      >>> a = list(range(2**256))
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: Python int too large to convert to C ssize_t
      >>> a = range(2**256)
      >>> len(a)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: Python int too large to convert to C ssize_t

      This is expected behaviour, but to the average user, who won't know what ssize_t is, or what this has to do with Python int, the user message is confusing and OverflowError is the symptom but not the cause. The cause is that the length sent to range was in a value too large to calculate.

      This patch changes OverflowError to ValueError to hint to the user that the value sent to the range object constructor is too large.

      >>> a = list(range(2**256))
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: Range object too large to calculate length (Overflow Error)
      >>> a = range(2**256)
      >>> len(a)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: Range object too large to calculate length (Overflow Error)
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Apr 8, 2019
    3. serhiy-storchaka commented on Apr 8, 2019

      @serhiy-storchaka
      Member

      For large but smaller than the C limit ranges the list constructor raises a MemoryError. It should raise the same error for larger ranges.

      Raising an OverflowError in range.__len__ is legitimate.

    4. zooba commented on Apr 12, 2019

      @zooba
      Member

      We should at least have consistent error messages:

      >>> class O:
      ...  def __len__(self):
      ...   return 2**100
      ...
      >>> o=O()
      >>> len(o)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      OverflowError: cannot fit 'int' into an index-sized integer

      I'd argue for replacing 'int' here with the rendered value, but I think OverflowError is the right type. Mentioning "C ssize_t" is the problem.

      As for the list constructor, I'd be okay with chaining a MemoryError here, provided the OverflowError sticks around. But in this context a MemoryError is "recoverable" while an OverflowError very likely indicates a programming error, so we shouldn't hide it from the user.

    5. self-assigned this
      on May 9, 2019
    6. 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

    Labels

    3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directory

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions