Skip to content

Integer Overflow in __len__ #56368

Closed
Closed

Description

@peterfankhaenel
BPO 12159
Nosy @vstinner, @benjaminp
PRs
  • bpo-12159: document sys.maxsize limit in len() function reference #17934
  • 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 = None
    closed_at = <Date 2011-05-23.14:27:30.522>
    created_at = <Date 2011-05-23.14:24:02.217>
    labels = ['OS-windows']
    title = 'Integer Overflow in __len__'
    updated_at = <Date 2020-01-12.09:04:36.073>
    user = 'https://bugs.python.org/peterfankhaenel'

    bugs.python.org fields:

    activity = <Date 2020-01-12.09:04:36.073>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-05-23.14:27:30.522>
    closer = 'benjamin.peterson'
    components = ['Windows']
    creation = <Date 2011-05-23.14:24:02.217>
    creator = 'peter.fankhaenel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 12159
    keywords = []
    message_count = 4.0
    messages = ['136644', '136645', '136646', '359837']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'benjamin.peterson', 'peter.fankhaenel']
    pr_nums = ['17934']
    priority = 'normal'
    resolution = 'works for me'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue12159'
    versions = ['Python 2.6', 'Python 2.7']

    Activity

    1. peterfankhaenel commented on May 23, 2011

      peterfankhaenelmannequin
      MannequinAuthor

      An OverflowError is emitted in case the return value of __len__
      exceeds 2**31-1.

      The following code:

      class C (object):
          def __len__ (self):
             return self.l
      
      c = C()
      c.l = 2**31

      len (c)

      # leads to an OverflowError in the last line. It works flawless for

      c.__len__ ()

    2. benjaminp commented on May 23, 2011

      @benjaminp
      Contributor

      Yep, len() has to return something less than sys.maxsize.

    3. vstinner commented on May 23, 2011

      @vstinner
      Member

      len(obj) is implemented using PyObject_Size() which is stores the result into a Py_ssize_t, and so is limited to sys.maxsize (2**31-1 or 2**63-1).

      This implementation detail should be documented.

    4. vstinner commented on Jan 12, 2020

      @vstinner
      Member

      New changeset d7c7add by Victor Stinner (Zac Hatfield-Dodds) in branch 'master':
      bpo-12159: Document sys.maxsize limit in len() function reference (GH-17934)
      d7c7add

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

    No one assigned

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions