annotate Lib/test/test_html.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 7b9235852b3b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
65362
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 """
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
2 Tests for the html module functions.
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 """
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
4
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 import html
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
6 import unittest
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 from test.support import run_unittest
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
8
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
9
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 class HtmlTests(unittest.TestCase):
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 def test_escape(self):
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
12 self.assertEqual(
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 html.escape('\'<script>"&foo;"</script>\''),
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 '&#x27;&lt;script&gt;&quot;&amp;foo;&quot;&lt;/script&gt;&#x27;')
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 self.assertEqual(
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 html.escape('\'<script>"&foo;"</script>\'', False),
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 '\'&lt;script&gt;"&amp;foo;"&lt;/script&gt;\'')
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
18
87275
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
19 def test_unescape(self):
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
20 numeric_formats = ['&#%d', '&#%d;', '&#x%x', '&#x%x;']
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
21 errmsg = 'unescape(%r) should have returned %r'
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
22 def check(text, expected):
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
23 self.assertEqual(html.unescape(text), expected,
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
24 msg=errmsg % (text, expected))
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
25 def check_num(num, expected):
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
26 for format in numeric_formats:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
27 text = format % num
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
28 self.assertEqual(html.unescape(text), expected,
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
29 msg=errmsg % (text, expected))
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
30 # check text with no character references
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
31 check('no character references', 'no character references')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
32 # check & followed by invalid chars
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
33 check('&\n&\t& &&', '&\n&\t& &&')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
34 # check & followed by numbers and letters
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
35 check('&0 &9 &a &0; &9; &a;', '&0 &9 &a &0; &9; &a;')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
36 # check incomplete entities at the end of the string
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
37 for x in ['&', '&#', '&#x', '&#X', '&#y', '&#xy', '&#Xy']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
38 check(x, x)
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
39 check(x+';', x+';')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
40 # check several combinations of numeric character references,
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
41 # possibly followed by different characters
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
42 formats = ['&#%d', '&#%07d', '&#%d;', '&#%07d;',
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
43 '&#x%x', '&#x%06x', '&#x%x;', '&#x%06x;',
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
44 '&#x%X', '&#x%06X', '&#X%x;', '&#X%06x;']
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
45 for num, char in zip([65, 97, 34, 38, 0x2603, 0x101234],
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
46 ['A', 'a', '"', '&', '\u2603', '\U00101234']):
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
47 for s in formats:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
48 check(s % num, char)
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
49 for end in [' ', 'X']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
50 check((s+end) % num, char+end)
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
51 # check invalid codepoints
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
52 for cp in [0xD800, 0xDB00, 0xDC00, 0xDFFF, 0x110000]:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
53 check_num(cp, '\uFFFD')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
54 # check more invalid codepoints
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
55 for cp in [0x1, 0xb, 0xe, 0x7f, 0xfffe, 0xffff, 0x10fffe, 0x10ffff]:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
56 check_num(cp, '')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
57 # check invalid numbers
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
58 for num, ch in zip([0x0d, 0x80, 0x95, 0x9d], '\r\u20ac\u2022\x9d'):
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
59 check_num(num, ch)
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
60 # check small numbers
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
61 check_num(0, '\uFFFD')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
62 check_num(9, '\t')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
63 # check a big number
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
64 check_num(1000000000000000000, '\uFFFD')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
65 # check that multiple trailing semicolons are handled correctly
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
66 for e in ['&quot;;', '&#34;;', '&#x22;;', '&#X22;;']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
67 check(e, '";')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
68 # check that semicolons in the middle don't create problems
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
69 for e in ['&quot;quot;', '&#34;quot;', '&#x22;quot;', '&#X22;quot;']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
70 check(e, '"quot;')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
71 # check triple adjacent charrefs
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
72 for e in ['&quot', '&#34', '&#x22', '&#X22']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
73 check(e*3, '"""')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
74 check((e+';')*3, '"""')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
75 # check that the case is respected
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
76 for e in ['&amp', '&amp;', '&AMP', '&AMP;']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
77 check(e, '&')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
78 for e in ['&Amp', '&Amp;']:
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
79 check(e, e)
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
80 # check that non-existent named entities are returned unchanged
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
81 check('&svadilfari;', '&svadilfari;')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
82 # the following examples are in the html5 specs
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
83 check('&notit', '¬it')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
84 check('&notit;', '¬it;')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
85 check('&notin', '¬in')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
86 check('&notin;', '∉')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
87 # a similar example with a long name
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
88 check('&notReallyAnExistingNamedCharacterReference;',
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
89 '¬ReallyAnExistingNamedCharacterReference;')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
90 # longest valid name
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
91 check('&CounterClockwiseContourIntegral;', '∳')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
92 # check a charref that maps to two unicode chars
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
93 check('&acE;', '\u223E\u0333')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
94 check('&acE', '&acE')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
95 # see #12888
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
96 check('&#123; ' * 1050, '{ ' * 1050)
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
97 # see #15156
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
98 check('&Eacuteric&Eacute;ric&alphacentauri&alpha;centauri',
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
99 'ÉricÉric&alphacentauriαcentauri')
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
100 check('&co;', '&co;')
65362
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
101
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
102
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
diff changeset
103 if __name__ == '__main__':
87275
7b9235852b3b #2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65362
diff changeset
104 unittest.main()