MCPcopy Create free account
hub / github.com/simplejson/simplejson / TestIterable

Class TestIterable

simplejson/tests/test_iterable.py:22–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class TestIterable(unittest.TestCase):
23 def test_iterable(self):
24 for l in ([], [1], [1, 2], [1, 2, 3]):
25 for opts in [{}, {'indent': 2}]:
26 for dumps in (json.dumps, iter_dumps, sio_dump):
27 expect = dumps(l, **opts)
28 default_expect = dumps(sum(l), **opts)
29 # Default is False
30 self.assertRaises(TypeError, dumps, iter(l), **opts)
31 self.assertRaises(TypeError, dumps, iter(l), iterable_as_array=False, **opts)
32 self.assertEqual(expect, dumps(iter(l), iterable_as_array=True, **opts))
33 # Ensure that the "default" gets called
34 self.assertEqual(default_expect, dumps(iter(l), default=sum, **opts))
35 self.assertEqual(default_expect, dumps(iter(l), iterable_as_array=False, default=sum, **opts))
36 # Ensure that the "default" does not get called
37 self.assertEqual(
38 expect,
39 dumps(iter(l), iterable_as_array=True, default=sum, **opts))
40
41 def test_iterable_as_array_propagates_non_typeerror(self):
42 # Regression test: MemoryError from __iter__ must not be
43 # swallowed by PyErr_Clear and replaced with TypeError.
44 # Test against the C encoder directly since json.dumps may
45 # use the Python fallback encoder.
46 try:
47 import simplejson._speedups as sp
48 import decimal
49 except ImportError:
50 return # C extension not available
51 def noop_default(obj):
52 raise TypeError('not serializable')
53 c_enc = sp.make_encoder(
54 {}, noop_default, sp.encode_basestring_ascii, None,
55 ', ', ': ', False, False, True, {}, False, False,
56 False, None, None, 'utf-8', False, False,
57 decimal.Decimal, True, # iterable_as_array=True
58 )
59 for exc_type in (MemoryError, RuntimeError):
60 with self.assertRaises(exc_type):
61 c_enc(BadIter(exc_type), 0)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…