Python 2.7: _PyLong_NumBits() Segfault

Adam Meily meily.adam at gmail.com
Fri Aug 14 09:21:57 EDT 2015


I am working on a CPython library that serializes Python objects to disk in
a custom format. I'm using _PyLong_NumBits() to determine whether
PyLong_AsLong(), PyLong_AsUnsignedLong(), PyLong_AsLongLong(), or
PyLong_AsUnsignedLongLong() should be used.

In Python 3.x, I'm able to determine how many bits are required to write an
int to disk without issue. However, in Python 2.7, _PyLong_NumBits()
segfaults when the type is a PyInt that has the value of 0xffffffff
(maximum unsigned 32-bit value). Additionally, _PyLong_NumBits() is not
accurate when the number is negative. My guess is that I can't use
_PyLong_NumBits() on a PyInt object. So, what is the correct way to
determine the number of bits required to represent a PyInt object in Python
2.7?

I'm using Python 2.7.6, x86-64 on Ubuntu.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150814/0d703792/attachment.html>


More information about the Python-list mailing list