Python style: to check or not to check args and data members

Jean-Paul Calderone exarkun at divmod.com
Sun Sep 3 12:50:00 EDT 2006


On Sun, 03 Sep 2006 16:29:11 +0200, Bruno Desthuilliers <bdesth.quelquechose at free.quelquepart.fr> wrote:
>Paul Rubin a écrit :
>> Bruno Desthuilliers <onurb at xiludom.gro> writes:
>>
>>>I've rarely encoutered "silent" data corruption with Python - FWIW, I
>>>once had such a problem, but with a lower-level statically typed
>>>language (integer overflow), and I was a very newbie programmer by that
>>>time. Usually, one *very quickly* notices when something goes wrong.
>>
>>
>> The same thing can happen in Python, and the resulting bugs can be
>> pretty subtle.  I noticed the following example as the result of
>> another thread, which was about how to sort an 85 gigabyte file.
>> Try to put a slice interface on a file-based object and you can
>> hit strange integer-overflow bugs once the file gets larger than 2GB:
>>
>>     Python 2.3.4 (#1, Feb  2 2005, 12:11:53)
>>     [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
>>     Type "help", "copyright", "credits" or "license" for more information.
>>     >>> print slice(0, 3**33)
>>     slice(0, 5559060566555523L, None)         # OK ...
>>
>> So we expect slicing with large args to work properly.  But then:
>>
>>     >>> class A:
>>     ...   def __getitem__(self, s):
>>     ...     print s
>>     ...
>>     >>> a = A()
>>     >>> a[0:3**33]
>>     slice(0, 2147483647, None)                # oops!!!!
>>     >>>
>
>Looks like a Python bug, not a programmer error. And BTW, it doesn't
>happens with >=2.4.1
>
>Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
>[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
>linux2
>Type "help", "copyright", "credits" or "license" for more information.
> >>> print slice(0, 3**33)
>slice(0, 5559060566555523L, None)
> >>> class A(object):
>...     def __getitem__(self, s):
>...             print s
>...
> >>> A()[0:3**33]
>slice(0, 5559060566555523L, None)
> >>>

Note that it _does_ happen with current trunk at HEAD:

Python 2.6a0 (trunk:51698M, Sep  3 2006, 12:40:55) 
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> slice(0, 3 ** 33)
slice(0, 5559060566555523L, None)
>>> class x:
...     def __getitem__(self, idx): print idx
... 
>>> x()[:3**33]
slice(0, 2147483647, None)
>>> 

Jean-Paul



More information about the Python-list mailing list