Skip to content

Commit

Permalink
Merge pull request #172 from simonsobs/g3indexed-loose-ends
Browse files Browse the repository at this point in the history
G3IndexedReader: catch and complain about Seek when at eof
  • Loading branch information
mhasself authored Mar 29, 2024
2 parents 44cdc8b + dfa2f95 commit 6f0edb3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 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 && offset != Tell()) {
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
6 changes: 0 additions & 6 deletions src/exceptions.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ static void translate_ValueError(so3g_exception const& e)
namespace bp = boost::python;
PYBINDINGS("so3g")
{
// bp::register_exception_translator<buffer_exception> (&translate_TypeError);
// bp::register_exception_translator<dtype_exception> (&translate_ValueError);
// bp::register_exception_translator<shape_exception> (&translate_RuntimeError);
// bp::register_exception_translator<agreement_exception> (&translate_RuntimeError);
// bp::register_exception_translator<tiling_exception> (&translate_RuntimeError);
// bp::register_exception_translator<general_agreement_exception> (&translate_ValueError);
bp::register_exception_translator<RuntimeError_exception> (&translate_RuntimeError);
bp::register_exception_translator<TypeError_exception> (&translate_TypeError);
bp::register_exception_translator<ValueError_exception> (&translate_ValueError);
Expand Down
12 changes: 12 additions & 0 deletions test/test_indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ 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()
# Ok to seek to EOF if at EOF.
r.Seek(pos)

# No back seeking once there, though.
with self.assertRaises(RuntimeError):
r.Seek(0)

0 comments on commit 6f0edb3

Please sign in to comment.