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

J

Jaime Wyant

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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top