Skip to content

Commit

Permalink
Do not allow seek after EOF in G3Reader (#153)
Browse files Browse the repository at this point in the history
Upstream merge of simonsobs/so3g#172
  • Loading branch information
arahlin authored Mar 29, 2024
1 parent cacf330 commit 0ba1351
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/src/G3Reader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void G3Reader::Process(G3FramePtr frame, std::deque<G3FramePtr> &out)
}

off_t G3Reader::Seek(off_t offset) {
if (stream_.peek() == EOF && offset != Tell())
log_fatal("Cannot seek %s; stream closed at EOF.", cur_file_.c_str());
return boost::iostreams::seek(stream_, offset, std::ios_base::beg);
}

Expand Down Expand Up @@ -138,8 +140,11 @@ PYBINDINGS("core") {
arg("n_frames_to_read")=0,arg("timeout")=-1.,arg("track_filename")=false)))
.def(init<std::vector<std::string>, int, float, bool>((arg("filename"),
arg("n_frames_to_read")=0, arg("timeout")=-1.,arg("track_filename")=false)))
.def("tell", &G3Reader::Tell)
.def("seek", &G3Reader::Seek)
.def("tell", &G3Reader::Tell,
"Return the current byte offset from start of stream.")
.def("seek", &G3Reader::Seek,
"Position the stream read pointer at specific byte offset. "
"Note that once EOF is reached, seek does not work anymore.")
.def_readonly("__g3module__", true)
;
}
Expand Down
16 changes: 16 additions & 0 deletions core/tests/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,19 @@ def __call__(self, frame):
pipe.Run()

assert cached.pos is None, "Missing wiring frame"

# check EOF handling
while cached.reader(None):
pass

# can seek to EOF if at EOF
pos = cached.reader.tell()
cached.reader.seek(pos)

# can't seek backward once at EOF
try:
cached.reader.seek(0)
except:
pass
else:
assert False, "Expected RuntimeError seeking back from EOF"

0 comments on commit 0ba1351

Please sign in to comment.