[PYTHON MATRIX-SIG] C API questions

Jim Hugunin hugunin@mit.edu
Wed, 6 Nov 1996 11:17:09 -0500


> > My usual solution in this case is to copy data frequently.  I assume
that
> > you want to modify the array x in-place here?  My own approach in these
> > situation has been to change my C API to something like:
> 
> Yes, the data does get modified, but even without modification I wouldn't
> think of copying, because the arrays can be very large (meaning a few MB,
> and there are several of them around).

Okay, I'm still not convinced, see my comments below...

> But I just got another idea: if before returning I set the array's
> shape to something corresponding to a zero length array, then
> there should be no way to access its memory area, right? Ignoring
> of course buggy C extensions, which I don't care about. To be
> even more safe, I could change the data pointer to point to some
> small statically allocated area. Or am I missing something?

Unfortunately, if the user has taken a slice of the array (or indexed into
it if it's more than 1d) they can have an array with a pointer to the data
in your original array and there's no way for you to know this (except of
course for the original array's reference count).

This does appear to be a bit of a problem.  I never imagined people would
want to call a NumPy function from generic, non-python-aware C.  Could you
explain a little bit more the reasoning behind this design?  At first
glance it looks really backwards.  I'm also a bit confused as to what you
are doing to these arrays in python code.  Most operations on arrays
involve a factor of 2 or so wasted memory (c = a+b).  If you're that
concerned about making copies of arrays it's hard for me to imagine what
useful things you could do with them in python code (without writing c-like
code of the form add(a,b,c) which is not meant for frequent use).

-Jim

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================