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;


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

// manually importing the imagemanager module and calling a simple
        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);

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

        return 0;


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

                // Create an object representing this extension module.
                python::module_builder imagemanager("imagemanager");
        catch (...)
                python::handle_exception(); // Deal with the exception for Python


        PyObject *to_python(CImageManager *p)


Boost list run by bdawes at, gregod at, cpdaniel at, john at