[Python-Dev] with_traceback
Andrew Dalke
dalke at dalkescientific.com
Fri Mar 2 23:58:52 CET 2007
On 3/1/07, Guido van Rossum <guido at python.org> wrote:
> Since by far the most common use case is to create the
> exception in the raise statement, the behavior there won't be any
> different than it is today; the traceback on precreated objects will
> be useless, but folks who precreate them for performance reasons
> presumably won't care; and those that create global exception
> instances by mistakenly copying the wrong idiom, well, they'll learn
> quickly (and a lot more quickly than when we try to override the
> exception).
Here's a few more examples of code which don't follow the idiom
raise ExceptionClass(args)
Zope's ZConfig/cmdline.py
def addOption(self, spec, pos=None):
if pos is None:
pos = "<command-line option>", -1, -1
if "=" not in spec:
e = ZConfig.ConfigurationSyntaxError(
"invalid configuration specifier", *pos)
e.specifier = spec
raise e
The current xml.sax.handler.Error handler includes
def error(self, exception):
"Handle a recoverable error."
raise exception
def fatalError(self, exception):
"Handle a non-recoverable error."
raise exception
and is used like this in xml.sax.expatreader.ExpatParser.feed
try:
# The isFinal parameter is internal to the expat reader.
# If it is set to true, expat will check validity of the entire
# document. When feeding chunks, they are not normally final -
# except when invoked from close.
self._parser.Parse(data, isFinal)
except expat.error, e:
exc = SAXParseException(expat.ErrorString(e.code), e, self)
# FIXME: when to invoke error()?
self._err_handler.fatalError(exc)
Note that the handler may decide to ignore the exception,
based on which error occured. The traceback should show
where in the handler the exception was raised, and not
the point at which the exception was created.
ZODB/Connection.py:
...
if isinstance(store_return, str):
assert oid is not None
self._handle_one_serial(oid, store_return, change)
else:
for oid, serial in store_return:
self._handle_one_serial(oid, serial, change)
def _handle_one_serial(self, oid, serial, change):
if not isinstance(serial, str):
raise serial
...
Andrew
dalke at dalkescientific.com
More information about the Python-Dev
mailing list