[Numpy-discussion] mtrand.c update 1.11 breaks my crappy code
Neal Becker
ndbecker2 at gmail.com
Wed Apr 6 10:02:37 EDT 2016
Neal Becker wrote:
> Robert Kern wrote:
>
>> On Wed, Apr 6, 2016 at 2:18 PM, Neal Becker <ndbecker2 at gmail.com> wrote:
>>>
>>> I have C++ code that tries to share the mtrand state. It unfortunately
>>> depends on the layout of RandomState which used to be:
>>>
>>> struct __pyx_obj_6mtrand_RandomState {
>>> PyObject_HEAD
>>> rk_state *internal_state;
>>> PyObject *lock;
>>> };
>>>
>>> But with 1.11 it's:
>>> struct __pyx_obj_6mtrand_RandomState {
>>> PyObject_HEAD
>>> struct __pyx_vtabstruct_6mtrand_RandomState *__pyx_vtab;
>>> rk_state *internal_state;
>>> PyObject *lock;
>>> PyObject *state_address;
>>> };
>>>
>>> So
>>> 1. Why the change?
>>> 2. How can I write portable code?
>>
>> There is no C API to RandomState at this time, stable, portable or
>> otherwise. It's all private implementation detail. If you would like a
>> stable and portable C API for RandomState, you will need to contribute
>> one using PyCapsules to expose the underlying rk_state* pointer.
>>
>> https://docs.python.org/2.7/c-api/capsule.html
>>
>> --
>> Robert Kern
>
> I don't see how pycapsule helps here. What I need is, my C++ code
> receives
> a RandomState object. I need to call e.g., rk_random, passing the pointer
> to rk_state - code looks like this;
>
> RandomState* r = (RandomState*)(rs.ptr());
> // result_type buffer;
> // rk_fill ((void*)&buffer, sizeof(buffer), r->internal_state);
> if (sizeof(result_type) == sizeof (uint64_t))
> return rk_ulong (r->internal_state);
> else if (sizeof(result_type) == sizeof (uint32_t))
> return rk_random (r->internal_state);
Nevermind, I see it's described here:
https://docs.python.org/2.7/extending/extending.html#using-capsules
More information about the NumPy-Discussion
mailing list