Adding own C code

Asked by fostex666

Hi,

is there a way to add own C code to the wrapper. My use case would be
a method which builds anumpy array from C++ data and returns it.

Cheers,
Uwe

Question information

Language:
English Edit question
Status:
Answered
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Gustavo Carneiro (gjc) said :
#1

See http://packages.python.org/PyBindGen/cppclass.html?highlight=add_function_as_method#pybindgen.cppclass.CppClass.add_custom_method_wrapper

and

http://packages.python.org/PyBindGen/cppmethod.html?highlight=customcppmethodwrapper#pybindgen.cppmethod.CustomCppMethodWrapper

Example, from the unit tests (tests/foomodulegen_common.py):

--------------------- python ----------------------------
    ## test a custom method wrapper
    Bar, = [cls for cls in module.classes if cls.name == 'Bar']
    wrapper_body = '''
static PyObject *
_wrap_PyBar_Hooray_lenx(PyBar *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs,
                        PyObject **return_exception)
{
    PyObject *py_retval;
    int x;
    const char *keywords[] = {"x", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "i", (char **) keywords, &x)) {
        PyObject *exc_type, *traceback;
        PyErr_Fetch(&exc_type, return_exception, &traceback);
        Py_XDECREF(exc_type);
        Py_XDECREF(traceback);
        return NULL;
    }

    std::string retval;
    retval = Bar::Hooray();
    py_retval = Py_BuildValue((char *) "i", int(retval.size() + x));
    return py_retval;
}
'''
    Bar.add_custom_method_wrapper("Hooray", "_wrap_PyBar_Hooray_lenx",
                                  wrapper_body,
                                  flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])

------------------------

Can you help with this problem?

Provide an answer of your own, or ask fostex666 for more information if necessary.

To post a message you must log in.