Issue1929

classification
Title: __len__ of new-style classes should raise OverflowError instead of TypeError when overflow occurs
Type: Severity: normal
Components: Versions: Jython 2.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: Arfrever, fwierzbicki, indra, pjenvey, zyasoft
Priority: normal Keywords: patch

Created on 2012-06-18.18:57:51 by Arfrever, last changed 2014-07-05.02:51:25 by zyasoft.

Files
File name Uploaded Description Edit Remove
__len__.patch Arfrever, 2012-07-14.00:21:48
__len__asInt.patch indra, 2014-03-24.09:58:42
Messages
msg7241 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-06-18.18:57:49
__len__ of new-style classes should raise OverflowError instead of TypeError for compatibility with CPython.

$ jython2.7
Jython 2.7.0a2+ (default:2cc31de620ba+, cze 17 2012, 09:04:11) 
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_03-icedtea
Type "help", "copyright", "credits" or "license" for more information.
>>> class X(object):
...     def __len__(self):
...         return 2 ** 70
... 
>>> len(X())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __len__ should return a int
>>>
$ python2.7
Python 2.7.3+ (2.7:0add70dd3c43+, Jun 17 2012, 04:30:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class X(object):
...     def __len__(self):
...         return 2 ** 70
... 
>>> len(X())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
>>>
$ python3.2
Python 3.2.3+ (3.2:714b8f91f3d4+, Jun 17 2012, 04:59:08) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...     def __len__(self):
...         return 2 ** 70
... 
>>> len(X())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'int' into an index-sized integer
>>>
msg7319 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-07-14.00:21:48
The attached patch seems to introduce behavior identical to CPython's behavior.
msg7320 (view) Author: Philip Jenvey (pjenvey) Date: 2012-07-14.02:04:19
This patch breaks when a type returns a long that can fit into an int (e.g. 3L) or a faux-int (a type that implements __int__).

What you want is to return res.asInt() and it should take care of all those details. A test would be nice too (unless there's already some in the stdlib, which I hope there are =] )
msg7384 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-08-10.21:29:15
Arfrever: have you had a chance to submit a contributor agreement yet?
msg8261 (view) Author: Indra Talip (indra) Date: 2014-03-24.09:58:42
Attached patch changes object.derived template to use asInt in order for __len__ methods to convert longs that fit in an int value to be returned or throw OverflowError otherwise.
msg8866 (view) Author: Jim Baker (zyasoft) Date: 2014-07-05.02:51:25
Target beta 4
History
Date User Action Args
2014-07-05 02:51:25zyasoftsetnosy: + zyasoft
messages: + msg8866
2014-03-24 09:58:43indrasetfiles: + __len__asInt.patch
nosy: + indra
messages: + msg8261
2013-02-25 18:18:48fwierzbickisetpriority: normal
versions: + Jython 2.5
2012-08-10 21:29:16fwierzbickisetassignee: fwierzbicki
messages: + msg7384
2012-07-14 02:04:20pjenveysetnosy: + pjenvey
messages: + msg7320
2012-07-14 00:21:49Arfreversetfiles: + __len__.patch
nosy: + fwierzbicki
messages: + msg7319
keywords: + patch
title: __len__ of new-style classes should raise OverflowError instead of TypeError -> __len__ of new-style classes should raise OverflowError instead of TypeError when overflow occurs
2012-06-18 18:57:51Arfrevercreate