[C++-sig] Calling python function from C++

Bjorn Pettersen BPettersen at NAREX.com
Wed Nov 13 04:09:15 CET 2002


In http://www.boost.org/libs/python/doc/v2/callbacks.html it says
"...given an _object_ instance f holding the function...", however I
can't find a simple way to get a reference to a Python function into an
object instance.

My test code is:

    PyRun_String(
        "def hello(s):              \n"
        "    return 'hello ' + s    \n",
        Py_file_input, ns, ns);
    PyObject* fn = PyRun_String("hello", Py_eval_input, ns, ns);

I then want code with the same semantics as:

    PyObject* res = PyObject_CallFunction(fn, "s", "world");
    Py_DECREF(fn);
    std::string cpp_res = PyString_AsString(res);
    Py_DECREF(res);

the closest I've gotten is:

    object hello = object(handle<>(borrowed(fn)));
    object res = hello("world");
    std::string cpp_res = extract<std::string>(res);

I found the wrapping of 'fn' in the example in the "errors.hpp" section.
Is this the easiest/correct way to do this? Second question: are the
reference counts in the Boost example all taken care of at end-of-scope?

-- bjorn






More information about the Cplusplus-sig mailing list