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
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: Arfrever, fwierzbicki, indra, lpan, pjenvey, zyasoft
Priority: normal Keywords: patch

Created on 2012-06-18.18:57:51 by Arfrever, last changed 2015-02-11.22:09:36 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
overflow_test.patch lpan, 2014-08-04.06:04:39
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
msg8904 (view) Author: Luoxi Pan (lpan) Date: 2014-08-04.06:04:39
I have verified that __len__asInt.patch will throw an overflow error on new style classes. I've applied the template to PyObjectDerived and written a test case for that. 

I've signed the contributor license agreement and my bugs.python.org user id is lpan78.
msg9484 (view) Author: Jim Baker (zyasoft) Date: 2015-02-03.05:59:02
Fixed as of https://hg.python.org/jython/rev/41d5032ef380, applying Indra's patch as the fix, and a new test case. Thanks Luoxi for suggesting a test, but I needed something that was more comprehensive.
History
Date User Action Args
2015-02-11 22:09:36zyasoftsetstatus: pending -> closed
2015-02-03 05:59:02zyasoftsetstatus: open -> pending
assignee: fwierzbicki -> zyasoft
resolution: fixed
messages: + msg9484
2014-08-04 06:04:40lpansetfiles: + overflow_test.patch
nosy: + lpan
messages: + msg8904
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