_PyLong_FromByteArray

Tim Peters tim.peters at gmail.com
Sat Aug 12 18:48:34 EDT 2006


[Dan Christensen]
> My student and I are writing a C extension that produces a large
> integer in binary which we'd like to convert to a python long.  The
> number of bits can be a lot more than 32 or even 64.  My student found
> the function _PyLong_FromByteArray in longobject.h which is exactly
> what we need, but the leading underscore makes me wary.  Is it safe to
> use this function?

Python uses it internally, so it better be ;-)

>  Will it continue to exist in future versions of python?

No guarantees, and that's why it has a leading underscore:  it's not
an officially supported, externally documented, part of the advertised
Python/C API.  It so happens that I added that function, because
Python needed some form of its functionality internally across
different C modules.  Making it an official part of the Python/C API
would have been a lot more work (which I didn't have time for), and
created an eternal new maintenance burden (which I'm not keen on
regardless ;-)).

In practice, few people touch this part of Python's implementation, so
I don't /expect/ it will go away, or even change, for years to come.
The biggest insecurity I can think of offhand is that someone may
launch a crusade to make some other byte-array <-> long interface
"official" based on a different way of representing negative integers.
 But even then I expect the current unofficial functions to remain,
since the 256's-complement representation remains necessary for the
`struct` module's "q" format, and for the `pickle` module's protocol=2
long serialization format.

> Or is there some other method we should use?

No.  That's why these functions were invented to begin with ;-)



More information about the Python-list mailing list