Message271249
JSONEncoder.iterencode doesn't work with empty iterators correctly.
Steps:
1. Define an iterator that is recognized by json as a list (inherit from list and define nonzero __len__).
2. Use json.dump with data containing an empty iterator defined as described in step#1 (but doesn't generate any items)
Expected result: it should be rendered as an empty list: '[]'
Actual result: it is rendered as ']' (only the closing bracket)
interestingly enough this behavior is not reproduced when using the dumps function.
I tried other alternatives to the standard json module: simplejson, ujson, hjson
All of them work as expected in this case (both brackets are rendered).
Here is an example of the code that demonstrates this error (compares the results of the dump and dumps functions):
import json as json
import io
class EmptyIterator(list):
def __iter__(self):
while False:
yield 1
def __len__(self):
return 1
def dump_to_str(data):
return json.dumps(data)
def dump_to_file(data):
stream = io.StringIO()
json.dump(data, stream)
return stream.getvalue()
data = {'it': EmptyIterator()}
print('to str: {0}'.format(dump_to_str(data)))
print('to file: {0}'.format(dump_to_file(data)))
This prints:
to str: {"it": []}
to file: {"it": ]} |
|
Date |
User |
Action |
Args |
2016-07-25 11:42:11 | altvod | set | recipients:
+ altvod |
2016-07-25 11:42:11 | altvod | set | messageid: <1469446931.7.0.792781039481.issue27613@psf.upfronthosting.co.za> |
2016-07-25 11:42:11 | altvod | link | issue27613 messages |
2016-07-25 11:42:11 | altvod | create | |
|