This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Integer Overflow in __len__
Type: Stage:
Components: Windows Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, peter.fankhaenel, vstinner
Priority: normal Keywords:

Created on 2011-05-23 14:24 by peter.fankhaenel, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17934 merged Zac Hatfield-Dodds, 2020-01-10 12:13
Messages (4)
msg136644 - (view) Author: Peter Fankhaenel (peter.fankhaenel) Date: 2011-05-23 14:24
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__ ()
msg136645 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-05-23 14:27
Yep, len() has to return something less than sys.maxsize.
msg136646 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-05-23 14:30
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.
msg359837 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-12 09:04
New changeset d7c7adde003ddca5cbe4fc47cf09464ab95a066e by Victor Stinner (Zac Hatfield-Dodds) in branch 'master':
bpo-12159: Document sys.maxsize limit in len() function reference (GH-17934)
https://github.com/python/cpython/commit/d7c7adde003ddca5cbe4fc47cf09464ab95a066e
History
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56368
2020-01-12 09:04:36vstinnersetmessages: + msg359837
2020-01-10 12:13:54Zac Hatfield-Doddssetpull_requests: + pull_request17339
2011-05-23 14:30:42vstinnersetnosy: + vstinner
messages: + msg136646
2011-05-23 14:27:30benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg136645

resolution: works for me
2011-05-23 14:24:02peter.fankhaenelcreate