[C++-sig] Trouble accessing custom object

David Abrahams dave at boost-consulting.com
Tue May 24 13:40:23 CEST 2005


"Ron Clarke" <ron.clarke at hp.com> writes:

> 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.

Your use of terminology may have confused me.  When there are two
languages involved, precision is really important.

> 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.

In Python, you "bind" names to values.

> 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

The call to Py_BuildValue is not only misspelled, it doesn't do
anything useful and will probably cause a crash.  See
http://docs.python.org/api/arg-parsing.html#l2h-214 for the
expectations Python has about the 2nd argument when the first argument
is "O"

It's not a "variable," it's an attribute of the ptrFilter object --
which I presume is a pointer to a module.

If you are trying to convert myimage to Python, I'd:

  python::object filter(handle<>(ptrFilter));
  filter.attr("imageIn") = python::object(myImage);

> 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).

Which of the two lines doesn't work?
What happens when it "doesn't work?"

> By the way, I have tried setting integer and string variables in the
> Python script, 

I think you meant you tried to set the integer and string variables in
the python _module_.  Setting them in a Python script would be
something like:

     # Python code
     integer = 3
     string = 'foo'

> just to make sure I understood the concepts. They
> worked fine.

How did you try to set them, and what indicated to you that it worked?

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

I still don't have a clear idea of what you're after or what problems
you had.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list