Py_DECREF after an exception

Discussion in 'Python' started by Hrvoje =?UTF-8?Q?Nik=C5=A1i=C4=87?=, Jan 23, 2007.

  1. 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?
     
    Hrvoje =?UTF-8?Q?Nik=C5=A1i=C4=87?=, Jan 23, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Moles

    Py_DECREF Question:

    Jeremy Moles, Aug 15, 2005, in forum: Python
    Replies:
    2
    Views:
    418
    Raymond Hettinger
    Aug 15, 2005
  2. Kevin D. Smith

    Py_DECREF/Py_INCREF as Functions?

    Kevin D. Smith, Mar 31, 2007, in forum: Python
    Replies:
    1
    Views:
    334
    Gabriel Genellina
    Mar 31, 2007
  3. Replies:
    1
    Views:
    445
    Thomas Heller
    Jan 17, 2008
  4. Ecir Hana
    Replies:
    2
    Views:
    568
    Ecir Hana
    Dec 27, 2009
  5. Chris Angelico
    Replies:
    0
    Views:
    309
    Chris Angelico
    May 6, 2011
Loading...

Share This Page