Message52799
String formatting using %d doesn't handle well a long value when the object isn't a PyLong itself.
That is:
py> "%d" % 42L
'42'
py> "%d" % 10000000000000000000000000000000L
'10000000000000000000000000000000'
py> "%d" % 42.0
'42'
py> "%d" % 10000000000000000000000000000000.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int argument required
Here it was shown with a float object, but any other object with a __long__ or __int__ method returning a PyLong fails (by example, the gmpy package).
Currently PyString_Format, for "%d", checks whether the value *is* a PyLong and processes it, else tries to *convert* it to PyInt (and fails for large values).
The attached patch first checks for a number; if it's a number but not PyInt nor PyLong, tries to convert to int; if it fails, tries to convert to long. The resulting value (either a PyInt or PyLong) is formatted the same way as before.
If the original object was actually an int or long, it's handled the same way as before.
The test ordering was chosen to only convert to long when necesary.
|
|
Date |
User |
Action |
Args |
2007-08-23 15:58:53 | admin | link | issue1742669 messages |
2007-08-23 15:58:53 | admin | create | |
|