Skip to content

Commit f551147

Browse files
committed
Raise explicit IndexError from extension module.
Do not rely on boost python translator of C++ exceptions. This fixes incorrect exceptions when pyobjcryst is imported as well. Also raise NotImplementedError for unknown enum values in the wrapper. Use abort() to assert unreachable code and avoid no-return-value compiler warnings.
1 parent 7a3e0f6 commit f551147

5 files changed

Lines changed: 19 additions & 8 deletions

File tree

srrealmodule/srreal_converters.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <valarray>
2626
#include <stdexcept>
2727
#include <cassert>
28+
#include <cstdlib>
2829

2930
#include <diffpy/Attributes.hpp>
3031
#include <diffpy/srreal/StructureAdapter.hpp>
@@ -276,7 +277,7 @@ NumPyArray_DoublePtr extractNumPyDoubleArray(::boost::python::object& obj)
276277
const char* emsg = "Cannot convert this object to numpy array.";
277278
PyErr_SetString(PyExc_TypeError, emsg);
278279
boost::python::throw_error_already_set();
279-
assert(false);
280+
abort();
280281
}
281282
boost::python::object rvobj((boost::python::handle<>(arr)));
282283
double* rvdata = static_cast<double*>(PyArray_DATA(arr));

srrealmodule/wrap_ObjCrystAdapters.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <boost/python/def.hpp>
2121

22+
#include <cstdlib>
23+
2224
#include <diffpy/features.hpp>
2325
#include <diffpy/srreal/StructureAdapter.hpp>
2426

@@ -82,8 +84,7 @@ StructureAdapterPtr convertObjCrystMolecule(python::object mol)
8284
std::string emsg = "ObjCryst support not available.";
8385
PyErr_SetString(PyExc_TypeError, emsg.c_str());
8486
boost::python::throw_error_already_set();
85-
assert(false);
86-
return StructureAdapterPtr();
87+
abort();
8788
}
8889

8990
StructureAdapterPtr convertObjCrystCrystal(python::object cryst)

srrealmodule/wrap_PairQuantity.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <boost/python/copy_non_const_reference.hpp>
3636
#include <boost/python/return_internal_reference.hpp>
3737

38+
#include <cstdlib>
39+
3840
#include "srreal_converters.hpp"
3941
#include "srreal_pickling.hpp"
4042

@@ -357,8 +359,10 @@ std::string stringevaluatortype(PQEvaluatorType tp)
357359
case OPTIMIZED:
358360
return evtp_OPTIMIZED;
359361
}
360-
std::string emsg = "Unknown internal value of PQEvaluatorType.";
361-
throw std::out_of_range(emsg);
362+
const char* emsg = "Unknown internal value of PQEvaluatorType.";
363+
PyErr_SetString(PyExc_NotImplementedError, emsg);
364+
throw_error_already_set();
365+
abort();
362366
}
363367

364368

srrealmodule/wrap_StructureAdapter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ void checkindex(const StructureAdapter& adpt, int i)
464464
if (dynamic_cast<const StructureAdapterWrap*>(&adpt)) return;
465465
// Prevent out-of-bounds crash from C++ objects.
466466
if (0 <= i && i < adpt.countSites()) return;
467-
throw std::out_of_range("Index out of range.");
467+
PyErr_SetString(PyExc_IndexError, "Index out of range.");
468+
throw_error_already_set();
468469
}
469470

470471
} // namespace

srrealmodule/wrap_StructureDifference.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <boost/python/class.hpp>
2121
#include <boost/python/slice.hpp>
2222

23+
#include <cstdlib>
24+
2325
#include <diffpy/srreal/StructureAdapter.hpp>
2426
#include <diffpy/srreal/StructureDifference.hpp>
2527

@@ -123,8 +125,10 @@ std::string get_diffmethod(const StructureDifference& sd)
123125
case StructureDifference::Method::SORTED:
124126
return "SORTED";
125127
}
126-
std::string emsg = "Unknown internal value of StructureDifference::Method.";
127-
throw std::out_of_range(emsg);
128+
const char* emsg = "Unknown internal value of StructureDifference::Method.";
129+
PyErr_SetString(PyExc_NotImplementedError, emsg);
130+
boost::python::throw_error_already_set();
131+
abort();
128132
}
129133

130134

0 commit comments

Comments
 (0)