[Patches] [ python-Patches-1742669 ] "%d" format handling for long values

SourceForge.net noreply at sourceforge.net
Thu Aug 2 21:17:02 CEST 2007


Patches item #1742669, was opened at 2007-06-25 03:24
Message generated for change (Settings changed) made by facundobatista
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1742669&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Gabriel Genellina (gagenellina)
>Assigned to: Facundo Batista (facundobatista)
Summary: "%d" format handling for long values

Initial Comment:
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.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1742669&group_id=5470


More information about the Patches mailing list