Jeroen Demeyer schrieb am 26.02.2015 um 08:24:
> On 2015-02-25 15:15, Stefan Behnel wrote:
>> Well - what do you need it for?
> I need to manually create such objects using _PyLong_New() and then
> manually fill in the ob_digit field. Currently, I use
> (<PyLongObject*>L).ob_digit which is not a disaster, but it's not as
> elegant as it could be.
And would you mind sharing with us why you need to create the objects in
this unusual way?
>> Cython is actually pretty good at optimising PyLong values, and fiddling
>> with them manually is quite risky as their memory layout depends on a
>> CPython compile time setting (digit size).
> I'm using the constants defined in the Python headers (like sizeof(digit)
> and PyLong_SHIFT). The Cython-generated code is compiled using the Python
> header, so I don't see the problem here.
The problem is that the size of "digit" can either be specified by
configure, or determined at C compile time by the header files. I've seen
cases where the latter yielded a different size for me than what CPython
was really using internally, thus leading to crashes and/or data corruption.
Basically, if PYLONG_BITS_IN_DIGIT is defined by pyconfig.h, then you're on
the safe side. If it's not (and that's the normal case), then there's a
relatively high chance of being lucky, and a certain risk of being unlucky.
Stefan