Skip to content

Commit 854d3e4

Browse files
committed
Update cgitb from Python 3.11
1 parent 3150b4d commit 854d3e4

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

Lib/cgitb.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
import time
3232
import tokenize
3333
import traceback
34+
import warnings
35+
from html import escape as html_escape
36+
37+
warnings._deprecated(__name__, remove=(3, 13))
38+
3439

3540
def reset():
3641
"""Return a string that resets the CGI and browser to a known state."""
@@ -105,10 +110,16 @@ def html(einfo, context=5):
105110
etype = etype.__name__
106111
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
107112
date = time.ctime(time.time())
108-
head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
109-
'<big><big>%s</big></big>' %
110-
strong(pydoc.html.escape(str(etype))),
111-
'#ffffff', '#6622aa', pyver + '<br>' + date) + '''
113+
head = f'''
114+
<body bgcolor="#f0f0f8">
115+
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
116+
<tr bgcolor="#6622aa">
117+
<td valign=bottom>&nbsp;<br>
118+
<font color="#ffffff" face="helvetica, arial">&nbsp;<br>
119+
<big><big><strong>{html_escape(str(etype))}</strong></big></big></font></td>
120+
<td align=right valign=bottom>
121+
<font color="#ffffff" face="helvetica, arial">{pyver}<br>{date}</font></td>
122+
</tr></table>
112123
<p>A problem occurred in a Python script. Here is the sequence of
113124
function calls leading up to the error, in the order they occurred.</p>'''
114125

@@ -181,8 +192,8 @@ def reader(lnum=[lnum]):
181192
182193
183194
<!-- The above is a description of an error in a Python program, formatted
184-
for a Web browser because the 'cgitb' module was enabled. In case you
185-
are not reading this in a Web browser, here is the original traceback:
195+
for a web browser because the 'cgitb' module was enabled. In case you
196+
are not reading this in a web browser, here is the original traceback:
186197
187198
%s
188199
-->

Lib/test/test_cgitb.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from test.support.os_helper import temp_dir
22
from test.support.script_helper import assert_python_failure
3+
from test.support.warnings_helper import import_deprecated
34
import unittest
45
import sys
5-
import cgitb
6+
cgitb = import_deprecated("cgitb")
67

78
class TestCgitb(unittest.TestCase):
89

@@ -31,7 +32,7 @@ def test_html(self):
3132
def test_text(self):
3233
try:
3334
raise ValueError("Hello World")
34-
except ValueError as err:
35+
except ValueError:
3536
text = cgitb.text(sys.exc_info())
3637
self.assertIn("ValueError", text)
3738
self.assertIn("Hello World", text)
@@ -41,8 +42,9 @@ def test_syshook_no_logdir_default_format(self):
4142
rc, out, err = assert_python_failure(
4243
'-c',
4344
('import cgitb; cgitb.enable(logdir=%s); '
44-
'raise ValueError("Hello World")') % repr(tracedir))
45-
out = out.decode(sys.getfilesystemencoding())
45+
'raise ValueError("Hello World")') % repr(tracedir),
46+
PYTHONIOENCODING='utf-8')
47+
out = out.decode()
4648
self.assertIn("ValueError", out)
4749
self.assertIn("Hello World", out)
4850
self.assertIn("<strong>&lt;module&gt;</strong>", out)
@@ -56,8 +58,9 @@ def test_syshook_no_logdir_text_format(self):
5658
rc, out, err = assert_python_failure(
5759
'-c',
5860
('import cgitb; cgitb.enable(format="text", logdir=%s); '
59-
'raise ValueError("Hello World")') % repr(tracedir))
60-
out = out.decode(sys.getfilesystemencoding())
61+
'raise ValueError("Hello World")') % repr(tracedir),
62+
PYTHONIOENCODING='utf-8')
63+
out = out.decode()
6164
self.assertIn("ValueError", out)
6265
self.assertIn("Hello World", out)
6366
self.assertNotIn('<p>', out)

0 commit comments

Comments
 (0)