Python callbacks & PyGILState_Release()

Randall Hopper viznut at charter.net
Sat Apr 23 10:08:14 EDT 2005


Thomas Heller:
 |>      Python -> C++ -> Python Callback
 |>
 |> (example attached) an exception raised in the callback doesn't make it back
 |> across C++ to Python.
...
 |> void callback_wrapper( void *user_data )
 |> {
 |>   // Acquire interpreter lock
 |>   PyGILState_STATE gstate = PyGILState_Ensure();
 |>   ...
 |>   // Call Python
 |>   pyresult = PyEval_CallObject( pyfunc, pyargs );
 |>   ...
 |
 |    if (pyresult == NULL)
 |        PyErr_Print();
 |
 |>   // Free interpreter lock
 |>   PyGILState_Release(gstate);
 |> }
 |
 |PyErr_Print() will do the 'right' thing?s.

Thanks for the reply.  However, this won't:

   a) Stop the main Python script, and
   b) Print the full stack trace (including Python and C++ SWIG wrapper)

Is there a clean way to save the full exception state in the callback
before the PyGILState_Release(), and restore it when we return across the
C++ wrapper?

If I knew what the proper "save" and "restore" exception state code bits
were, I could easily implement this with exception typemaps in SWIG.

Thanks,

Randall

P.S. Perhaps PyGILState_Release should take an argument instructing it to
exclude exception state when resetting the interpreter state back to its
original state.



More information about the Python-list mailing list