This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients NankerPhelge, eryksun
Date 2020-09-29.05:46:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1601358361.09.0.770981761989.issue41883@roundup.psfhosted.org>
In-reply-to
Content
I think this is a numpy issue. Its data_as() method doesn't support the ctypes _objects protocol to keep the numpy array referenced by subsequently created ctypes objects. For example:

    import ctypes
    import numpy as np

    dtype = ctypes.c_double
    ptype = ctypes.POINTER(dtype)

    class array_struct(ctypes.Structure):
        _fields_ = (('dim1', ctypes.c_int),
                    ('dim2', ctypes.c_int),
                    ('ptr', ptype))

    n = 12
    m = 50
    a = np.ones((n, m), dtype=dtype)
    p = a.ctypes.data_as(ptype)

data_as() is implemented as a cast() of the ctypes._data pointer. This is a c_void_p instance for the base address of the numpy array, but it doesn't reference the numpy array object itself. The pointer returned by cast() references this _data pointer in its _objects:

    >>> p._objects
    {139993690976448: c_void_p(42270224)}

data_as() also sets a reference to the numpy array object as a non-ctypes _arr attribute:

    >>> p._arr is a
    True

This _arr attribute keeps the array referenced for the particular instance, but ctypes isn't aware of it. When the object returned by data_as() is set in a struct, ctypes only carries forward the c_void_p reference from _objects that it's aware of:

    >>> a_wrap1 = array_struct(n, m, p)
    >>> a_wrap1._objects
    {'2': {139993690976448: c_void_p(42270224)}}

It would be sufficient to keep the numpy array alive if this c_void_p instance referenced the array, but it doesn't. Alternatively, data_as() could update the _objects dict to reference the array. For example:

    >>> p._objects['1'] = a
    >>> a_wrap2 = array_struct(n, m, p)
    >>> a_wrap2._objects['2']['1'] is a
    True
History
Date User Action Args
2020-09-29 05:46:01eryksunsetrecipients: + eryksun, NankerPhelge
2020-09-29 05:46:01eryksunsetmessageid: <1601358361.09.0.770981761989.issue41883@roundup.psfhosted.org>
2020-09-29 05:46:00eryksunlinkissue41883 messages
2020-09-29 05:46:00eryksuncreate