Inconsistency in retrieving exceptions via sys module

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue May 13 02:56:10 EDT 2008


En Mon, 12 May 2008 13:47:45 -0300, Raj Bandyopadhyay <rajb at rice.edu>  
escribió:

> 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.

The sys.exc_* variables are just for backwards compatibility, don't use  
them; use sys.exc_info() in Python code and PyErr_Occurred (and the other  
PyErr... functions) in C. (Note that even PyErr_Occurred is not so common  
in C code, as most functions have some way to say "there was an error"; by  
example, functions that return a PyObject* mark an error returning NULL).

The sys.exc_* variables are set only at the end of each Python bytecode  
instruction (see ceval.c)

-- 
Gabriel Genellina




More information about the Python-list mailing list