[C++-sig] Trouble accessing custom object

Ron Clarke ron.clarke at hp.com
Tue May 24 00:42:43 CEST 2005


David - thanks for the reply.

I was off working on something else for a while; now I'm back to this...

Perhaps I'm more confused than I thought, but I'm not sure your answer gets 
to my issue.
What I'm trying to do is assign a instance of a C++ object (whose class is 
wrapped by Boost) to a variable in a user-written Python script, in much the 
same way one would use Py_BuildValue and PyObject_SetAttrString to set 
values inherently understood by Python, such as integers and strings.

I thought I could use syntax like this in my C++ code, after importing the 
Python script:
    PyObject &pargsi = PyBuildValue("O", &myimage);
    PyObject_SetAttrString(ptrFitler, "imageIn", pargsi);  //where "imageIn" 
is the variable in the Python script

I've not been able to get this to work. (I've tried several variations of 
this theme after reading the Boost Python docs again).
By the way, I have tried setting integer and string variables in the Python 
script, just to make sure I understood the concepts. They worked fine.

Am I missing something basic? Is there a way to do what I need to do?

Thanks in advance.


"David Abrahams" <dave at boost-consulting.com> wrote in message 
news:1x8b6nzs.fsf at boost-consulting.com...
> "Clarke, Ronald" <ron.clarke at hp.com> writes:
>
>>     // ...and try calling printProperties() again, to see if the new
>> image is printed
>>     int nrunres = PyRun_SimpleString("imageIn.printProperties()\n");
>>
>>
>> The address of pargsi looks reasonable, and the return code from the
>> SetAttrString call is zero.
>> However, PyRun_SimpleString fails with this error:
>>
>> Traceback (most recent call last):
>>   File "<string>", line 1, in ?
>> NameError: name 'imageIn' is not defined
>
> There's no reason for the unqualified name "imageIn" to be looked up
> in your filter module AFAICT.
>
> I suggest:
>
>    int nrunres = PyRun_SimpleString(
>          const_cast<char*>(
>             extract<char const*>(
>                str("from %s import *; imageIn.printProperties()\n")  % 
> sfilterName
>             )
>          )
>    );
>
> HTH,
>
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com 






More information about the Cplusplus-sig mailing list