<div dir="ltr">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.<div><br></div><div>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?</div><div><br></div><div>I'm using Python 2.7.6, x86-64 on Ubuntu.</div></div>