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-30.03:49:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1601437795.92.0.49548477251.issue41883@roundup.psfhosted.org>
In-reply-to
Content
> `data_as` method which has the desired behavior: "The returned 
> pointer will keep a reference to the array."

I don't think it's the desired behavior at all. data_as() sets an _arr attribute of which ctypes isn't aware. It should cast the address to the given type and manually set the array reference in the _objects dict, which ctypes will automatically carry forward in all instances of aggregate types (structs and arrays) that reference the numpy array. For example:

    >>> p = a.ctypes.data_as(ptype)
    >>> p._objects['1'] = a

Adding p to an array carries its _objects dict forward:

    >>> ptarr = (ptype * 1)(p)
    >>> ptarr._objects['0']['1'] is a
    True

If the returned pointer is cast() again, then bpo-12836 is an issue. For example:

    >>> p2 = ctypes.cast(p, ctypes.c_void_p)
    >>> p._objects is p2._objects
    True
    >>> for k in p._objects:
    ...     if p._objects[k] is p:
    ...         print('circular reference')
    ... 
    circular reference

That needs to be fixed. But relying on _arr instead of correctly integrating with ctypes isn't a good idea, IMO. Work around the actual bug instead of introducing behavior that risks crashing just for the sake of resolving an uncommon circular reference problem.
History
Date User Action Args
2020-09-30 03:49:56eryksunsetrecipients: + eryksun, NankerPhelge
2020-09-30 03:49:55eryksunsetmessageid: <1601437795.92.0.49548477251.issue41883@roundup.psfhosted.org>
2020-09-30 03:49:55eryksunlinkissue41883 messages
2020-09-30 03:49:55eryksuncreate