[MATRIX-SIG] PyArrayObjects

Perry A. Stoll pas@xis.xerox.com
Tue, 29 Jul 1997 16:28:12 PDT


On Tue, 29 Jul 1997, Dale Bertrand wrote:
    
    Unfortunately, it seems to me that PyArray_ContiguousFromObject makes a copy of 
    the PyObject which I _must_ return to see my changes.  I would like my c functio
    n to simply access and modify the array data without making a copy.

I think the more python way of doing things is to have functions return a
copy and leave the original unchanged. This accounts for the behaviour
you correctly identified for PyArray_ContiguousFromObject. But if you have
to modify the original, you have to...

    How do I access the array data without copying the Object?

How about something like this (untested)?
    
    static PyObject *
    do_array(PyObject *self,PyObject *args)
    {
        PyObject *op;  /* <- I would like to edit this */
        PyArrayObject *mp; /*  <-without making this  */ 
        int h,w;
        int ubound;
        int lbound;
        
        TRY(PyArg_ParseTuple(args, "Oii", &op, &lbound , &ubound));

        /* check that it really is an array object. I believe this
           will forbid inheriting in Python from C objects. Oh well. */
	TRY(PyArray_Check(op));

        /* a simple cast now that you know it really is an array object */
	mp = (PyArrayObject *)op;
        h = mp->dimensions[0];
        w = mp->dimensions[1];
        
        threshold((unsigned char *)mp->data, &lbound , &ubound, 1,w*h);
        return op;
    }

Hope that helps.

  -Perry
  <pas@xis.xerox.com>



_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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