Should I always call PyErr_Clear() when an exception occurs?

Discussion in 'Python' started by Jaime Wyant, Dec 21, 2004.

  1. Jaime Wyant

    Jaime Wyant Guest

    I've found that the code below will crash if I don't have the
    PyErr_Clear() function call. Should I always call PyErr_Clear()? The
    error message I get has to do with garbage collection -->

    Exception exceptions.ImportError: 'No module named badmodule' in 'garbage collec
    tion' ignored
    Fatal Python error: unexpected exception during garbage collection

    /* Code that fails if PyErr_Clear() is removed */
    #include "python.h"

    int main()
    {
    PyObject *pName, *pModule;
    int i;

    Py_Initialize();
    for (i = 0 ; i < 30; i++) {
    // First, import the module
    pName = PyString_FromString("badmodule");
    pModule = PyImport_Import(pName);
    Py_DECREF(pName);
    if (!pModule)
    {
    fprintf(stderr, "couldn't import badmodule\n");
    PyErr_Clear();
    }
    }

    Py_Finalize();
    }

    /* End code */

    Thanks,
    jw
    Jaime Wyant, Dec 21, 2004
    #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. Tony Cheng
    Replies:
    5
    Views:
    839
    Tony Cheng
    Mar 23, 2005
  2. Robin Becker
    Replies:
    3
    Views:
    393
    Andrew MacIntyre
    Nov 29, 2004
  3. Tim Peters
    Replies:
    0
    Views:
    606
    Tim Peters
    Dec 21, 2004
  4. Replies:
    0
    Views:
    556
  5. Newb Newb
    Replies:
    3
    Views:
    96
    7stud --
    Apr 29, 2009
Loading...

Share This Page