Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HDembinski committed Sep 11, 2022
1 parent 4d980a0 commit 726ef92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/pystream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
pystreambuf::pystreambuf(py::object iohandle, int size)
: buffer_(size), iohandle_(iohandle) {
// this ctor must not throw
if (hasattr(iohandle, "readinto") || hasattr(iohandle, "readinto1"))
readinto_ =
iohandle.attr(py::hasattr(iohandle, "readinto1") ? "readinto1" : "readinto");
const bool has_readinto1 = hasattr(iohandle, "readinto1");
if (has_readinto1 || hasattr(iohandle, "readinto"))
readinto_ = iohandle.attr(has_readinto1 ? "readinto1" : "readinto");
if (hasattr(iohandle, "write")) write_ = iohandle.attr("write");
char* b = buffer_.mutable_data();
setp(b, b + size);
Expand Down Expand Up @@ -86,9 +86,9 @@ int pystreambuf::sync_() {
}

void pystreambuf::pywrite_buffer() {
const int s = std::distance(pbase(), pptr());
py::gil_scoped_acquire g;
assert(write_);
const int s = std::distance(pbase(), pptr());
// TODO there is probably a more efficient way
write_(buffer_[py::slice(0, s, 1)]);
}
Expand Down
18 changes: 9 additions & 9 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
def test_pystream_1():
fn = str(Path(__file__).parent / "sibyll21.dat")
with open(fn, "rb") as f:
with pyiostream(f, 1000) as pis:
with io.ReaderAscii(pis) as r:
with pyiostream(f, 1000) as s:
with io.ReaderAscii(s) as r:
ev1 = r.read()
with io.ReaderAscii(fn) as r:
ev2 = r.read()
Expand All @@ -37,8 +37,8 @@ def test_pystream_2():
f2.write(f.read())

with gzip.open(fn2) as f:
with pyiostream(f, 1000) as pis:
with io.ReaderAscii(pis) as r:
with pyiostream(f, 1000) as s:
with io.ReaderAscii(s) as r:
ev1 = r.read()

with io.ReaderAscii(fn) as r:
Expand All @@ -52,14 +52,14 @@ def test_pystream_2():
def test_pystream_3(evt): # noqa
fn = "test_pystream_3.dat.gz"
with gzip.open(fn, "w") as f:
with pyiostream(f, 1000) as pis:
with io.WriterAscii(pis) as w:
with pyiostream(f, 1000) as s:
with io.WriterAscii(s) as w:
w.write(evt)

with gzip.open(fn) as f:
pis = pyiostream(f, 1000)
with io.ReaderAscii(pis) as r:
evt2 = r.read()
with pyiostream(f, 1000) as s:
with io.ReaderAscii(s) as r:
evt2 = r.read()

assert evt == evt2

Expand Down

0 comments on commit 726ef92

Please sign in to comment.