Issue 132: Public Cython dump() function should take file object, not bytearray
MODIFY
fastavro/__init__.py
fastavro/__init__.py
@@ -69,7 +69,7 @@ schemaless_reader = _reader.schemaless_reader
load = _reader.read_data
writer = _writer.writer
schemaless_writer = _writer.schemaless_writer
-dump = _writer.write_data
+dump = _writer.dump
acquaint_schema = _acquaint_schema
_schema.acquaint_schema = _acquaint_schema
is_avro = _reader.is_avro

MODIFY
fastavro/_writer.pyx
fastavro/_writer.pyx
@@ -782,3 +782,11 @@ def schemaless_writer(fo, schema, record):
acquaint_schema(schema)
write_data(tmp, record, schema)
fo.write(tmp)
+
+
+cpdef dump(fo, datum, schema):
+ cdef bytearray output = bytearray()
+
+ result = write_data(output, datum, schema)
+ fo.write(output)
+ return result

MODIFY
fastavro/_writer_py.py
fastavro/_writer_py.py
@@ -675,3 +675,6 @@ def schemaless_writer(fo, schema, record):
"""
acquaint_schema(schema)
write_data(fo, record, schema)
+
+
+dump = write_data

MODIFY
fastavro/writer.py
fastavro/writer.py
@@ -6,6 +6,7 @@ except ImportError as e:
from ._writer_common import SCHEMA_DEFS
acquaint_schema = _writer.acquaint_schema
+dump = _writer.dump
writer = _writer.writer
Writer = _writer.Writer
schemaless_writer = _writer.schemaless_writer

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -990,3 +990,31 @@ def test_writer_class_split_files(tmpdir):
new_interim_record_counts.append(len(new_records))
assert new_records == records
assert interim_record_counts == new_interim_record_counts
+
+
+def test_dump_load(tmpdir):
+ """
+ Write an Avro record to a file using the dump() function and loads it back
+ using the load() function.
+ """
+ schema = {
+ "type": "record",
+ "name": "Test",
+ "namespace": "test",
+ "fields": [
+ {
+ "name": "field",
+ "type": {"type": "string"}
+ }
+ ]
+ }
+ record = {"field": "foobar"}
+
+ temp_path = tmpdir.join('test_dump.avro')
+ with temp_path.open('wb') as fo:
+ fastavro.dump(fo, record, schema)
+
+ with temp_path.open('rb') as fo:
+ new_record = fastavro.load(fo, schema)
+
+ assert record == new_record

Issue 132: Public Cython dump() function should take file object, not bytearray