Py_DECREF after an exception

Hrvoje Nikšić hrvoje.niksic at avl.com
Tue Jan 23 10:36:59 EST 2007


I'm wondering what happens with the exception info during object cleanup
immediately after an exception is thrown.  Consider this code:

  PyObject *args = PyBuild_Value("(O(O){})", name, parent);
  if (!args)
    return NULL;
  PyObject *val = some_python_func(x, args, NULL);
  Py_DECREF(args);
  if (!val)
    return NULL;

The idea is to propagate the exception possibly raised by
some_python_func and at the same time avoid leaking memory.  But
Py_DECREF can cause arbitrary Python code to get executed, including the
code that eventually ends up calling PyErr_Clear when it wants to ignore
some unrelated exception.  This could cause exception information to be
forgotten.

Is there a way around this, or is there a reason why this is not a
problem in practice?






More information about the Python-list mailing list