Skip to content

Commit 7cb5d32

Browse files
committed
flake8 plus tests for cgitb.py
flake8 fixes for cgitb.py. Also set up tests for functions in unit. Need tests before making some other changes in cgitb.py.
1 parent 4a5edb7 commit 7cb5d32

File tree

2 files changed

+158
-12
lines changed

2 files changed

+158
-12
lines changed

roundup/cgi/cgitb.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
from __future__ import print_function
88
__docformat__ = 'restructuredtext'
99

10-
import sys, os, keyword, linecache, tokenize, inspect
11-
import pydoc, traceback
10+
import inspect
11+
import keyword
12+
import linecache
13+
import os
14+
import pydoc
15+
import sys
16+
import tokenize
17+
import traceback
1218

1319
from roundup.anypy.html import html_escape
14-
15-
from roundup.cgi import TranslationService
1620
from roundup.anypy.strings import s2b
21+
from roundup.cgi import TranslationService
1722

1823

1924
def get_translator(i18n=None):
@@ -45,8 +50,8 @@ def niceDict(indent, dict):
4550
l = []
4651
for k in sorted(dict):
4752
v = dict[k]
48-
l.append('<tr><td><strong>%s</strong></td><td>%s</td></tr>' % (k,
49-
html_escape(repr(v))))
53+
l.append('<tr><td><strong>%s</strong></td><td>%s</td></tr>' % (
54+
k, html_escape(repr(v))))
5055
return '\n'.join(l)
5156

5257

@@ -72,9 +77,10 @@ def pt_html(context=5, i18n=None):
7277
s.append(_('<li>"%(name)s" (%(info)s)</li>')
7378
% {'name': name, 'info': esc(repr(info))})
7479
s = '\n'.join(s)
75-
l.append(_('<li>Looking for "%(name)s", '
76-
'current path:<ol>%(path)s</ol></li>'
77-
) % {'name': ti.name, 'path': s})
80+
l.append(_(
81+
'<li>Looking for "%(name)s", '
82+
'current path:<ol>%(path)s</ol></li>'
83+
) % {'name': ti.name, 'path': s})
7884
else:
7985
l.append(_('<li>In %s</li>') % esc(str(ti)))
8086
if '__traceback_supplement__' in locals:
@@ -101,7 +107,7 @@ def pt_html(context=5, i18n=None):
101107
'line': context.position[0],
102108
'globals': niceDict(' ', context.global_vars),
103109
'locals': niceDict(' ', context.local_vars)
104-
})
110+
})
105111

