Eryk Sun <eryk...@gmail.com> added the comment:

> `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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41883>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to