Server IP : 85.214.239.14 / Your IP : 3.15.10.117 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/cwd/proc/3/root/lib/python3/dist-packages/simplejson/tests/ |
Upload File : |
from __future__ import absolute_import import decimal from unittest import TestCase import simplejson as json from simplejson.compat import StringIO, b, binary_type from simplejson import OrderedDict class MisbehavingBytesSubtype(binary_type): def decode(self, encoding=None): return "bad decode" def __str__(self): return "bad __str__" def __bytes__(self): return b("bad __bytes__") class TestDecode(TestCase): if not hasattr(TestCase, 'assertIs'): def assertIs(self, a, b): self.assertTrue(a is b, '%r is %r' % (a, b)) def test_decimal(self): rval = json.loads('1.1', parse_float=decimal.Decimal) self.assertTrue(isinstance(rval, decimal.Decimal)) self.assertEqual(rval, decimal.Decimal('1.1')) def test_float(self): rval = json.loads('1', parse_int=float) self.assertTrue(isinstance(rval, float)) self.assertEqual(rval, 1.0) def test_decoder_optimizations(self): # Several optimizations were made that skip over calls to # the whitespace regex, so this test is designed to try and # exercise the uncommon cases. The array cases are already covered. rval = json.loads('{ "key" : "value" , "k":"v" }') self.assertEqual(rval, {"key":"value", "k":"v"}) def test_empty_objects(self): s = '{}' self.assertEqual(json.loads(s), eval(s)) s = '[]' self.assertEqual(json.loads(s), eval(s)) s = '""' self.assertEqual(json.loads(s), eval(s)) def test_object_pairs_hook(self): s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), ("qrt", 5), ("pad", 6), ("hoy", 7)] self.assertEqual(json.loads(s), eval(s)) self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p) self.assertEqual(json.load(StringIO(s), object_pairs_hook=lambda x: x), p) od = json.loads(s, object_pairs_hook=OrderedDict) self.assertEqual(od, OrderedDict(p)) self.assertEqual(type(od), OrderedDict) # the object_pairs_hook takes priority over the object_hook self.assertEqual(json.loads(s, object_pairs_hook=OrderedDict, object_hook=lambda x: None), OrderedDict(p)) def check_keys_reuse(self, source, loads): rval = loads(source) (a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) self.assertIs(a, c) self.assertIs(b, d) def test_keys_reuse_str(self): s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'.encode('utf8') self.check_keys_reuse(s, json.loads) def test_keys_reuse_unicode(self): s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' self.check_keys_reuse(s, json.loads) def test_empty_strings(self): self.assertEqual(json.loads('""'), "") self.assertEqual(json.loads(u'""'), u"") self.assertEqual(json.loads('[""]'), [""]) self.assertEqual(json.loads(u'[""]'), [u""]) def test_raw_decode(self): cls = json.decoder.JSONDecoder self.assertEqual( ({'a': {}}, 9), cls().raw_decode("{\"a\": {}}")) # http://code.google.com/p/simplejson/issues/detail?id=85 self.assertEqual( ({'a': {}}, 9), cls(object_pairs_hook=dict).raw_decode("{\"a\": {}}")) # https://github.com/simplejson/simplejson/pull/38 self.assertEqual( ({'a': {}}, 11), cls().raw_decode(" \n{\"a\": {}}")) def test_bytes_decode(self): cls = json.decoder.JSONDecoder data = b('"\xe2\x82\xac"') self.assertEqual(cls().decode(data), u'\u20ac') self.assertEqual(cls(encoding='latin1').decode(data), u'\xe2\x82\xac') self.assertEqual(cls(encoding=None).decode(data), u'\u20ac') data = MisbehavingBytesSubtype(b('"\xe2\x82\xac"')) self.assertEqual(cls().decode(data), u'\u20ac') self.assertEqual(cls(encoding='latin1').decode(data), u'\xe2\x82\xac') self.assertEqual(cls(encoding=None).decode(data), u'\u20ac') def test_bounds_checking(self): # https://github.com/simplejson/simplejson/issues/98 j = json.decoder.JSONDecoder() for i in [4, 5, 6, -1, -2, -3, -4, -5, -6]: self.assertRaises(ValueError, j.scan_once, '1234', i) self.assertRaises(ValueError, j.raw_decode, '1234', i) x, y = sorted(['128931233', '472389423'], key=id) diff = id(x) - id(y) self.assertRaises(ValueError, j.scan_once, y, diff) self.assertRaises(ValueError, j.raw_decode, y, i)