Mercurial > cpython-lru_cache-weakref
annotate Lib/json/tests/test_unicode.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | a9680746ae4a |
| children |
| rev | line source |
|---|---|
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
1 from collections import OrderedDict |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
2 from json.tests import PyTest, CTest |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
3 |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
4 |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
5 class TestUnicode(object): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 def test_encoding1(self): |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
7 encoder = self.json.JSONEncoder(encoding='utf-8') |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
9 s = u.encode('utf-8') |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
10 ju = encoder.encode(u) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 js = encoder.encode(s) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65901
diff
changeset
|
12 self.assertEqual(ju, js) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
13 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 def test_encoding2(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
15 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 s = u.encode('utf-8') |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
17 ju = self.dumps(u, encoding='utf-8') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
18 js = self.dumps(s, encoding='utf-8') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65901
diff
changeset
|
19 self.assertEqual(ju, js) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 def test_encoding3(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
23 j = self.dumps(u) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65901
diff
changeset
|
24 self.assertEqual(j, '"\\u03b1\\u03a9"') |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 def test_encoding4(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
27 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
28 j = self.dumps([u]) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65901
diff
changeset
|
29 self.assertEqual(j, '["\\u03b1\\u03a9"]') |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
30 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
31 def test_encoding5(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
32 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
33 j = self.dumps(u, ensure_ascii=False) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65901
diff
changeset
|
34 self.assertEqual(j, u'"{0}"'.format(u)) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
35 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
36 def test_encoding6(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
37 u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
38 j = self.dumps([u], ensure_ascii=False) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65901
diff
changeset
|
39 self.assertEqual(j, u'["{0}"]'.format(u)) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
40 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
41 def test_big_unicode_encode(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
42 u = u'\U0001d120' |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
43 self.assertEqual(self.dumps(u), '"\\ud834\\udd20"') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
44 self.assertEqual(self.dumps(u, ensure_ascii=False), u'"\U0001d120"') |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
45 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
46 def test_big_unicode_decode(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
47 u = u'z\U0001d120x' |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
48 self.assertEqual(self.loads('"' + u + '"'), u) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
49 self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
50 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
51 def test_unicode_decode(self): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
52 for i in range(0, 0xd7ff): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
53 u = unichr(i) |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
54 s = '"\\u{0:04x}"'.format(i) |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
55 self.assertEqual(self.loads(s), u) |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
56 |
|
52225
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
57 def test_object_pairs_hook_with_unicode(self): |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
58 s = u'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
59 p = [(u"xkd", 1), (u"kcw", 2), (u"art", 3), (u"hxm", 4), |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
60 (u"qrt", 5), (u"pad", 6), (u"hoy", 7)] |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
61 self.assertEqual(self.loads(s), eval(s)) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
62 self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
63 od = self.loads(s, object_pairs_hook = OrderedDict) |
|
52225
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
64 self.assertEqual(od, OrderedDict(p)) |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
65 self.assertEqual(type(od), OrderedDict) |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
66 # the object_pairs_hook takes priority over the object_hook |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
67 self.assertEqual(self.loads(s, |
|
52225
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
68 object_pairs_hook = OrderedDict, |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
69 object_hook = lambda x: None), |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
70 OrderedDict(p)) |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
71 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
72 def test_default_encoding(self): |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
73 self.assertEqual(self.loads(u'{"a": "\xe9"}'.encode('utf-8')), |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
74 {'a': u'\xe9'}) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
75 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
76 def test_unicode_preservation(self): |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
77 self.assertEqual(type(self.loads(u'""')), unicode) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
78 self.assertEqual(type(self.loads(u'"a"')), unicode) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
79 self.assertEqual(type(self.loads(u'["a"]')[0]), unicode) |
|
65901
23b49678401f
Issue 10038. Restore the Python 2.6 behavior that json.loads() always returns
Barry Warsaw <barry@python.org>
parents:
52225
diff
changeset
|
80 # Issue 10038. |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
81 self.assertEqual(type(self.loads('"foo"')), unicode) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
82 |
|
74369
a9680746ae4a
Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
70084
diff
changeset
|
83 def test_bad_encoding(self): |
|
a9680746ae4a
Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
70084
diff
changeset
|
84 self.assertRaises(UnicodeEncodeError, self.loads, '"a"', u"rat\xe9") |
|
a9680746ae4a
Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
70084
diff
changeset
|
85 self.assertRaises(TypeError, self.loads, '"a"', 1) |
|
a9680746ae4a
Issue #13774: json: Fix a SystemError when a bogus encoding is passed to
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
70084
diff
changeset
|
86 |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
87 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
88 class TestPyUnicode(TestUnicode, PyTest): pass |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66348
diff
changeset
|
89 class TestCUnicode(TestUnicode, CTest): pass |
