[Python-ideas] Py_SIZE of PyLongs
Tim Peters
tim.peters at gmail.com
Wed Oct 19 23:00:04 EDT 2016
[Elliot Gorokhovsky <elliot.gorokhovsky at gmail.com>]
> I'm working on a special-case compare function for bounded integers for the
> sort stuff. By looking at the implementation, I figured out that Py_SIZE of
> a long is the sign times the number of digits (...right?).
> ...
Please ignore the other reply you got - they clearly aren't familiar
with the code.
The details are explained in Include/longintrepr.h.
In short, an integer _is_ based on PyVarObject. Py_SIZE is a macro
that merely extracts (or allows to set) the ob_size member, the sign
of the int is stored as the sign of ob_size (which is really an abuse
of ob_size's intended meaning), and the number of "digits" is the
absolute value of ob_size. And ob_size is 0 if and only if the int is
0.
Note that the number of bits per digit varies across platforms. That
too is all explained in longintrepr.h.
More information about the Python-ideas
mailing list