Shift pseudo-code to Transition Plan section and provide a pseudo-code
implementation of what BaseException will look like in Python 3.0 .
This commit is contained in:
parent
cc5472f135
commit
1c4f4d3f18
135
pep-0352.txt
135
pep-0352.txt
|
@ -2,7 +2,8 @@ PEP: 352
|
|||
Title: Required Superclass for Exceptions
|
||||
Version: $Revision$
|
||||
Last-Modified: $Date$
|
||||
Author: Brett Cannon <brett@python.org>, Guido van Rossum <guido@python.org>
|
||||
Author: Brett Cannon <brett@python.org>
|
||||
Guido van Rossum <guido@python.org>
|
||||
Status: Final
|
||||
Type: Standards Track
|
||||
Content-Type: text/x-rst
|
||||
|
@ -53,70 +54,36 @@ Requiring a Common Superclass
|
|||
|
||||
This PEP proposes introducing a new exception named BaseException that
|
||||
is a new-style class and has a single attribute, ``message`` (that
|
||||
will cause the deprecation of the existing ``args`` attribute)::
|
||||
will cause the deprecation of the existing ``args`` attribute) Below
|
||||
is the code as the exception will work in Python 3.0 (how it will
|
||||
work in Python 2.x is covered in the `Transition Plan`_ section)::
|
||||
|
||||
class BaseException(object):
|
||||
|
||||
"""Superclass representing the base of the exception hierarchy.
|
||||
|
||||
Provides a 'message' attribute that contains any single argument
|
||||
passed in during instantiation. If more than one argument is passed, it
|
||||
is set to the empty string. It is meant to represent any message
|
||||
(usually some text) that should be printed out with the traceback.
|
||||
Unfortunately, for backwards-compatibility, the 'args' attribute
|
||||
(discussed below) is used for printing out to tracebacks.
|
||||
|
||||
The 'args' attribute and __getitem__ method are provided for
|
||||
backwards-compatibility and will be deprecated at some point.
|
||||
Provides a 'message' attribute that contains either the single
|
||||
argument to the constructor or the empty string. This attribute
|
||||
is used in both the string and unicode representation for the
|
||||
exception. This is so that it provides the extra details in the
|
||||
traceback.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Set 'message' and 'args' attribute.
|
||||
|
||||
'args' will eventually be deprecated. But it is still used when
|
||||
printing out tracebacks for backwards-compatibility. Once 'args' is
|
||||
removed, though, 'message' will be used instead.
|
||||
|
||||
"""
|
||||
self.args = args
|
||||
self.message = args[0] if args else ''
|
||||
def __init__(self, message=''):
|
||||
"""Set the 'message' attribute'"""
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
"""Return the str of args[0] or args, depending on length.
|
||||
|
||||
Once 'args' has been removed, 'message' will be used exclusively for
|
||||
the str representation for exceptions.
|
||||
|
||||
"""
|
||||
return str(self.args[0]
|
||||
if len(self.args) <= 1
|
||||
else self.args)
|
||||
"""Return the str of 'message'"""
|
||||
return str(self.message)
|
||||
|
||||
def __unicode__(self):
|
||||
"""Return the unicode of args[0] or args, depending on length.
|
||||
|
||||
Once 'args' has been removed, 'message' will be used exclusively for
|
||||
the unicode representation of exceptions.
|
||||
|
||||
"""
|
||||
return unicode(self.args[0]
|
||||
if len(self.args) <= 1
|
||||
else self.args)
|
||||
"""Return the unicode of 'message'"""
|
||||
return unicode(self.message)
|
||||
|
||||
def __repr__(self):
|
||||
func_args = repr(self.args) if self.args else "()"
|
||||
return self.__class__.__name__ + func_args
|
||||
|
||||
def __getitem__(self, index):
|
||||
"""Index into arguments passed in during instantiation.
|
||||
|
||||
Provided for backwards-compatibility and will be
|
||||
deprecated.
|
||||
|
||||
"""
|
||||
return self.args[index]
|
||||
|
||||
return "%s(%s)" % (self.__class__.__name__, repr(self.message))
|
||||
|
||||
The ``message`` attribute will contain either the first argument
|
||||
passed in at instantiation of the object or the empty string if no
|
||||
|
@ -212,6 +179,72 @@ semantics starting in the version following the introduction of the
|
|||
deprecation and the raising of a DeprecationWarning for the version
|
||||
specifically listed.
|
||||
|
||||
Here is BaseException as implemented in the 2.x series::
|
||||
|
||||
class BaseException(object):
|
||||
|
||||
"""Superclass representing the base of the exception hierarchy.
|
||||
|
||||
Provides a 'message' attribute that contains any single argument
|
||||
passed in during instantiation. If more than one argument is
|
||||
passed, it is set to the empty string. It is meant to represent
|
||||
any message (usually some text) that should be printed out with
|
||||
the traceback. Unfortunately, for backwards-compatibility, the
|
||||
'args' attribute (discussed below) is used for printing out to
|
||||
tracebacks.
|
||||
|
||||
The 'args' attribute and __getitem__ method are provided for
|
||||
backwards-compatibility and will be deprecated at some point.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Set 'message' and 'args' attribute.
|
||||
|
||||
'args' will eventually be deprecated. But it is still used
|
||||
when printing out tracebacks for backwards-compatibility.
|
||||
Once 'args' is removed, though, 'message' will be used instead.
|
||||
|
||||
"""
|
||||
self.args = args
|
||||
self.message = args[0] if args else ''
|
||||
|
||||
def __str__(self):
|
||||
"""Return the str of args[0] or args, depending on length.
|
||||
|
||||
Once 'args' has been removed, 'message' will be used
|
||||
exclusively for the str representation for exceptions.
|
||||
|
||||
"""
|
||||
return str(self.args[0]
|
||||
if len(self.args) <= 1
|
||||
else self.args)
|
||||
|
||||
def __unicode__(self):
|
||||
"""Return the unicode of args[0] or args, depending on length.
|
||||
|
||||
Once 'args' has been removed, 'message' will be used
|
||||
exclusively for the unicode representation of exceptions.
|
||||
|
||||
"""
|
||||
return unicode(self.args[0]
|
||||
if len(self.args) <= 1
|
||||
else self.args)
|
||||
|
||||
def __repr__(self):
|
||||
func_args = repr(self.args) if self.args else "()"
|
||||
return self.__class__.__name__ + func_args
|
||||
|
||||
def __getitem__(self, index):
|
||||
"""Index into arguments passed in during instantiation.
|
||||
|
||||
Provided for backwards-compatibility and will be
|
||||
deprecated.
|
||||
|
||||
"""
|
||||
return self.args[index]
|
||||
|
||||
|
||||
Deprecation of features in Python 2.9 is optional. This is because it
|
||||
is not known at this time if Python 2.9 (which is slated to be the
|
||||
last version in the 2.x series) will actively deprecate features that
|
||||
|
|
Loading…
Reference in New Issue