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

Paul Rubin http
Sun Sep 3 06:38:57 CEST 2006


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!!!!
    >>> 



More information about the Python-list mailing list