106112
l.append('''
107113
</ol>
@@ -164,7 +170,7 @@ def tokeneater(type, token, start, end, line, names=names):
164170
if type == tokenize.NAME and token not in keyword.kwlist:
165171
if token not in names:
166172
names.append(token)
167-
if type == tokenize.NEWLINE: raise IndexError
173+
if type == tokenize.NEWLINE: raise IndexError # noqa: E701
168174

169175
def linereader(file=file, lnum=[lnum]):
170176
line = s2b(linecache.getline(file, lnum[0]))

test/test_misc.py

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# misc tests
22

3+
import re
4+
import sys
35
import unittest
6+
47
import roundup.anypy.cmp_
5-
import sys
8+
69
from roundup.anypy.strings import StringIO # define StringIO
10+
from roundup.cgi import cgitb
711
from roundup.cgi.accept_language import parse
812

13+
914
class AcceptLanguageTest(unittest.TestCase):
1015
def testParse(self):
1116
self.assertEqual(parse("da, en-gb;q=0.8, en;q=0.7"),
@@ -63,3 +68,138 @@ def test_Version_Check(self):
6368
sys.version_info = real_ver
6469

6570

71+
class CgiTbCheck(unittest.TestCase):
72+
73+
def test_NiceDict(self):
74+
d = cgitb.niceDict(" ", { "two": "three", "four": "five" })
75+
76+
expected = (
77+
"<tr><td><strong>four</strong></td><td>'five'</td></tr>\n"
78+
"<tr><td><strong>two</strong></td><td>'three'</td></tr>"
79+
)
80+
81+
self.assertEqual(expected, d)
82+
83+
def test_breaker(self):
84+
b = cgitb.breaker()
85+
86+
expected = ('<body bgcolor="white"><font color="white" size="-5">'
87+
' > </font> </table></table></table></table></table>')
88+
89+
self.assertEqual(expected, b)
90+
91+
def test_pt_html(self):
92+
""" templating error """
93+
try:
94+
f = 5
95+
d = a + 4
96+
except Exception:
97+
p = cgitb.pt_html(context=2)
98+
99+
expected2 = """<h1>Templating Error</h1>
100+
<p><b>&lt;type 'exceptions.NameError'&gt;</b>: global name 'a' is not defined</p>
101+
<p class="help">Debugging information follows</p>
102+
<ol>
103+
104+
</ol>
105+
<table style="font-size: 80%; color: gray">
106+
<tr><th class="header" align="left">Full traceback:</th></tr>
107+
<tr><td><pre>Traceback (most recent call last):
108+
File "XX/test/test_misc.py", line XX, in test_pt_html
109+
d = a + 4
110+
NameError: global name 'a' is not defined
111+
</pre></td></tr>
112+
</table>
113+
<p>&nbsp;</p>"""
114+
115+
expected3 = """<h1>Templating Error</h1>
116+
<p><b>&lt;class 'NameError'&gt;</b>: name 'a' is not defined</p>
117+
<p class="help">Debugging information follows</p>
118+
<ol>
119+
120+
</ol>
121+
<table style="font-size: 80%; color: gray">
122+
<tr><th class="header" align="left">Full traceback:</th></tr>
123+
<tr><td><pre>Traceback (most recent call last):
124+
File "XX/test/test_misc.py", line XX, in test_pt_html
125+
d = a + 4
126+
NameError: name 'a' is not defined
127+
</pre></td></tr>
128+
</table>
129+
<p>&nbsp;</p>"""
130+
131+
# allow file directory prefix and line number to change
132+
p = re.sub(r'(File ")/.*/(test/test_misc.py",)', r'\1XX/\2', p)
133+
p = re.sub(r'(", line )\d*,', r'\1XX,', p)
134+
135+
print(p)
136+
137+
if sys.version_info > (3, 0, 0):
138+
self.assertEqual(expected3, p)
139+
else:
140+
self.assertEqual(expected2, p)
141+
142+
def test_html(self):
143+
""" templating error """
144+
# enabiling this will cause the test to fail as the variable
145+
# is included in the live outpu but not in expected.
146+
# self.maxDiff = None
147+
148+
try:
149+
f = 5
150+
d = a + 4
151+
except Exception:
152+
h = cgitb.html(context=2)
153+
154+
expected2 = """
155+
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
156+
<tr bgcolor="#777777">
157+
<td valign=bottom>&nbsp;<br>
158+
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><font size=+1><strong>NameError</strong>: global name 'a' is not defined</font></font></td
159+
><td align=right valign=bottom
160+
><font color="#ffffff" face="helvetica, arial">Python 2.7.17<br>/usr/bin/python2</font></td></tr></table>
161+
<p>A problem occurred while running a Python script. Here is the sequence of function calls leading up to the error, with the most recent (innermost) call first. The exception attributes are:<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__class__&nbsp;= &lt;type 'exceptions.NameError'&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__delattr__&nbsp;= &lt;method-wrapper '__delattr__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__dict__&nbsp;= {} <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__doc__&nbsp;= 'Name not found globally.' <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__format__&nbsp;= &lt;built-in method __format__ of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__getattribute__&nbsp;= &lt;method-wrapper '__getattribute__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__getitem__&nbsp;= &lt;method-wrapper '__getitem__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__getslice__&nbsp;= &lt;method-wrapper '__getslice__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__hash__&nbsp;= &lt;method-wrapper '__hash__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__init__&nbsp;= &lt;method-wrapper '__init__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__new__&nbsp;= &lt;built-in method __new__ of type object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__reduce__&nbsp;= &lt;built-in method __reduce__ of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__reduce_ex__&nbsp;= &lt;built-in method __reduce_ex__ of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__repr__&nbsp;= &lt;method-wrapper '__repr__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__setattr__&nbsp;= &lt;method-wrapper '__setattr__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__setstate__&nbsp;= &lt;built-in method __setstate__ of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__sizeof__&nbsp;= &lt;built-in method __sizeof__ of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__str__&nbsp;= &lt;method-wrapper '__str__' of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__subclasshook__&nbsp;= &lt;built-in method __subclasshook__ of type object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__unicode__&nbsp;= &lt;built-in method __unicode__ of exceptions.NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>args&nbsp;= ("global name 'a' is not defined",) <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>message&nbsp;= "global name 'a' is not defined"<p>
162+
<table width="100%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0>
163+
<tr><td><a href="file:XX/test/test_misc.py">XX/test/test_misc.py</a> in <strong>test_html</strong>(self=&lt;test.test_misc.CgiTbCheck testMethod=test_html&gt;)</td></tr></table>
164+
<tt><small><font color="#909090">&nbsp;&nbsp;XX</font></small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f&nbsp;=&nbsp;5<br>
165+
</tt>
166+
167+
168+
<table width="100%" bgcolor="white" cellspacing=0 cellpadding=0 border=0>
169+
<tr><td><tt><small><font color="#909090">&nbsp;&nbsp;XX</font></small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;=&nbsp;a&nbsp;+&nbsp;4<br>
170+
</tt></td></tr></table>
171+
<tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt><small><font color="#909090"><strong>d</strong>&nbsp;= <em>undefined</em>, <em>global</em> <strong>a</strong>&nbsp;= <em>undefined</em></font></small><br><p>&nbsp;</p>"""
172+
173+
expected3 = """
174+
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
175+
<tr bgcolor="#777777">
176+
<td valign=bottom>&nbsp;<br>
177+
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><font size=+1><strong>NameError</strong>: name 'a' is not defined</font></font></td
178+
><td align=right valign=bottom
179+
><font color="#ffffff" face="helvetica, arial">Python 3.6.9<br>/usr/bin/python3</font></td></tr></table>
180+
<p>A problem occurred while running a Python script. Here is the sequence of function calls leading up to the error, with the most recent (innermost) call first. The exception attributes are:<br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__cause__&nbsp;= None <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__class__&nbsp;= &lt;class 'NameError'&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__context__&nbsp;= None <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__delattr__&nbsp;= &lt;method-wrapper '__delattr__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__dict__&nbsp;= {} <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__dir__&nbsp;= &lt;built-in method __dir__ of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__doc__&nbsp;= 'Name not found globally.' <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__eq__&nbsp;= &lt;method-wrapper '__eq__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__format__&nbsp;= &lt;built-in method __format__ of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__ge__&nbsp;= &lt;method-wrapper '__ge__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__getattribute__&nbsp;= &lt;method-wrapper '__getattribute__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__gt__&nbsp;= &lt;method-wrapper '__gt__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__hash__&nbsp;= &lt;method-wrapper '__hash__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__init__&nbsp;= &lt;method-wrapper '__init__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__init_subclass__&nbsp;= &lt;built-in method __init_subclass__ of type object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__le__&nbsp;= &lt;method-wrapper '__le__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__lt__&nbsp;= &lt;method-wrapper '__lt__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__ne__&nbsp;= &lt;method-wrapper '__ne__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__new__&nbsp;= &lt;built-in method __new__ of type object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__reduce__&nbsp;= &lt;built-in method __reduce__ of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__reduce_ex__&nbsp;= &lt;built-in method __reduce_ex__ of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__repr__&nbsp;= &lt;method-wrapper '__repr__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__setattr__&nbsp;= &lt;method-wrapper '__setattr__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__setstate__&nbsp;= &lt;built-in method __setstate__ of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__sizeof__&nbsp;= &lt;built-in method __sizeof__ of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__str__&nbsp;= &lt;method-wrapper '__str__' of NameError object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__subclasshook__&nbsp;= &lt;built-in method __subclasshook__ of type object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__suppress_context__&nbsp;= False <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>__traceback__&nbsp;= &lt;traceback object&gt; <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>args&nbsp;= ("name 'a' is not defined",) <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>with_traceback&nbsp;= &lt;built-in method with_traceback of NameError object&gt;<p>
181+
<table width="100%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0>
182+
<tr><td><a href="file:XX/test/test_misc.py">XX/test/test_misc.py</a> in <strong>test_html</strong>(self=&lt;test.test_misc.CgiTbCheck testMethod=test_html&gt;)</td></tr></table>
183+
<tt><small><font color="#909090">&nbsp;&nbsp;XX</font></small>&nbsp;<br>
184+
</tt>
185+
186+
187+
<table width="100%" bgcolor="white" cellspacing=0 cellpadding=0 border=0>
188+
<tr><td><tt><small><font color="#909090">&nbsp;&nbsp;XX</font></small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:<br>
189+
</tt></td></tr></table>
190+
<p>&nbsp;</p>"""
191+
192+
# strip file path prefix from href and text
193+
# /home/user/develop/roundup/test/test_misc.py in test_html
194+
h = re.sub(r'(file:)/.*/(test/test_misc.py")', r'\1XX/\2', h)
195+
h = re.sub(r'(/test_misc.py">)/.*/(test/test_misc.py</a>)',
196+
r'\1XX/\2', h)
197+
# replace code line numbers with XX
198+
h = re.sub(r'(&nbsp;)\d*(</font>)', r'\1XX\2', h)
199+
200+
print(h)
201+
202+
if sys.version_info > (3, 0, 0):
203+
self.assertEqual(expected3, h)
204+
else:
205+
self.assertEqual(expected2, h)

0 commit comments

Comments
 (0)