From ba64bf332abc2e7c818aea31e643ca20b975f509 Mon Sep 17 00:00:00 2001 From: Matthew Hasselfield Date: Fri, 29 Mar 2024 14:49:25 +0000 Subject: [PATCH 1/2] Fully remove old exception mappings --- src/exceptions.cxx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/exceptions.cxx b/src/exceptions.cxx index 1d2a99d..3bd4923 100644 --- a/src/exceptions.cxx +++ b/src/exceptions.cxx @@ -26,12 +26,6 @@ static void translate_ValueError(so3g_exception const& e) namespace bp = boost::python; PYBINDINGS("so3g") { - // bp::register_exception_translator (&translate_TypeError); - // bp::register_exception_translator (&translate_ValueError); - // bp::register_exception_translator (&translate_RuntimeError); - // bp::register_exception_translator (&translate_RuntimeError); - // bp::register_exception_translator (&translate_RuntimeError); - // bp::register_exception_translator (&translate_ValueError); bp::register_exception_translator (&translate_RuntimeError); bp::register_exception_translator (&translate_TypeError); bp::register_exception_translator (&translate_ValueError); From dfa2f95deda49e49b836f61fa735d3b49914af0e Mon Sep 17 00:00:00 2001 From: Matthew Hasselfield Date: Fri, 29 Mar 2024 14:49:55 +0000 Subject: [PATCH 2/2] G3IndexedReader: catch and complain about back-Seek when at eof --- src/G3IndexedReader.cxx | 12 ++++++++++-- test/test_indexed.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/G3IndexedReader.cxx b/src/G3IndexedReader.cxx index bad295e..4e8e6b6 100644 --- a/src/G3IndexedReader.cxx +++ b/src/G3IndexedReader.cxx @@ -1,6 +1,7 @@ #include #include #include +#include "exceptions.h" #include @@ -90,6 +91,10 @@ void G3IndexedReader::Process(G3FramePtr frame, std::deque &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); } @@ -111,8 +116,11 @@ PYBINDINGS("so3g") { arg("n_frames_to_read")=0))) .def(init, 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) ; } diff --git a/test/test_indexed.py b/test/test_indexed.py index a866fbd..8f3769e 100644 --- a/test/test_indexed.py +++ b/test/test_indexed.py @@ -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)