[New-bugs-announce] [issue29839] Avoid raising OverflowError in len() when __len__() returns negative large value

Serhiy Storchaka report at bugs.python.org
Fri Mar 17 15:52:14 EDT 2017


New submission from Serhiy Storchaka:

For now len() raises ValueError if __len__() returns small negative integer and OverflowError if __len__() returns large negative integer. 

>>> class NegativeLen:
...     def __len__(self):
...         return -10
... 
>>> len(NegativeLen())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: __len__() should return >= 0
>>> class HugeNegativeLen:
...     def __len__(self):
...         return -sys.maxsize-10
... 
>>> len(HugeNegativeLen())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'int' into an index-sized integer

Proposed patch makes it always raising ValueError.

----------
components: Interpreter Core
messages: 289779
nosy: Oren Milman, haypo, serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Avoid raising OverflowError in len() when __len__() returns negative large value
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29839>
_______________________________________


More information about the New-bugs-announce mailing list