Interesting list Validity (True/False)

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri May 18 02:14:05 EDT 2007


En Fri, 18 May 2007 01:48:29 -0300, Alex Martelli <aleax at mac.com> escribió:

> The gmpy designer, writer and maintainer (all in the singular -- that's
> me) has NOT chosen anything of the sort.  gmpy.mpz does implement
> __int__ and __long__ -- but '%d'%somempzinstance chooses not to call
> either of them.  sys.maxint has nothing to do with the case:
> '%d'%somelonginstance DOES work just fine -- hey, even a *float*
> instance formats just fine here (it gets truncated).  I personally
> consider this a bug in %d-formatting, definitely NOT in gmpy.

Yes, sorry, at first I thought it was gmpz which refused to convert itself  
to long. But the fault is in the string formatting code, and it was  
pointed out later on this same thread. Floats have the same problem: "%d"  
% 5.2 does work, but "%d" % 1e30 does not.

After digging a bit in the implementation of PyString_Format, for a "%d"  
format it does:
- test if the value to be printed is actually a long integer (using  
PyLong_Check). Yes? Format as a long integer.
- else, convert the value into a plain integer (using PyInt_AsLong), and  
format that.
No attempt is made to *convert* the value to a long integer. I understand  
that this could be a slow operation, so the various tests should be  
carefully ordered, but anyway the __long__ conversion should be done.

-- 
Gabriel Genellina




More information about the Python-list mailing list