This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ggenellina
Recipients
Date 2007-06-25.06:24:36
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
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.
History
Date User Action Args
2007-08-23 15:58:53adminlinkissue1742669 messages
2007-08-23 15:58:53admincreate