how to raise exceptions when returning std::shared_ptr?
I'm having an issue when I'm trying to raise an exception inside a
swig wrapped function. I think that this is because Python requires
you to return a NULL to indicate an exception, but for functions
returning std::shared_ptr SWIG always returns an object.
here is my source. when it fails, it sets an error string and
8215 p_teca_table as_teca_table(p_teca_dataset
8217 p_teca_table o_inst =
8218 if (!o_inst)
8220 teca_py_gil_state gil;
8222 "Failed to convert from \"%s\" to \"%s\"",
8224 return o_inst;
on line 63093, SWIG constructs an object. This prevents Python
interpreter from noticing the error.
Here is an example where I set up an exception handler and
intentionally use the above to trigger the exception, but the
exception never occurs. Very confusingly the error is only reported
by a later Python call! or if in an interactive interpreter session
if I hit enter.
Python 2.7.12 (default, Sep 27 2016, 11:20:58)
[GCC 5.3.1 20160406 (Red Hat 5.3.1-6)] on linux2
Type "help", "copyright", "credits" or "license" for more
>>> from teca import *
>>> import sys
>>> def convert(ds):
... tab = as_teca_table(ds)
... sys.stderr.write('caught the exception!')
... return tab
>>> ds = teca_cartesian_mesh.New()
>>> a = convert(ds)
then if I hit enter I get the error report printed in the terminal,
but still no exception
TypeError: Failed to convert from "teca_dataset" to
I think that Python is not recognizing the exception occurred
because the swig generated code never returns a NULL.
My question is: shouldn't the swig wrapper return NULL if the smart
pointer points to a nullptr?
if not then the question is how does one raise an exception in
functions that return std::shared_ptr?
Re: how to raise exceptions when returning std::shared_ptr?
> I'm having an issue when I'm trying to raise an exception inside a swig wrapped
> function. I think that this is because Python requires you to return a NULL to indicate
> an exception, but for functions returning std::shared_ptr SWIG always returns an object.