[issue33850] Json.dump() bug when using generator

2018-06-13 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

This is a duplicate of issue27613.

--
nosy: +serhiy.storchaka
resolution:  -> duplicate
status: open -> closed
superseder:  -> Empty iterator with fake __len__ is rendered as a single 
bracket ] when using json's iterencode

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33850] Json.dump() bug when using generator

2018-06-13 Thread Walter Dörwald

Walter Dörwald  added the comment:

The problem here is that StreamArray lies about the length of the iterator. 
This confuses json.encoder._make_iterencode._iterencode_list(), (which is 
called by json.dump()), because it first does a check for "if not lst" and then 
assumes in the loop that it will be entered at least once.

(Note that json.dumps() doesn't have that problem, because it calls 
JSONEncoder.encode() with _one_shot=True which leads to a totally different 
code path).

We could declare that bug as "don't do that then", but the problem is easily 
solvable, because we can check whether the loop was entered. The attached patch 
should do the trick.

An even better approach would IMHO be, that the encoder supports a special flag 
that enables JSON serialization of generators directly, so it's no longer 
required to masquerade generators as list

--
keywords: +patch
nosy: +doerwalter
resolution: not a bug -> 
status: closed -> open
Added file: https://bugs.python.org/file47640/json-dump-generators-bug.diff

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33850] Json.dump() bug when using generator

2018-06-13 Thread Eric V. Smith


Eric V. Smith  added the comment:

You should ask your question on this mailing list: 
https://mail.python.org/mailman/listinfo/python-list

The bug tracker is not a place for asking how to use Python. If you actually 
find a bug in Python, you can re-open this issue. I do not believe that what 
you show here is a Python bug.

Good luck!

--
nosy: +eric.smith
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33850] Json.dump() bug when using generator

2018-06-13 Thread Clément Boyer

New submission from Clément Boyer :

I use a class to write easily json when having generator.
```python
class StreamArray(list):
def __init__(self, generator):
super().__init__()
self.generator = generator
def __iter__(self):
return self.generator
def __len__(self):
return 1
```
Below a test comparing json.dump and json.dumps.
```
>>> import json
>>> class StreamArray(list):
... def __init__(self, generator):
... super().__init__()
... self.generator = generator
... def __iter__(self):
... return self.generator
... def __len__(self):
... return 1
... 
>>> g = (i for i in range(0))
>>> json.dumps({"a": StreamArray(g)})
'{"a": []}'
>>> f = open("/tmp/test.json", "w+")
>>> g = (i for i in range(0))
>>> json.dump({"a": StreamArray(g)}, f)
>>> f.close()
>>> print(open("/tmp/test.json").read())
{"a": ]}
```
I don't know if it's me or if there is actually a problem, could you help me ?

--
components: Library (Lib)
messages: 319436
nosy: biloup
priority: normal
severity: normal
status: open
title: Json.dump() bug when using generator
type: behavior
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com