Issue 12159: Integer Overflow in len (original) (raw)

Issue12159

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:17 admin set github: 56368
2020-01-12 09:04:36 vstinner set messages: +
2020-01-10 12:13:54 Zac Hatfield-Dodds set pull_requests: + <pull%5Frequest17339>
2011-05-23 14:30:42 vstinner set nosy: + vstinnermessages: +
2011-05-23 14:27:30 benjamin.peterson set status: open -> closednosy: + benjamin.petersonmessages: + resolution: works for me
2011-05-23 14:24:02 peter.fankhaenel create