[Python-ideas] Py_SIZE of PyLongs

Thomas Nyberg tomuxiong at gmx.com
Wed Oct 19 21:24:30 EDT 2016


On 10/19/2016 09:04 PM, Elliot Gorokhovsky wrote:
> A quick note:
>
> 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?).
> Before looking at the implementation, though, I had looked for this info
> in the docs, and I couldn't find it anywhere. Since Py_SIZE is public, I
> think the documentation should make clear what it returns for PyLongs,
> for example somewhere on the "Integer Objects" page. Apologies if this
> is specified somewhere else in the docs and I just couldn't find it.
>
> Elliot

I don't think this is right.

	https://github.com/python/cpython/blob/master/Include/object.h#L119
	https://docs.python.org/3/c-api/structures.html#c.Py_SIZE
	https://docs.python.org/3/c-api/structures.html#c.PyVarObject

It returns the `ob_size` fields of a PyVarObject. I think this has to do 
with objects with variable sizes like lists. PyLongs are not 
PyVarObjects because they have no notion of length.

Why would a long be stored as a sequence of digits instead of a (say) 64 
bit integer as 8 bytes?

Cheers,
Thomas



More information about the Python-ideas mailing list