Py_DECREF after an exception

  • Thread starter Hrvoje =?UTF-8?Q?Nik=C5=A1i=C4=87?=
  • Start date
H

Hrvoje =?UTF-8?Q?Nik=C5=A1i=C4=87?=

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top