[C++-sig] How to handle argument error exceptions in boost.python?

Karl Bicker karl.bicker at gordiano.net
Fri Jan 10 14:36:48 CET 2014


Dear Gary,

I am not sure if this is what you want, but what about writing a wrapper function around EXTrender_effect which takes boost::python::objects and then use boost::python::extract to check if you can convert the objects you got to what they should be (cf. https://wiki.python.org/moin/boost.python/extract)? Does this help?

Regards,
Charly

----- Original Message -----
From: "Gary Oberbrunner" <garyo at genarts.com>
To: "Development of Python/C++ integration" <cplusplus-sig at python.org>
Sent: Friday, January 10, 2014 2:16:16 PM
Subject: [C++-sig] How to handle argument error exceptions in boost.python?

[I posted this on StackOverflow yesterday, but got nothing.  Hoping you will have some insight!]


I'm writing a C++ python extension.  I have a boost::python extension function

static void EXTrender_effect(EffectGlobals_t *effect_handle, 
                    std::string preset,
            bp::object dest, int xdim, int ydim)
{ ... }
that I export as usual in my boost.python extension:

def("render_effect", EXTrender_effect);

When I call that from python(2.7), I get a C++ exception boost::python::error_already_set. Tracing that down in Visual Studio, I can see it's coming from a boost::python::objects::function::argument_error. So OK, I have an argument error; I'm probably calling it with the wrong args. What I'd like to do is print or throw something sensible in my python extension when this happens, so users of my extension will see the nice message that I know is lurking in PyErr_Fetch. (I can see the message getting built in the boost.python argument_error code.)

But I can't catch that error_already_set; boost.python does that internally around the funcall. And I can't check for python errors in my extension code, since my function never gets called (the argument error is detected by boost.python). What ends up happening is it just silently fails.

What can I hook up so I (or my users) can see the argument error messages? And ideally convert those into python exceptions?

This is all Win7, Python2.7.

-- 
. . . . . . . . . . . . . . . . . . . . . .
Gary Oberbrunner           garyo at genarts.com
VP Engineering             Tel: 617-492-2888
GenArts, Inc.              www.genarts.com 
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig at python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list