peps: Style updates to PEP 409.

http://hg.python.org/peps/rev/c1ed6327769a changeset: 4028:c1ed6327769a user: Georg Brandl <georg@python.org> date: Sun Jan 29 09:41:13 2012 +0100 summary: Style updates to PEP 409. files: pep-0409.txt | 78 +++++++++++++++++++++------------------ 1 files changed, 42 insertions(+), 36 deletions(-) diff --git a/pep-0409.txt b/pep-0409.txt --- a/pep-0409.txt +++ b/pep-0409.txt @@ -14,51 +14,54 @@ Abstract ======== -One of the open issues from PEP 3134 is suppressing context: currently there is -no way to do it. This PEP proposes one. +One of the open issues from PEP 3134 is suppressing context: currently +there is no way to do it. This PEP proposes one. + Motivation ========== -There are two basic ways to generate exceptions: 1) Python does it (buggy code, -missing resources, ending loops, etc.); and, 2) manually (with a raise -statement). +There are two basic ways to generate exceptions: 1) Python does it +(buggy code, missing resources, ending loops, etc.); and, 2) manually +(with a raise statement). -When writing libraries, or even just custom classes, it can become necessary to -raise exceptions; moreover it can be useful, even necessary, to change from one -exception to another. To take an example from my dbf module: +When writing libraries, or even just custom classes, it can become +necessary to raise exceptions; moreover it can be useful, even +necessary, to change from one exception to another. To take an +example from my dbf module:: try: value = int(value) except Exception: raise DbfError(...) -Whatever the original exception was (ValueError, TypeError, or something else) -is irrelevant. The exception from this point on is a DbfError, and the original -exception is of no value. However, if this exception is printed, we would -currently see both. +Whatever the original exception was (ValueError, TypeError, or +something else) is irrelevant. The exception from this point on is a +DbfError, and the original exception is of no value. However, if this +exception is printed, we would currently see both. Alternatives ============ + Several possibilities have been put forth: - - raise as NewException() +- ``raise as NewException()`` - Reuses the 'as' keyword; can be confusing since we are not really reraising - the originating exception + Reuses the 'as' keyword; can be confusing since we are not really reraising + the originating exception - - raise NewException() from None +- ``raise NewException() from None`` - Follows existing syntax of explicitly declaring the originating exception + Follows existing syntax of explicitly declaring the originating exception - - exc = NewException(); exc.__context__ = None; raise exc +- ``exc = NewException(); exc.__context__ = None; raise exc`` - Very verbose way of the previous method + Very verbose way of the previous method - - raise NewException.no_context(...) +- ``raise NewException.no_context(...)`` - Make context suppression a class method. + Make context suppression a class method. All of the above options will require changes to the core. @@ -66,17 +69,18 @@ Proposal ======== -I proprose going with the second option: +I proprose going with the second option:: - raise NewException from None + raise NewException from None -It has the advantage of using the existing pattern of explicitly setting -the cause: +It has the advantage of using the existing pattern of explicitly +setting the cause:: - raise KeyError() from NameError() + raise KeyError() from NameError() -but because the 'cause' is None the previous context is discarded. There is a -patch to this effect attached to issue 6210 [#issue6210]_. +but because the 'cause' is None the previous context is discarded. +There is a patch to this effect attached to issue 6210 [#issue6210]_. + References ========== @@ -84,17 +88,19 @@ .. [#issue6210] http://bugs.python.org/issue6210 + Copyright ========= This document has been placed in the public domain. - + + .. - Local Variables: - mode: indented-text - indent-tabs-mode: nil - sentence-end-double-space: t - fill-column: 70 - coding: utf-8 - End: + Local Variables: + mode: indented-text + indent-tabs-mode: nil + sentence-end-double-space: t + fill-column: 70 + coding: utf-8 + End: -- Repository URL: http://hg.python.org/peps
participants (1)
-
georg.brandl