New submission from STINNER Victor:

In Python 3, it becomes possible to chain two exceptions. It's one of the 
killer feature of Python 3, it helps debugging.


In Python, exceptions are chained by default. Example:

    try:
        raise TypeError("old message")
    except TypeError:
        raise ValueError("new message")

Output:

    Traceback (most recent call last):
      File "x.py", line 2, in <module>
        raise TypeError("old message")
    TypeError: old message

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "x.py", line 4, in <module>
        raise ValueError("new message")
    ValueError: new message

In C, using the public PyErr_SetNone(), PyErr_Format(), PyErr_SetString(), ... 
functions, exceptions are *not* chained by default.

You have to call explicitly the new private _PyErr_ChainExceptions() function 
introduced in Python 3.4. It is not trivial to use it: you have to call 
PyErr_Fetch() and check if an exception was already raised.


In Python, the following examples are bad practice because they may hide 
important exceptions like MemoryError or KeyboardInterrupt:

    try:
        ....
    except:
        pass

or

    try:
        ....
    except:
        raise ValueError(...)

In C extensions, it's common to write such code, few functions check which 
exception was raised.


Last months, I enhanced Python to detect exceptions ignored by mistake: I added 
assert(!PyErr_Occurred()) in many functions which can clear the current 
exception (ex: call PyErr_Clear()) or raise a new exception (ex: call 
PyErr_SetString(...)). The last step is the issue #23571 which now implements 
in release mode.


For the next step, I propose to explicitly clear the current exception before 
raising a new exception.


I don't know yet if it would be a good idea to modify PyErr_*() functions to 
automatically chain exceptions.

----------
messages: 239130
nosy: haypo
priority: normal
severity: normal
status: open
title: Chain exceptions in C

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23763>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to