Boost logo

Boost :

From: Karl Bellve (Karl.Bellve_at_[hidden])
Date: 2001-03-28 13:31:22


From the partial code below, my module is loaded by Python (statically
linked in my app, as well as BPL). But, it fails when it tries to find
the function "GetT" using the call PyDict_GetItemString. It also doesn't
redirect STDIO or STDERR when I put that in a simple
PyRun_SimpleString(). It is a little harder to check things inside
Python because I am not making a DLL, so I can't really load the module
into a python shell and see the STDIO/STDERR, unless I redirect it back
to my Application.

Everything compiles fine. Program runs fine but isn't able to call GetT,
stdout and stderr....

int CImageManager::StartPython()
{
        int a;
        long answer;
        PyObject *pException, *modname, *mod, *mdict, *func, *rslt;

        Py_Initialize();

        // need to init the module with the appropiate methods
        InitMyModule();
        
// a=PyRun_SimpleString("import imagemanager\n"); /* import into name
space */

// manually importing the imagemanager module and calling a simple
function
        modname = PyString_FromString("imagemanager");
// import my module and lets see if we can call a simple function
        mod = PyImport_Import(modname);
        if (mod)
        {
                mdict = PyModule_GetDict(mod);
                func = PyDict_GetItemString(mdict,"GetT"); /* this returns NULL */
                if (func)
                {
                        if (PyCallable_Check(func))
                        {
                                rslt = PyObject_CallFunction(func, NULL);
                                if (rslt)
                                {
                                        answer = PyInt_AsLong(rslt);
                                        Py_XDECREF(rslt);
                                }
                        }
                }
        }

        // redirect STDIO and STDERR to my functions, using
Py_Run_SimpleString()
        a=PyRun_SimpleString("class my_stdout:\n"
                             " def write(self,string):\n"
                             " imagemanager.stdout(string)\n"
                             "\n"
                             "class my_stderr:\n"
                             " def write(self,string):\n"
                             " imagemanager.stderr(string)\n"
                             "\n"
                             "import sys\n"
                             "sys.stdout = my_stdout()\n"
                             "sys.stderr = my_stderr()\n"
                                "\n"
                             );
        
        a=PyRun_SimpleString("print 3*3\n");
        Py_Finalize();

        return 0;

}

extern "C"
namespace python = boost::python;
void InitMyModule()
{

        try
        {
                // Create an object representing this extension module.
                python::module_builder imagemanager("imagemanager");
                python::class_builder<CImageManager>
imagemanager_class(imagemanager,"CImageManager");
                
                imagemanager_class.def(python::constructor<>());
                imagemanager_class.def(&CImageManager::GetT,"GetT");
                imagemanager_class.def(&CImageManager::s_stdout,"stdout");
                imagemanager_class.def(&CImageManager::s_stderr,"stderr");
        }
        catch (...)
        {
                python::handle_exception(); // Deal with the exception for Python
        }
        
}

BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE

        PyObject *to_python(CImageManager *p)
        {
                return
python::python_extension_class_converters<CImageManager>::smart_ptr_to_python(p);
        }

BOOST_PYTHON_END_CONVERSION_NAMESPACE


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk