Skip to content

Commit

Permalink
G3IndexedReader: catch and complain about Seek when at eof
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasself committed Mar 29, 2024
1 parent ba64bf3 commit e3d0e15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/G3IndexedReader.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <pybindings.h>
#include <dataio.h>
#include <G3IndexedReader.h>
#include "exceptions.h"

#include <boost/filesystem.hpp>

Expand Down Expand Up @@ -90,6 +91,10 @@ void G3IndexedReader::Process(G3FramePtr frame, std::deque<G3FramePtr> &out)
}

int G3IndexedReader::Seek(int offset) {
if (stream_.peek() == EOF) {
log_error("Cannot seek; stream closed at EOF.");
throw RuntimeError_exception("Cannot seek; stream closed at EOF.");
}
return boost::iostreams::seek(stream_, offset, std::ios_base::beg);
}

Expand All @@ -111,8 +116,11 @@ PYBINDINGS("so3g") {
arg("n_frames_to_read")=0)))
.def(init<std::vector<std::string>, int>((arg("filename"),
arg("n_frames_to_read")=0)))
.def("Tell", &G3IndexedReader::Tell)
.def("Seek", &G3IndexedReader::Seek)
.def("Tell", &G3IndexedReader::Tell,
"Return the current byte offset from start of stream.")
.def("Seek", &G3IndexedReader::Seek,
"Position the stream read pointer at specific byte offset. "
"Note that once EOF is reached, Seek does not work any more.")
.def_readonly("__g3module__", true)
;
}
Expand Down
8 changes: 8 additions & 0 deletions test/test_indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ def test_seek(self):

# Now that we've seeked, our next frame should be Wiring
assert r.Process(None)[0].type == core.G3FrameType.Wiring

# Confirm exception is raised if seek at eof.
r = so3g.G3IndexedReader(self._file)
while len(r.Process(None)):
pass
pos = r.Tell()
with self.assertRaises(RuntimeError):
r.Seek(pos)

0 comments on commit e3d0e15

Please sign in to comment.