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, haypo, peter.fankhaenel
Priority: normal Keywords:

Created on 2011-05-23 14:24 by peter.fankhaenel, last changed 2011-05-23 14:30 by haypo. This issue is now closed.

Messages (3)
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 (haypo) * (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.
History
Date User Action Args
2011-05-23 14:30:42hayposetnosy: + haypo
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