Skip to content

Commit

Permalink
🐛 (io) make bytes buffer not fail in weird cirumstances
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwoerpel committed Feb 8, 2024
1 parent a9bf81f commit 897c0c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion anystore/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
self.args = args
kwargs["mode"] = kwargs.get("mode", DEFAULT_MODE)
self.sys_io = _get_sysio(kwargs["mode"])
if kwargs["mode"].endswith("b"):
if hasattr(self.sys_io, "buffer"):
self.sys_io = self.sys_io.buffer
self.kwargs = kwargs
self.handler: OpenFile | TextIO | None = None
Expand Down
8 changes: 8 additions & 0 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ def get_data2(*args, **kwargs):
assert store.get("X") is None
assert get_data2("x") == "data2"
assert store.get("X") == b"data2"

# not yet existing
@anycache(uri=tmp_path / "foo", key_func=lambda x: x)
def get_data3(data):
return data

assert get_data3("bar") == "bar"
assert (tmp_path / "foo" / "bar").exists()
11 changes: 11 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import pytest
from moto import mock_aws
from anystore.exceptions import DoesNotExist
Expand Down Expand Up @@ -48,3 +49,13 @@ def test_store(tmp_path, fixtures_path):

store = Store.from_json_uri(fixtures_path / "store.json")
assert store.uri == "file:///tmp/cache"

# put into not yet existing sub paths
store = Store(uri="s3://anystore", raise_on_nonexist=False)
store.put("foo/bar/baz", 1)
assert store.get("foo/bar/baz") == 1

store = Store(uri=tmp_path / "foo", raise_on_nonexist=False)
store.put("/bar/baz", 1)
assert (tmp_path / "foo/bar/baz").exists()
assert store.get("/bar/baz") == 1

0 comments on commit 897c0c5

Please sign in to comment.