64-bit ints <-> PyLongs?

Trent Mick trentm at ActiveState.com
Thu Oct 26 11:57:48 EDT 2000


On Thu, Oct 26, 2000 at 04:02:13PM +0200, Kalle Svensson wrote:
> Hi.
> 
> I'm writing a wrapper around a C library where some functions return, or
> expect as arguments, 64-bit ints. Will PyLong_*LongLong work on
> win32 (and preferably Mac too), and is LONG_LONG guaranteed to be at least
> 64 bits?
> 

You'll never lose significant bits going *into* a PyLong (hence
PyLong_From*). They have infinite (limited only by your system's memory)
significance. However, coming out of a PyLong depends on the platform and on
the number coming out. In any case, the Python 2.0 code will (it is a bug if
it does not) throw an OverflowError if you try to use PyLong_As*() and the
PyLong won't fit in the C type.

I am not sure if LONG_LONG is guaranteed to be 64-bits but I am pretty sure
that that is a safe assumption (Tim Peters will probably correct me on that).
The problem is not every platform will HAVE_LONG_LONG (though, again, most
common ones do).

> If not, any suggestions on how to convert between 64-bit ints and PyLongs
> at least semi-portably (gcc and win32)?
> 

PyLong_FromLongLong will never lose significance.
PyLong_AsLongLong will work without losing significance or will throw
OverflowError.

CHeers,
Trent

-- 
Trent Mick
TrentM at ActiveState.com




More information about the Python-list mailing list