dataset.freeze should not close provided fileobj

Test and solution for issue pudo/dataset#147
This commit is contained in:
Paul Morelle 2016-01-18 05:11:24 +01:00
parent dc3c14321a
commit 18bd08c009
3 changed files with 13 additions and 2 deletions

View File

@ -47,4 +47,5 @@ class CSVSerializer(Serializer):
def close(self):
for writer, fh in self.handles.values():
if fh != self.fileobj:
fh.close()

View File

@ -55,4 +55,5 @@ class JSONSerializer(Serializer):
self.export.get('callback'),
data)
fh.write(data)
if self.fileobj is None:
fh.close()

View File

@ -62,6 +62,15 @@ class FreezeTestCase(unittest.TestCase):
finally:
fh.close()
def test_memory_streams(self):
import io
for fmt in ('csv', 'json', 'tabson'):
with io.StringIO() as fd:
freeze(self.tbl.all(), format=fmt, fileobj=fd)
self.assertFalse(fd.closed, 'fileobj was closed for format %s' % fmt)
fd.getvalue() # should not throw
class SerializerTestCase(unittest.TestCase):