Skip to content

Commit d8363ca

Browse files
committed
FIX: Fix DBNStore.from_bytes with seekable buffer
1 parent a8c8192 commit d8363ca

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.21.0 - TBD
4+
5+
#### Bug fixes
6+
- Fixed an issue where `DBNStore.from_bytes` did not rewind seekable buffers
7+
38
## 0.20.0 - 2023-09-21
49

510
#### Enhancements

databento/common/dbnstore.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,12 @@ class MemoryDataSource(DataSource):
252252
"""
253253

254254
def __init__(self, source: BytesIO | bytes | IO[bytes]):
255-
initial_data = source if isinstance(source, bytes) else source.read()
255+
if isinstance(source, bytes):
256+
initial_data = source
257+
else:
258+
source.seek(0)
259+
initial_data = source.read()
260+
256261
if len(initial_data) == 0:
257262
raise ValueError(
258263
f"Cannot create data source from empty {type(source).__name__}",

tests/test_historical_bento.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,24 @@ def test_dbnstore_buffer_long(
924924
with pytest.raises(BentoError):
925925
dbnstore.to_json(tmp_path / "test.json")
926926

927+
def test_dbnstore_buffer_rewind(
928+
test_data: Callable[[Schema], bytes],
929+
tmp_path: Path,
930+
) -> None:
931+
"""
932+
Test that creating a DBNStore from a seekable buffer will rewind.
933+
"""
934+
# Arrange
935+
dbn_stub_data = (
936+
zstandard.ZstdDecompressor().stream_reader(test_data(Schema.MBO)).read()
937+
)
938+
939+
# Act
940+
dbn_bytes = BytesIO()
941+
dbn_bytes.write(dbn_stub_data)
942+
dbnstore = DBNStore.from_bytes(data=dbn_bytes)
943+
944+
assert len(dbnstore.to_df()) == 4
927945

928946
@pytest.mark.parametrize(
929947
"schema",

0 commit comments

Comments
 (0)