Skip to content

Commit

Permalink
Fix issue with iterable_as_array and indent option, closes #128
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Oct 27, 2015
1 parent 82fdecb commit b979a80
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Version 3.8.1 released 2015-XX-XX
Version 3.8.1 released 2015-10-27

* Fix issue with iterable_as_array and indent option
https://github.com/simplejson/simplejson/issues/128
* Fix typo in keyword argument name introduced in 3.8.0
https://github.com/simplejson/simplejson/pull/123

Expand Down
12 changes: 8 additions & 4 deletions simplejson/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,14 @@ def _iterencode_list(lst, _current_indent_level):
chunks = _iterencode(value, _current_indent_level)
for chunk in chunks:
yield chunk
if newline_indent is not None:
_current_indent_level -= 1
yield '\n' + (_indent * _current_indent_level)
yield ']'
if first:
# iterable_as_array misses the fast path at the top
yield '[]'
else:
if newline_indent is not None:
_current_indent_level -= 1
yield '\n' + (_indent * _current_indent_level)
yield ']'
if markers is not None:
del markers[markerid]

Expand Down
1 change: 1 addition & 0 deletions simplejson/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def get_suite():
'simplejson.tests.test_fail',
'simplejson.tests.test_float',
'simplejson.tests.test_indent',
'simplejson.tests.test_iterable',
'simplejson.tests.test_pass1',
'simplejson.tests.test_pass2',
'simplejson.tests.test_pass3',
Expand Down
34 changes: 17 additions & 17 deletions simplejson/tests/test_iterable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from StringIO import StringIO
from simplejson.compat import StringIO

import simplejson as json

Expand All @@ -13,19 +13,19 @@ def sio_dump(obj, **kw):

class TestIterable(unittest.TestCase):
def test_iterable(self):
l = [1, 2, 3]
for dumps in (json.dumps, iter_dumps, sio_dump):
expect = dumps(l)
default_expect = dumps(sum(l))
# Default is False
self.assertRaises(TypeError, dumps, iter(l))
self.assertRaises(TypeError, dumps, iter(l), iterable_as_array=False)
self.assertEqual(expect, dumps(iter(l), iterable_as_array=True))
# Ensure that the "default" gets called
self.assertEqual(default_expect, dumps(iter(l), default=sum))
self.assertEqual(default_expect, dumps(iter(l), iterable_as_array=False, default=sum))
# Ensure that the "default" does not get called
self.assertEqual(
default_expect,
dumps(iter(l), iterable_as_array=True, default=sum))

for l in ([], [1], [1, 2], [1, 2, 3]):
for opts in [{}, {'indent': 2}]:
for dumps in (json.dumps, iter_dumps, sio_dump):
expect = dumps(l, **opts)
default_expect = dumps(sum(l), **opts)
# Default is False
self.assertRaises(TypeError, dumps, iter(l), **opts)
self.assertRaises(TypeError, dumps, iter(l), iterable_as_array=False, **opts)
self.assertEqual(expect, dumps(iter(l), iterable_as_array=True, **opts))
# Ensure that the "default" gets called
self.assertEqual(default_expect, dumps(iter(l), default=sum, **opts))
self.assertEqual(default_expect, dumps(iter(l), iterable_as_array=False, default=sum, **opts))
# Ensure that the "default" does not get called
self.assertEqual(
expect,
dumps(iter(l), iterable_as_array=True, default=sum, **opts))

0 comments on commit b979a80

Please sign in to comment.