Skip to content

Commit

Permalink
Merge pull request #647 from lsst/tickets/DM-35457
Browse files Browse the repository at this point in the history
DM-35457: Fix handling of FITS error messages passed to C++ exceptions
  • Loading branch information
mwittgen committed Jul 30, 2022
2 parents 68453d6 + 9383817 commit d599963
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/lsst/afw/fits/_fits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,9 @@ PYBIND11_MODULE(_fits, mod) {
wrappers.addSignatureDependency("lsst.pex.exceptions");
wrappers.addSignatureDependency("lsst.daf.base");
// FIXME: after afw.image pybind wrappers are converted
//wrappers.addSignatureDependency("lsst.afw.image");
wrappers.wrapException<FitsError, lsst::pex::exceptions::IoError>("FitsError", "IoError");
//wrappers.addSignatureDependency("lsst.afw.image");
auto cls = wrappers.wrapException<FitsError, lsst::pex::exceptions::IoError>("FitsError", "IoError");
cls.def(py::init<std::string const &>());
declareImageCompression(wrappers);
declareImageScalingOptions(wrappers);
declareImageScale(wrappers);
Expand Down
7 changes: 7 additions & 0 deletions src/fits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unordered_map>
#include <filesystem>
#include <regex>
#include <cctype>

#include "fitsio.h"
extern "C" {
Expand Down Expand Up @@ -439,7 +440,13 @@ std::string makeErrorMessage(std::string const &fileName, int status, std::strin
}
os << "\ncfitsio error stack:\n";
char cfitsioMsg[FLEN_ERRMSG];
// fits_read_errmsg can return a junk string with non printable characters
// creating problem with python exception bindings
while (fits_read_errmsg(cfitsioMsg) != 0) {
cfitsioMsg[FLEN_ERRMSG-1] = char(0); // ensure termination
std::size_t len=strlen(cfitsioMsg);
for(std::size_t i = 0; i < len; i++)
if( !isprint(cfitsioMsg[i]) ) cfitsioMsg[i] = '.';
os << " " << cfitsioMsg << "\n";
}
return os.str();
Expand Down

0 comments on commit d599963

Please sign in to comment.