Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcd34a6

Browse files
committed
merge #12890: don't emit <p> tags in text mode when logdir specified.
Patch by Jeff McNeil.
2 parents 4503713 + c4b8e05 commit dcd34a6

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

Lib/cgitb.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,19 @@ def handle(self, info=None):
292292
if self.logdir is not None:
293293
suffix = ['.txt', '.html'][self.format=="html"]
294294
(fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir)
295+
295296
try:
296297
file = os.fdopen(fd, 'w')
297298
file.write(doc)
298299
file.close()
299-
msg = '<p> %s contains the description of this error.' % path
300+
msg = '%s contains the description of this error.' % path
300301
except:
301-
msg = '<p> Tried to save traceback to %s, but failed.' % path
302-
self.file.write(msg + '\n')
302+
msg = 'Tried to save traceback to %s, but failed.' % path
303+
304+
if self.format == 'html':
305+
self.file.write('<p>%s</p>\n' % msg)
306+
else:
307+
self.file.write(msg + '\n')
303308
try:
304309
self.file.flush()
305310
except: pass

Lib/test/test_cgitb.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from test.support import run_unittest
2+
from test.script_helper import assert_python_failure, temp_dir
23
import unittest
34
import sys
45
import subprocess
6+
import tempfile
57
import cgitb
68

79
class TestCgitb(unittest.TestCase):
@@ -36,16 +38,31 @@ def test_text(self):
3638
self.assertIn("ValueError", text)
3739
self.assertIn("Hello World", text)
3840

39-
def test_hook(self):
40-
proc = subprocess.Popen([sys.executable, '-c',
41-
('import cgitb;'
42-
'cgitb.enable();'
43-
'raise ValueError("Hello World")')],
44-
stdout=subprocess.PIPE)
45-
out = proc.stdout.read().decode(sys.getfilesystemencoding())
46-
self.addCleanup(proc.stdout.close)
41+
def test_syshook_no_logdir_default_format(self):
42+
with temp_dir() as tracedir:
43+
rc, out, err = assert_python_failure(
44+
'-c',
45+
('import cgitb; cgitb.enable(logdir="%s"); '
46+
'raise ValueError("Hello World")') % tracedir)
47+
out = out.decode(sys.getfilesystemencoding())
4748
self.assertIn("ValueError", out)
4849
self.assertIn("Hello World", out)
50+
# By default we emit HTML markup.
51+
self.assertIn('<p>', out)
52+
self.assertIn('</p>', out)
53+
54+
def test_syshook_no_logdir_text_format(self):
55+
# Issue 12890: we were emitting the <p> tag in text mode.
56+
with temp_dir() as tracedir:
57+
rc, out, err = assert_python_failure(
58+
'-c',
59+
('import cgitb; cgitb.enable(format="text", logdir="%s"); '
60+
'raise ValueError("Hello World")') % tracedir)
61+
out = out.decode(sys.getfilesystemencoding())
62+
self.assertIn("ValueError", out)
63+
self.assertIn("Hello World", out)
64+
self.assertNotIn('<p>', out)
65+
self.assertNotIn('</p>', out)
4966

5067

5168
def test_main():

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ Mark Mc Mahon
769769
Gordon McMillan
770770
Andrew McNamara
771771
Caolan McNamara
772+
Jeff McNeil
772773
Craig McPheeters
773774
Lambert Meertens
774775
Bill van Melle

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Core and Builtins
6262
Library
6363
-------
6464

65+
- Issue #12890: cgitb no longer prints spurious <p> tags in text
66+
mode when the logdir option is specified.
67+
6568
- Issue #16307: Fix multiprocessing.Pool.map_async not calling its callbacks.
6669
Patch by Janne Karila.
6770

0 commit comments

Comments
 (0)