Hi Stefan,

Allocating a new PyArrayObject isn't terribly expensive (compared to
all the other allocations that Python programs are constantly doing),
but I'm afraid you have a more fundamental problem. The reason there
is no supported API to change the storage pointer of a PyArrayObject
is that the semantics of PyArrayObject are that the data must remain
allocated, and in the same place, until the PyArrayObject is freed
(and when this happens is in general is up to the garbage collector,
not you). You could make a copy, but you can't free the original
buffer until Python tells you you can. The problem is that many simple
operations on arrays return views, which are implemented as
independent PyArrayObjects whose data field points directly into your
memory buffer; these views will hold a reference to your
PyArrayObject, but there's no supported way to reverse this mapping to
find all the views that might be pointing into your buffer.

If you're very determined there are probably hacks you could use (be
very careful never to allocate views, or maybe gc.getreferrers() will
work to let you run around and fix up all the views), but at that
point you're kind of on your own anyway, and violating PyArrayObject's
encapsulation boundary is the least of your worries :-).

Hope things are well with you,
-n

On Thu, May 22, 2014 at 12:03 AM, Stefan Seefeld <ste...@seefeld.name> wrote:
> Hello,
>
> I would like to expose an existing (C++) object as a NumPy array to
> Python. Right now I'm using PyArray_New, passing the pointer to my
> object's storage. It now happens that the storage point of my object may
> change over its lifetime, so I'd like to change the pointer that is used
> in the PyArrayObject. Is there any API to do this ? (I'd like to avoid
> allocating a new PyArrayObject, as that is presumably a costly operation.)
> If not, may I access (i.e., change) the "data" member of the array
> object, or would I risk corrupting the application state doing that ?
>
> Many thanks,
>         Stefan
>
>
> --
>
>       ...ich hab' noch einen Koffer in Berlin...
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to