Inconsistency in retrieving exceptions via sys module

Raj Bandyopadhyay rajb at rice.edu
Mon May 12 12:47:45 EDT 2008


Hi

I am writing some C code which sets and retrieves a Python exception. I 
set the exception using PyErr_SetString(), and retrieve it in 2 ways: 1) 
using PyErr_Occurred() and 2) Using sys.exc_type. However, I get two 
different results and am very puzzled by this apparent inconsistency. 
Could anyone please clarify what I'm missing? I need both to work 
consistently for my application.

Here is the C code and the output

#include "Python.h"

#define OPRINT(o) PyObject_Print((o),stdout,0);printf("\n")

int main() {
    PyObject *sysmod,*etype1,*etype2;

    Py_Initialize();
    sysmod = PyImport_ImportModule("sys");
   
    PyErr_SetString(PyExc_Exception,"HI");      //set exception

    etype1 = PyObject_GetAttrString(sysmod,"exc_type");  //sys.exc_type
    etype2 = PyErr_Occurred();

    printf("sys.exc_type : "); OPRINT(etype1);
    printf("PyErr_Occurred() : "); OPRINT(etype2);
}


The output

sys.exc_type : None
PyErr_Occurred() : <type 'exceptions.Exception'>


Thank you
Raj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080512/b8016e14/attachment.html>


More information about the Python-list mailing list