[Checkins] SVN: zope.exceptions/trunk/src/zope/exceptions/ Revert
changed for the 3.5.0 branch,
because they made the logs harder to read.
Aaron Lehmann
aaron at zope.com
Mon Apr 28 10:53:56 EDT 2008
Log message for revision 85808:
Revert changed for the 3.5.0 branch, because they made the logs harder to read.
Changed:
U zope.exceptions/trunk/src/zope/exceptions/exceptionformatter.py
U zope.exceptions/trunk/src/zope/exceptions/log.py
U zope.exceptions/trunk/src/zope/exceptions/tests/test_exceptionformatter.py
-=-
Modified: zope.exceptions/trunk/src/zope/exceptions/exceptionformatter.py
===================================================================
--- zope.exceptions/trunk/src/zope/exceptions/exceptionformatter.py 2008-04-28 14:52:49 UTC (rev 85807)
+++ zope.exceptions/trunk/src/zope/exceptions/exceptionformatter.py 2008-04-28 14:53:56 UTC (rev 85808)
@@ -28,16 +28,15 @@
line_sep = '\n'
show_revisions = 0
- def __init__(self, limit=None, with_filenames=False, format=None):
+ def __init__(self, limit=None, with_filenames=False):
self.limit = limit
self.with_filenames = with_filenames
- self.format = format
def escape(self, s):
return s
def getPrefix(self):
- return self.formatText('Traceback (most recent call last):')
+ return 'Traceback (most recent call last):'
def getLimit(self):
limit = self.limit
@@ -119,12 +118,12 @@
s = s + ', in %s' % name
result = []
- result.append(self.escape(self.formatText(s)))
+ result.append(self.escape(s))
# Append the source line, if available
line = linecache.getline(filename, lineno)
if line:
- result.append(self.escape(self.formatText(" " + line.strip())))
+ result.append(" " + self.escape(line.strip()))
# Output a traceback supplement, if any.
if '__traceback_supplement__' in locals:
@@ -146,6 +145,7 @@
if DEBUG_EXCEPTION_FORMATTER:
traceback.print_exc()
# else just swallow the exception.
+
try:
tbi = locals.get('__traceback_info__', None)
if tbi is not None:
@@ -158,25 +158,13 @@
return self.line_sep.join(result)
def formatExceptionOnly(self, etype, value):
- result = ''.join(
- [self.formatText(line) for line in traceback.format_exception_only(etype, value)]
- # traceback.format_exception_only(etype, value)
- )
- return result
+ result = ''.join(traceback.format_exception_only(etype, value))
+ return result.replace('\n', self.line_sep)
def formatLastLine(self, exc_line):
return self.escape(exc_line)
- def formatText(self, text):
- if self.format:
- args = self.format[1].__dict__.copy()
- args['message'] = text
- line = self.format[0] % args
- else:
- line = text
- return line
-
- def formatException(self, etype, value, tb, format=None):
+ def formatException(self, etype, value, tb):
# The next line provides a way to detect recursion.
__exception_formatter__ = 1
result = [self.getPrefix() + '\n']
@@ -201,11 +189,10 @@
line_sep = '<br />\r\n'
def escape(self, s):
- return cgi.escape(super(HTMLExceptionFormatter, self).escape(s))
+ return cgi.escape(s)
def getPrefix(self):
- return '<p>%s\r\n<ul>' % super(
- HTMLExceptionFormatter, self).getPrefix()
+ return '<p>Traceback (most recent call last):\r\n<ul>'
def formatSupplementLine(self, line):
return '<b>%s</b>' % self.escape(str(line))
@@ -220,11 +207,11 @@
return '<li>%s</li>' % line
def formatLastLine(self, exc_line):
- return '</ul>%s</p>' % self.escape(exc_line).replace('\n', self.line_sep)
+ return '</ul>%s</p>' % self.escape(exc_line)
def format_exception(t, v, tb, limit=None, as_html=False,
- with_filenames=False, format=None):
+ with_filenames=False):
"""Format a stack trace and the exception information.
Similar to 'traceback.format_exception', but adds supplemental
@@ -232,14 +219,14 @@
and 'with_filenames'.
"""
if as_html:
- fmt = HTMLExceptionFormatter(limit, with_filenames, format=format)
+ fmt = HTMLExceptionFormatter(limit, with_filenames)
else:
- fmt = TextExceptionFormatter(limit, with_filenames, format=format)
+ fmt = TextExceptionFormatter(limit, with_filenames)
return fmt.formatException(t, v, tb)
def print_exception(t, v, tb, limit=None, file=None, as_html=False,
- with_filenames=True, format=None):
+ with_filenames=True):
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
Similar to 'traceback.print_exception', but adds supplemental
@@ -248,6 +235,6 @@
"""
if file is None:
file = sys.stderr
- lines = format_exception(t, v, tb, limit, as_html, with_filenames, format=format)
+ lines = format_exception(t, v, tb, limit, as_html, with_filenames)
for line in lines:
file.write(line)
Modified: zope.exceptions/trunk/src/zope/exceptions/log.py
===================================================================
--- zope.exceptions/trunk/src/zope/exceptions/log.py 2008-04-28 14:52:49 UTC (rev 85807)
+++ zope.exceptions/trunk/src/zope/exceptions/log.py 2008-04-28 14:53:56 UTC (rev 85808)
@@ -21,55 +21,14 @@
from zope.exceptions.exceptionformatter import print_exception
class Formatter(logging.Formatter):
- def __init__(self, fmt=None, datefmt=None, as_html=0,):
- logging.Formatter.__init__(self, fmt=fmt, datefmt=datefmt)
- self.as_html = as_html
- def format(self, record):
- record.message = record.getMessage()
- if self._fmt.find("%(asctime)") >= 0:
- record.asctime = self.formatTime(record, self.datefmt)
- cdict = record.__dict__.copy()
- lines = []
- for line in record.message.splitlines():
- cdict['message'] = line
- lines.append(self._fmt % cdict)
- s = '\n'.join(lines)
- if record.exc_info:
- # Cache the traceback text to avoid converting it multiple times
- # (it's constant anyway)
- if not record.exc_text:
- record.exc_text = self.formatException(record.exc_info, record=record)
- if record.exc_text:
- s = record.exc_text
- else:
- cdict = record.__dict__.copy()
- lines = []
- for line in record.message.splitlines():
- cdict['message'] = line
- lines.append(self._fmt % cdict)
- s = '\n'.join(lines)
- return s
-
- def formatException(self, ei, record=None):
+ def formatException(self, ei):
"""Format and return the specified exception information as a string.
Uses zope.exceptions.exceptionformatter to generate the traceback.
"""
sio = cStringIO.StringIO()
- if record:
- format = (self._fmt, record)
- else:
- format = None
-
- print_exception(
- ei[0],
- ei[1],
- ei[2],
- file=sio,
- as_html=self.as_html,
- with_filenames=True,
- format=format)
+ print_exception(ei[0], ei[1], ei[2], file=sio, with_filenames=True)
s = sio.getvalue()
if s.endswith("\n"):
s = s[:-1]
Modified: zope.exceptions/trunk/src/zope/exceptions/tests/test_exceptionformatter.py
===================================================================
--- zope.exceptions/trunk/src/zope/exceptions/tests/test_exceptionformatter.py 2008-04-28 14:52:49 UTC (rev 85807)
+++ zope.exceptions/trunk/src/zope/exceptions/tests/test_exceptionformatter.py 2008-04-28 14:53:56 UTC (rev 85808)
@@ -16,31 +16,19 @@
$Id$
"""
import sys
-import unittest
-import zope.testing.doctest
+from unittest import TestCase, main, makeSuite
-import zope.exceptions.log
from zope.exceptions.exceptionformatter import format_exception
from zope.testing.cleanup import CleanUp # Base class w registry cleanup
-import logging
-import StringIO
-def tb(as_html=0, format=None):
- log = logging.getLogger('')
- fake_stdout = StringIO.StringIO()
- hndler = logging.StreamHandler(fake_stdout)
- hndler.setFormatter(zope.exceptions.log.Formatter(as_html=as_html, fmt=format))
- log.addHandler(hndler)
-
+def tb(as_html=0):
t, v, b = sys.exc_info()
try:
- log.exception('')
- s = fake_stdout.getvalue()
- return s
+ return ''.join(format_exception(t, v, b, as_html=as_html))
finally:
del b
- log.removeHandler(hndler)
+
class ExceptionForTesting (Exception):
pass
@@ -58,7 +46,7 @@
-class Test(CleanUp, unittest.TestCase):
+class Test(CleanUp, TestCase):
def testBasicNamesText(self, as_html=0):
try:
@@ -172,47 +160,9 @@
' ^',
'SyntaxError: unexpected EOF while parsing'])
- def testFormattedExceptionText(self):
- try:
- exec 'syntax error'
- except SyntaxError, se:
- s = tb(format="Hello, World! %(message)s")
- # Hello, World! Traceback (most recent call last):
- # Hello, World! Module zope.exceptions.tests.test_exceptionformatter, line ??, in testFormattedExceptionText
- # Hello, World! exec \'syntax error\'
- # Hello, World! File "<string>", line 1
- # Hello, World! syntax error
- # Hello, World! ^
- # Hello, World! SyntaxError: unexpected EOF while parsing
- self.assertEquals(s.splitlines()[-3:],
- ['Hello, World! syntax error',
- 'Hello, World! ^',
- 'Hello, World! SyntaxError: unexpected EOF while parsing'])
- def testFormattedExceptionHTML(self):
- try:
- exec 'syntax error'
- except SyntaxError, se:
- s = tb(as_html=1, format="Hello, World! %(message)s")
- # <p>Hello, World! Traceback (most recent call last):
- # <ul>
- # <li>Hello, World! File "/Users/aaron/work/projects/zope.exceptions-traceback-log-formatting/src/zope/exceptions/tests/test_exceptionformatter.py", line 197, in testFormattedExceptionHTML<br />
- # Hello, World! exec 'syntax error'</li>
- # </ul>Hello, World! File "<string>", line 1<br />
- # Hello, World! syntax error<br />
- # Hello, World! ^<br />
- # Hello, World! SyntaxError: unexpected EOF while parsing<br />
- # </p>
- self.assertEquals(s.splitlines()[-4:],
- ['Hello, World! syntax error<br />',
- 'Hello, World! ^<br />',
- 'Hello, World! SyntaxError: unexpected EOF while parsing<br />',
- '</p>'])
-
def test_suite():
- return unittest.TestSuite([
- unittest.makeSuite(Test),
- ])
+ return makeSuite(Test)
if __name__=='__main__':
- unittest.main(defaultTest='test_suite')
+ main(defaultTest='test_suite')
More information about the Checkins
mailing list