annotate Lib/json/tests/test_decode.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b16a5f0d0c87
children 4a5ad099b176
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 import decimal
52427
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
2 from StringIO import StringIO
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
3 from collections import OrderedDict
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
4 from json.tests import PyTest, CTest
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
7 class TestDecode(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
8 def test_decimal(self):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
9 rval = self.loads('1.1', parse_float=decimal.Decimal)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52427
diff changeset
10 self.assertTrue(isinstance(rval, decimal.Decimal))
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
11 self.assertEqual(rval, decimal.Decimal('1.1'))
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
13 def test_float(self):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
14 rval = self.loads('1', parse_int=float)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52427
diff changeset
15 self.assertTrue(isinstance(rval, float))
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
16 self.assertEqual(rval, 1.0)
52199
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
17
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
18 def test_decoder_optimizations(self):
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
19 # Several optimizations were made that skip over calls to
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
20 # the whitespace regex, so this test is designed to try and
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
21 # exercise the uncommon cases. The array cases are already covered.
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
22 rval = self.loads('{ "key" : "value" , "k":"v" }')
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
23 self.assertEqual(rval, {"key":"value", "k":"v"})
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
24
69313
d58c63ff5bb2 #9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not available.
Ezio Melotti
parents: 66348
diff changeset
25 def test_empty_objects(self):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
26 self.assertEqual(self.loads('{}'), {})
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
27 self.assertEqual(self.loads('[]'), [])
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
28 self.assertEqual(self.loads('""'), u"")
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
29 self.assertIsInstance(self.loads('""'), unicode)
69313
d58c63ff5bb2 #9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not available.
Ezio Melotti
parents: 66348
diff changeset
30
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
31 def test_object_pairs_hook(self):
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
32 s = '{"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
33 p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
34 ("qrt", 5), ("pad", 6), ("hoy", 7)]
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
35 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: 69822
diff changeset
36 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: 69822
diff changeset
37 self.assertEqual(self.json.load(StringIO(s),
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
38 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: 69822
diff changeset
39 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
40 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
41 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
42 # 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: 69822
diff changeset
43 self.assertEqual(self.loads(s,
52427
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
44 object_pairs_hook=OrderedDict,
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
45 object_hook=lambda x: None),
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
46 OrderedDict(p))
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
47
78645
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
48 def test_extra_data(self):
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
49 s = '[1, 2, 3]5'
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
50 msg = 'Extra data'
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
51 self.assertRaisesRegexp(ValueError, msg, self.loads, s)
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
52
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
53 def test_invalid_escape(self):
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
54 s = '["abc\\y"]'
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
55 msg = 'escape'
b16a5f0d0c87 Issue #15615: Add some tests for the json module's handling of invalid input data.
Antoine Pitrou <solipsis@pitrou.net>
parents: 70084
diff changeset
56 self.assertRaisesRegexp(ValueError, msg, self.loads, s)
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
57
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
58 class TestPyDecode(TestDecode, PyTest): pass
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69822
diff changeset
59 class TestCDecode(TestDecode, CTest): pass