[C++-sig] Trouble accessing custom object

Clarke, Ronald ron.clarke at hp.com
Mon May 9 19:54:42 CEST 2005


I am trying to access a custom object and am having a few problems.
Hopefully someone can point out what I'm doing wrong.
Here's the scenario:
1. We have a proprietary image class, scifiImage, written in C++ and
wrapped for the Python environment using Boost Python. This part works
well. 

2. We have one of these image objects in a Python script, and are trying
to access it from another C++ module. The Python script recognizes the
image object (as expected, since this part has worked for quite a
while), but we cannot access the "wrapped" image object from another C++
module.

Here is the Python script, with some comments:

from scifi_py import *  #this is the library that contains scifiImage

# Here we create an instance of the scifiImage object, initialize it,
and print some
# properties, just to validate the image construction works. This works
fine.
imageIn = scifiImage(1,10,10,8)
imageIn.initRows(22)
print "initial properties of imageIn...\n", imageIn.printProperties()



Now here is the code from a C++ module that tries to access the
'imageIn' variable. Note this is not the same module 
that wraps scifiImage.

    Py_Initialize();

    // this line imports the Python code listed above. The import
succeeds, and the output
    // from imageIn.printProperties() appears as expected
    PyObject *pPyfilter =
PyImport_ImportModule(const_cast<char*>(sfilterName));

    // for test purposes, create another scifiImage object in the C++
world
    scifiImage* pimagetest = new scifiImage(3,5,5,8);
    pimagetest->initRows(50);

    //build a Python object using the new scifiImage object
    PyObject *pargsi = Py_BuildValue("O", &pimagetest);

    //Now set the 'imageIn' attribute in the Python script...
    int nattrreti = PyObject_SetAttrString(pPyfilter, "imageIn",
pargsi);

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


Can anyone tell me what I am doing wrong? Have I missed a step? Is my
syntax in error?
Any help is greatly appreciated.

Thanks.

_____________________________
Ron Clarke
Digital Printing Technologies
Hewlett-Packard Company
ron.clarke at hp.com




More information about the Cplusplus-sig mailing list