You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>>> classA:
... __len__=lambdaself: 10**20
>>> len(A())
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
len(A())
OverflowError: cannot fit 'int' into an index-sized integer
Mark: I thought it was too, but the two I noted were the closest I could find. Maybe you'll find something even more on point :)
4kir4 commented on May 6, 2014
4kir4mannequin
Mannequin
More actions
If len() signature can't be changed to return Python int objects (unlimited) then the OverflowError may contain the actual .length
property instead (based on msg66459 by Antoine Pitrou)
It is equivalent to len(sized) if len doesn't raise
OverflowError i.e., if the length is less than sys.maxsize on
CPython; otherwise return OverflowError.length attribute
"""
try:
return len(sized)
except OverflowError as e:
return e.length
I recommend this be closed: too much impact on existing code for too little benefit.
CPython has historically imposed some artificial implementation specific details in order make the implementation cleaner and faster internally (i.e. a limit on the number of function arguments, sys.maxsize limits, etc.)
If people really care about this (I'm not sure they do), there is a possible general solution.
Add an OverflowError fallback code path to the builtin len() function. That path can then compute arbitrarily large sizes from objects that have integer start, stop, and step attributes. The fixes cases like len(range(1 << 100)).
The builtin len() could also be built out with a fallback that calls __len__ method directly. This won't help with builtin types like rangeobject, but it would help with user defined types:
These two measures would mitigate the limitations imposed by CPython's system of internal slots like mp_length and sq_length. However, I don't think any work should be done until we've ascertained whether we're partially solving a real problem or just addressing a theoretical concern.
(1) can only fix len() for range(), (2) only works with Python implementations and may call __len__ twice.
Adding an attribute to OverflowError and catching it in len() could solve both problem, but it is a bit unsafe, because an OverflowError can be raised in immediate computations.
The only reliable method is adding slots mp_long_length and sq_long_length. But it is too heavy change for solving the problem for which we don't know how often it occurs in real code.
Activity
cool-RR commentedon May 5, 2014
I want to use big numbers for length.
bitdancer commentedon May 6, 2014
While this is classed as a CPython implementation detail (see bpo-15718) it doesn't sound like it is likely to be changed (see bpo-2723).
[-]__len__ can't return big numbers[/-][+]__len__[/+][-]__len__[/-][+]__len__ can't return big numbers[/+]mdickinson commentedon May 6, 2014
Whoops; sorry -- accidental title change by typing
__len__into something that wasn't the search box.Stupid fingers...
(I suspect this issue is a duplicate of an existing issue.)
bitdancer commentedon May 6, 2014
Mark: I thought it was too, but the two I noted were the closest I could find. Maybe you'll find something even more on point :)
4kir4 commentedon May 6, 2014
If
len()signature can't be changed to return Python int objects (unlimited) then the OverflowError may contain the actual.lengthproperty instead (based on msg66459 by Antoine Pitrou)
operator.length():
mdickinson commentedon May 6, 2014
That's pretty evil. :-)
rhettinger commentedon May 8, 2014
I recommend this be closed: too much impact on existing code for too little benefit.
CPython has historically imposed some artificial implementation specific details in order make the implementation cleaner and faster internally (i.e. a limit on the number of function arguments, sys.maxsize limits, etc.)
rhettinger commentedon Apr 29, 2022
If people really care about this (I'm not sure they do), there is a possible general solution.
Add an OverflowError fallback code path to the builtin len() function. That path can then compute arbitrarily large sizes from objects that have integer start, stop, and step attributes. The fixes cases like
len(range(1 << 100)).The builtin len() could also be built out with a fallback that calls
__len__method directly. This won't help with builtin types like rangeobject, but it would help with user defined types:These two measures would mitigate the limitations imposed by CPython's system of internal slots like mp_length and sq_length. However, I don't think any work should be done until we've ascertained whether we're partially solving a real problem or just addressing a theoretical concern.
serhiy-storchaka commentedon Apr 30, 2022
(1) can only fix
len()forrange(), (2) only works with Python implementations and may call__len__twice.Adding an attribute to
OverflowErrorand catching it inlen()could solve both problem, but it is a bit unsafe, because an OverflowError can be raised in immediate computations.The only reliable method is adding slots
mp_long_lengthandsq_long_length. But it is too heavy change for solving the problem for which we don't know how often it occurs in real code.