PEP 415 – Implement context suppression with exception attributes
- Benjamin Peterson <benjamin at python.org>
- Nick Coghlan
- Standards Track
- Python-Dev message
PEP 409 introduced support for the
raise exc from None construct to
allow the display of the exception context to be explicitly suppressed.
This PEP retains the language level changes already implemented in PEP 409,
but replaces the underlying implementation mechanism with a simpler approach
based on a new
__suppress_context__ attribute on all
This PEP was accepted by Nick Coghlan on the 14th of May, 2012.
PEP 409 changes
__cause__ to be
Ellipsis by default. Then if
__cause__ is set to
raise exc from None, no context or cause
will be printed should the exception be uncaught.
The main problem with this scheme is it complicates the role of
__cause__ should indicate the cause of the exception not
__context__ should be printed or not. This use of
also not easily extended in the future. For example, we may someday want to
allow the programmer to select which of
be printed. The PEP 409 implementation is not amenable to this.
The use of
Ellipsis is a hack. Before PEP 409,
Ellipsis was used
exclusively in extended slicing. Extended slicing has nothing to do with
exceptions, so it’s not clear to someone inspecting an exception object why
__cause__ should be set to
Ellipsis by default for
__cause__ makes it asymmetrical with
A new attribute on
be introduced. Whenever
__cause__ is set,
will be set to
True. In particular,
raise exc from cause
syntax will set
printing code will check for that attribute to determine whether
context and cause will be printed.
__cause__ will return to its
original purpose and values.
There is precedence for
__suppress_context__ with the
print_line_and_file exception attribute.
raise exc from cause will be equivalent to:
exc.__cause__ = cause raise exc
exc.__cause__ = cause implicitly sets
There is a patch on Issue 14133.
This document has been placed in the public domain.
Last modified: 2022-01-21 11:03:51 GMT