[C++-sig] C++ const -> Python immutable?

David Abrahams dave at boost-consulting.com
Wed Sep 25 22:34:08 CEST 2002


Phil,

I'm currently implementing NumPy support, but I'm taking an entirely
different approach which will allow it to interoperate with numarray as
well. Downside: it's probably going to be a little less-efficient from the
C++ side than your approach, since it goes through the Python 'C' api to
access all functionality.

From: "Philip Austin" <paustin at eos.ubc.ca>


> Ralf W. Grosse-Kunstleve writes:
>  >
>  > Now it seems to me that both by_reference and by_const_reference
>  > work, but that the "const" is lost. Is this correct?  Ralf
>
>
> This is one step further than I've managed to get, and I'm wondering
> whether someone could point out the errors in the following program,
> or suggest another approach.  The goal is to have a simple wrapper
> (numhandle) that holds an object containing a PyArrayObject*.  Given
> any python sequence, numpy can produce a deep copy with:
>
> a) PyArray_CopyFromObject(o,PyArray_NOTYPE,0,0)))
>
> and  either an incremented reference or a deep
> copy with:
>
> b) PyArray_ContiguousFromObject(o,PyArray_NOTYPE,0,0)))
>
> Below I try to invoke a) with the function:
>
> numhandle pass_by_value(numhandle theArray)
>
> and b) with
>
> numhandle pass_by_reference(numhandle& theArray)

Hmmm; that doesn't seem right.
Taking the approach used for list, tuple, et. al for consistency would mean
that you never copy the object and only match python arguments of the
correct numpy type. If you want a generic object, a boost::python::object
argument will do the job just fine.

> (I also try to return a reference, but instantiating
> numhandle_to_python_reference() causes the compiler
> to complain as noted on the line tagged Error: below).
>
> The code compiles, but the results aren't what
> I expect viz:
>
> In python:
>
> import Numeric as N
> import num_test_ext as nt
> thearray=N.arange(3,typecode=N.Float32)
> print "python calls pass by value"
> print nt.pass_by_value(thearray)
>
> produces:
>
> python calls pass by value
> inside reference constructor
> inside pass_by_value[ 999.    1.    2.]
>
> i.e.  the reference constructor is called, even though
> the signature is:
>
> numhandle pass_by_value(numhandle theArray)

Your numhandle object has these constructors?

    numhandle(numhandle)
    numhandle(numhandle&)

Or do you mean something else??

> Next I try calling:
>
> numhandle pass_by_reference(numhandle& theArray)
>
> in python:
>
> print "python calls pass by reference"
> print nt.pass_by_reference(thearray)
>
> which yields:
>
> python calls pass by reference
> Traceback (most recent call last):
>   File "<stdin>", line 12, in ?
> TypeError: bad argument type for built-in operation
>
>
> Many thanks, Phil Austin


No time to look at your code this instant, but I'll have my new stuff
checked in before the day's end. Why don't you hang on?

-Dave







More information about the Cplusplus-sig mailing list