Newbie question: Specifying custom parameter types

Asked by T Powers

Suppose I have a .h that looks like this;

 extern PIX * pixCreate ( l_int32 width, l_int32 height, l_int32 depth );
 extern void pixDestroy ( PIX **ppix );

 extern PIX * pixClone ( PIX *pixs );
 extern PIX * pixCopy ( PIX *pixd, PIX *pixs );

I tried

    pix = mod.add_class('PIX')
    mod.add_function('pixCreate',
                     ReturnValue.new('PIX *', caller_owns_return=True),
                     [Parameter.new('l_int32', 'width'),
                      Parameter.new('l_int32', 'height'),
                      Parameter.new('l_int32', 'depth'),
                      ]
                     )
    mod.add_function('pixDestroy',
                     None,
                     [Parameter.new('PIX **', 'ppix'),
                      ]
                     )

But that results in:

 pybindgen.typehandlers.base.TypeLookupError: ['l_int32']
 File "modulegen.py", line 41, in <module>
   my_module_gen(sys.stdout)
 File "modulegen.py", line 27, in my_module_gen
   [Parameter.new('l_int32', 'width'),
 File "c:\Python26\Lib\site-packages\pybindgen\typehandlers\base.py", line 1241, in new
   param_type_matcher.lookup(ctype)
 File "c:\Python26\Lib\site-packages\pybindgen\typehandlers\base.py", line 1404, in lookup
   raise TypeLookupError(tried_names)

Also pixDestroy() is going to want the address of the pointer returned by pixCreate(). How would one specify that parameter?

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

For l_int32 case:

  from pybindgen.typehandlers import base as typehandlers
  typehandlers.add_type_alias('int32_t', 'l_int32')

For the PIX **, you're out of luck. You'd have to make your own type handler, which is not trivial and not well documented. If it's only that one method, maybe you're better off just manually writing a custom wrapper. Something along these lines:

    wrapper_body = '''
static PyObject *
_wrap_Pix_Destroy(PyPIX *self, PyObject *args, PyObject *kwargs,
                        PyObject **return_exception)
{
    PyObject *py_retval;
    const char *keywords[] = {NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "", (char **) keywords)) {
        PyObject *exc_type, *traceback;
        PyErr_Fetch(&exc_type, return_exception, &traceback);
        Py_XDECREF(exc_type);
        Py_XDECREF(traceback);
        return NULL;
    }
    pixDestroy (&self->obj);
    Py_INCREF(Py_None);
    return Py_None;
}
'''
    pix.add_custom_method_wrapper("Destroy", "_wrap_Pix_Destroy",
                                  wrapper_body,
                                  flags=["METH_VARARGS", "METH_KEYWORDS"])

Revision history for this message
T Powers (tomp-2010+launchpad) said :
#2

Thanks for astonishingly quick answer!

Unfortunately, a quick search of the rest of the C library's header file that I am trying to access (see Leptonica.com if you're interested) from Python shows LOTS of instances of similar SOMETYPE ** parameters. Mainly seems to be used to return optional data back to caller.

I guess I'll look into Boost.Python, SWIG, or expy, but I really liked the idea of the generator being entirely written in python.

Can you help with this problem?

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

To post a message you must log in.