• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from json.tests import CTest
2
3
4class BadBool:
5    def __nonzero__(self):
6        1/0.0
7
8
9class TestSpeedups(CTest):
10    def test_scanstring(self):
11        self.assertEqual(self.json.decoder.scanstring.__module__, "_json")
12        self.assertIs(self.json.decoder.scanstring, self.json.decoder.c_scanstring)
13
14    def test_encode_basestring_ascii(self):
15        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
16                         "_json")
17        self.assertIs(self.json.encoder.encode_basestring_ascii,
18                      self.json.encoder.c_encode_basestring_ascii)
19
20class TestDecode(CTest):
21    def test_make_scanner(self):
22        self.assertRaises(AttributeError, self.json.scanner.c_make_scanner, 1)
23
24    def test_bad_bool_args(self):
25        def test(value):
26            self.json.decoder.JSONDecoder(strict=BadBool()).decode(value)
27        self.assertRaises(ZeroDivisionError, test, '""')
28        self.assertRaises(ZeroDivisionError, test, '{}')
29        self.assertRaises(ZeroDivisionError, test, u'""')
30        self.assertRaises(ZeroDivisionError, test, u'{}')
31
32
33class TestEncode(CTest):
34    def test_make_encoder(self):
35        self.assertRaises(TypeError, self.json.encoder.c_make_encoder,
36            None,
37            "\xCD\x7D\x3D\x4E\x12\x4C\xF9\x79\xD7\x52\xBA\x82\xF2\x27\x4A\x7D\xA0\xCA\x75",
38            None)
39
40    def test_bad_bool_args(self):
41        def test(name):
42            self.json.encoder.JSONEncoder(**{name: BadBool()}).encode({'a': 1})
43        self.assertRaises(ZeroDivisionError, test, 'skipkeys')
44        self.assertRaises(ZeroDivisionError, test, 'ensure_ascii')
45        self.assertRaises(ZeroDivisionError, test, 'check_circular')
46        self.assertRaises(ZeroDivisionError, test, 'allow_nan')
47        self.assertRaises(ZeroDivisionError, test, 'sort_keys')
48
49    def test_bad_encoding(self):
50        with self.assertRaises(UnicodeEncodeError):
51            self.json.encoder.JSONEncoder(encoding=u'\udcff').encode({'key': 123})
52