diff -r 5e45dfc421e4 Lib/cgitb.py --- a/Lib/cgitb.py Sat Nov 19 16:27:22 2011 +0000 +++ b/Lib/cgitb.py Mon Nov 21 09:28:26 2011 -0500 @@ -292,13 +292,17 @@ if self.logdir is not None: suffix = ['.txt', '.html'][self.format=="html"] (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir) + + if self.format == 'html': + self.file.write('

') + try: file = os.fdopen(fd, 'w') file.write(doc) file.close() - msg = '

%s contains the description of this error.' % path + msg = '%s contains the description of this error.' % path except: - msg = '

Tried to save traceback to %s, but failed.' % path + msg = 'Tried to save traceback to %s, but failed.' % path self.file.write(msg + '\n') try: self.file.flush() diff -r 5e45dfc421e4 Lib/test/test_cgitb.py --- a/Lib/test/test_cgitb.py Sat Nov 19 16:27:22 2011 +0000 +++ b/Lib/test/test_cgitb.py Mon Nov 21 09:28:26 2011 -0500 @@ -2,6 +2,7 @@ import unittest import sys import subprocess +import tempfile import cgitb class TestCgitb(unittest.TestCase): @@ -37,15 +38,31 @@ self.assertIn("Hello World", text) def test_hook(self): + tracedir = tempfile.gettempdir() + # Ensure HTML is printed when format is not set. The default case is + # to assume the caller wants HTML markup. proc = subprocess.Popen([sys.executable, '-c', ('import cgitb;' - 'cgitb.enable();' - 'raise ValueError("Hello World")')], + 'cgitb.enable(logdir="%s");' + 'raise ValueError("Hello World")' % tracedir)], stdout=subprocess.PIPE) out = proc.stdout.read().decode(sys.getfilesystemencoding()) self.addCleanup(proc.stdout.close) self.assertIn("ValueError", out) self.assertIn("Hello World", out) + self.assertIn('

', out) + + # Ensure HTML is not printed when requesting text formatting. + proc = subprocess.Popen([sys.executable, '-c', + ('import cgitb;' + 'cgitb.enable(format="text", logdir="%s");' + 'raise ValueError("Hello World")' % tracedir)], + stdout=subprocess.PIPE) + out = proc.stdout.read().decode(sys.getfilesystemencoding()) + self.addCleanup(proc.stdout.close) + self.assertIn("ValueError", out) + self.assertIn("Hello World", out) + self.assertNotIn('

', out) def test_main():