Exception Formatter for Django
The django.contrib.exceptionformatter package provides an alternative traceback formatter for Django. The formatter implements parts of the traceback standard
library module and the ITracebackSupplement
interface of Zope3. Also see file interfaces.py.
Untar exceptionformatter-1.0.0.tar.gz into the django/contrib
directory
of your Django sandbox. To activate the formatter, add django.contrib.exceptionformatter
to the
INSTALLED_APPS list in your project's settings file.
Traceback Supplement
The traceback supplement facility allows the programmer to supplement exception tracebacks with additional information pertaining to the code under execution.
"The interface is geared toward providing meaningful feedback when exceptions occur in user code written in mini-languages like Zope page templates and restricted Python scripts."
To use the facility from your own code, create a class implementing the ITracebackSupplement interface (see interfaces.py). There is no restriction on the type of class or the number of constructor arguments, other than that, once instantiated, it must implement ITracebackSupplement (at least partially).
To supplement a frame, add a __traceback_supplement__ variable to the local namespace of a function or method. The variable must contain a tuple consisting of the supplement constructor and optional constructor arguments.
Example: __traceback_supplement__ = (SupplementClass, arg1, arg2, arg3)
The supplement is evaluated lazily, i.e. it is only instantiated in case
its data is needed to render an exception traceback. The formatter
instantiates the supplement by calling: SupplementClass(*args)
.
Example:
>>> from django.contrib.exceptionformatter import exceptionformatter >>> class Supplement: ... # implements(ITracebackSupplement) ... def __init__(self, context, argument): ... self.source_url = context.file ... self.line, self.column = context.pos ... self.expression = '%s + 1' % repr(argument) >>> class context: ... file = '/home/bedrock.com/flintstone/fred.pt' ... pos = (23, 42) >>> def increment(argument): ... __traceback_supplement__ = (Supplement, context, argument) ... return argument + 1 >>> increment(1) 2 >>> try: ... increment('a') ... except: ... print exceptionformatter.format_exc() Traceback (most recent call last): ... File "<doctest readme.txt...>", line ..., in increment return argument + 1 - /home/bedrock.com/flintstone/fred.pt - Line 23, Column 42 - Expression: 'a' + 1 TypeError: cannot concatenate 'str' and 'int' objects <BLANKLINE>
Traceback Info
Trackeback info is a quick and dirty way of supplementing a traceback. The value given in __traceback_info__ is included in the traceback as is.
Example:
>>> def increment(argument): ... __traceback_info__ = '%s + 1' % repr(argument) ... return argument + 1 >>> increment(1) 2 >>> try: ... increment('a') ... except: ... print exceptionformatter.format_exc() Traceback (most recent call last): ... File "<doctest readme.txt...>", line ..., in increment return argument + 1 - __traceback_info__: 'a' + 1 TypeError: cannot concatenate 'str' and 'int' objects <BLANKLINE>
Contact
ExceptionFormatter 1.0.0
(c) 2005, Stefan H. Holek, [email protected]
License: ZPL