Some C-API functions clear the error indicator?

Discussion in 'Python' started by Austin Bingham, Jan 29, 2010.

  1. I've noticed that several (many?) python functions seem to clear the
    error/exception indicators when they're called from a C/C++ program.
    For example, both PyImport_ImportModule and traceback.extract_tb()
    (called via the function call methods) do this: if error indicators
    are set prior to their call (as indicated by PyErr_Fetch, and
    including a call to PyErr_Restore), I see that they are unset (using
    the same method) after the call. This happens even when the functions
    succeed.

    The functions that do this don't seem to indicate in their
    documentation that this will happen. So first, does anyone know why
    this is happening? Is it because of the context in which I'm making
    the calls? Is there any pattern or reason behind which functions will
    do this? Or am I just doing something wrong?

    If the problem is context-dependent (e.g. I haven't properly
    established a call stack, or something of that flavor), any pointers
    on doing things properly would be great.

    Here's some example code demonstrating the problem:

    ---

    #include <Python.h>

    int main(int argc, char** argv)
    {
    Py_Initialize();

    // Cause an IndexError
    PyObject* list = PyList_New(0);
    PyObject* obj = PyList_GetItem(list, 100);

    PyObject *t = NULL, *v = NULL, *tb = NULL;

    // Verify that we see the error
    PyErr_Fetch(&t, &v, &tb);
    assert(t);
    PyErr_Restore(t, v, tb);

    // Import a module, which seems to be clearing the error indicator
    PyObject* mod = PyImport_ImportModule("sys");
    assert(PyObject_HasAttrString(mod, "path"));

    // Verify that the error indicator has been cleared
    PyErr_Fetch(&t, &v, &tb);
    assert(!t); // <=== The error is gone!
    PyErr_Restore(t, v, tb);

    Py_Finalize();

    return 0;
    }

    ---

    Thanks in advance.

    Austin
     
    Austin Bingham, Jan 29, 2010
    #1
    1. Advertising

  2. That makes a lot of sense. And if I take the approach that any Py*
    function might do this, it actually looks like I can simplify my code
    (rather than managing some list of ill-behaved functions or
    something.) Thanks!

    On Fri, Jan 29, 2010 at 3:58 PM, Duncan Booth
    <> wrote:
    > Austin Bingham <> wrote:
    >
    >> The functions that do this don't seem to indicate in their
    >> documentation that this will happen. So first, does anyone know why
    >> this is happening? Is it because of the context in which I'm making
    >> the calls? Is there any pattern or reason behind which functions will
    >> do this? Or am I just doing something wrong?
    >>

    > (Just guessing here)
    > I would expect that any function that executes Python code will clear the
    > error.
    >
    > I think that has to happen otherwise the Python code will throw an
    > exception whenever it gets round to checking for errors. In the past I've
    > found that if you fail to check for an error in C code before returning to
    > the interpreter you get the exception thrown a few instructions later, so
    > something similar would probably happen if you call other Python code from
    > C.
    >
    > If it is anything that executes Python then that would include any function
    > that creates or destroys an object with Python constructor or destructor
    > code. or that compares or otherwise operates on instances defined in
    > Python. In particular it might mean that any function that doesn't appear
    > to clear the error could do so in a slightly different situation.
    >
    > --
    > Duncan Booth http://kupuguy.blogspot.com
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Austin Bingham, Jan 30, 2010
    #2
    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. Che
    Replies:
    0
    Views:
    372
  2. Replies:
    2
    Views:
    1,242
    Hendrik Maryns
    Nov 27, 2007
  3. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,070
    Mark Fitzpatrick
    Jan 31, 2008
  4. Replies:
    67
    Views:
    1,327
    Antoninus Twink
    Aug 7, 2008
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    1,009
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page