It's kind of odd

Charles G Waldman cgw at fnal.gov
Sat May 29 10:55:27 EDT 1999


Not that this is important, but I just discovered the following
slightly odd quirk.  

Python 1.5 (#1, Apr  3 1998, 14:05:35) [C] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import sys
>>> x=sys.maxint*"a"
Traceback (innermost last):
  File "<stdin>", line 1, in ?
MemoryError
>>> x=sys.maxint*"ab"
>>> x
''
>>> x=sys.maxint*"abc"
Traceback (innermost last):
  File "<stdin>", line 1, in ?
MemoryError
>>> x=sys.maxint*"abcd"
>>> x
''
>>> x=sys.maxint*"abcde"
Traceback (innermost last):
  File "<stdin>", line 1, in ?
MemoryError
>>> x=sys.maxint*"abcdef"
>>>

If the string has an odd number of characters, the operation fails, if
the string has an even number of characters it succeds.

Note that if you try this test on Linux you may get a core-dump.  I thought
this was a Python bug but it turns out to be a glibc malloc bug - with the
latest glibc, malloc(INT_MAX) will succeed, due to an integer overflow in
the padding and alignment calculations.  I've already submitted a patch to
the glibc maintainers to fix this, don't know if they'll use it though.




More information about the Python-list mailing list