Browse Source

python: use AssertionError for trade.AbstractImporter usage errors.

Because using RuntimeError conflates with import failures. Also update
and fix docs to not show ValueError for where IndexError should be.
pull/15/head
Vladimír Vondruš 4 years ago
parent
commit
8e8a03e175
  1. 68
      doc/python/magnum.trade.rst
  2. 40
      src/python/magnum/test/test_trade.py
  3. 14
      src/python/magnum/trade.cpp

68
doc/python/magnum.trade.rst

@ -97,68 +97,72 @@
:raise RuntimeError: If file opening fails
.. py:property:: magnum.trade.AbstractImporter.mesh_count
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:function:: magnum.trade.AbstractImporter.mesh_level_count
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than :ref:`mesh_count`
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than :ref:`mesh_count`
.. py:function:: magnum.trade.AbstractImporter.mesh_for_name
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:function:: magnum.trade.AbstractImporter.mesh_name
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than :ref:`mesh_count`
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than :ref:`mesh_count`
.. py:function:: magnum.trade.AbstractImporter.mesh
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than :ref:`mesh_count`
:raise AssertionError: If no file is opened
:raise RuntimeError: If mesh import fails
:raise IndexError: If :p:`id` is negative or not less than :ref:`mesh_count`
.. py:property:: magnum.trade.AbstractImporter.image1d_count
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:property:: magnum.trade.AbstractImporter.image2d_count
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:property:: magnum.trade.AbstractImporter.image3d_count
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:function:: magnum.trade.AbstractImporter.image1d_level_count
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image1d_count`
.. py:function:: magnum.trade.AbstractImporter.image2d_level_count
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image2d_count`
.. py:function:: magnum.trade.AbstractImporter.image3d_level_count
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image3d_count`
.. py:function:: magnum.trade.AbstractImporter.image1d_for_name
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:function:: magnum.trade.AbstractImporter.image2d_for_name
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:function:: magnum.trade.AbstractImporter.image3d_for_name
:raise RuntimeError: If no file is opened
:raise AssertionError: If no file is opened
.. py:function:: magnum.trade.AbstractImporter.image1d_name
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image1d_count`
.. py:function:: magnum.trade.AbstractImporter.image2d_name
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image2d_count`
.. py:function:: magnum.trade.AbstractImporter.image3d_name
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image3d_count`
.. py:function:: magnum.trade.AbstractImporter.image1d
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise RuntimeError: If image import fails
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image1d_count`
.. py:function:: magnum.trade.AbstractImporter.image2d
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise RuntimeError: If image import fails
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image2d_count`
.. py:function:: magnum.trade.AbstractImporter.image3d
:raise RuntimeError: If no file is opened
:raise ValueError: If :p:`id` is negative or not less than
:raise AssertionError: If no file is opened
:raise RuntimeError: If image import fails
:raise IndexError: If :p:`id` is negative or not less than
:ref:`image3d_count`

40
src/python/magnum/test/test_trade.py

@ -116,53 +116,53 @@ class Importer(unittest.TestCase):
importer = trade.ImporterManager().load_and_instantiate('StbImageImporter')
self.assertFalse(importer.is_opened)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.mesh_count
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.mesh_level_count(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.mesh_for_name('')
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.mesh_name(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.mesh(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image1d_count
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image2d_count
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image3d_count
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image1d_level_count(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image2d_level_count(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image3d_level_count(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image1d_for_name('')
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image2d_for_name('')
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image3d_for_name('')
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image1d_name(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image2d_name(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image3d_name(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image1d(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image2d(0)
with self.assertRaisesRegex(RuntimeError, "no file opened"):
with self.assertRaisesRegex(AssertionError, "no file opened"):
importer.image3d(0)
def test_index_oob(self):

14
src/python/magnum/trade.cpp

@ -143,14 +143,14 @@ template<UnsignedInt dimensions> void imageData(py::class_<Trade::ImageData<dime
exactly two, in fact. */
template<class R, R(Trade::AbstractImporter::*f)() const> R checkOpened(Trade::AbstractImporter& self) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}
return (self.*f)();
}
template<class R, class Arg1, R(Trade::AbstractImporter::*f)(Arg1)> R checkOpened(Trade::AbstractImporter& self, Arg1 arg1) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}
return (self.*f)(arg1);
@ -158,7 +158,7 @@ template<class R, class Arg1, R(Trade::AbstractImporter::*f)(Arg1)> R checkOpene
/** @todo drop this in favor of our own string caster */
template<class R, R(Trade::AbstractImporter::*f)(Containers::StringView)> R checkOpenedString(Trade::AbstractImporter& self, const std::string& arg1) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}
return (self.*f)(arg1);
@ -166,7 +166,7 @@ template<class R, R(Trade::AbstractImporter::*f)(Containers::StringView)> R chec
template<class R, R(Trade::AbstractImporter::*f)(UnsignedInt), UnsignedInt(Trade::AbstractImporter::*bounds)() const> R checkOpenedBounds(Trade::AbstractImporter& self, UnsignedInt id) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}
@ -180,7 +180,7 @@ template<class R, R(Trade::AbstractImporter::*f)(UnsignedInt), UnsignedInt(Trade
/** @todo drop this in favor of our own string caster */
template<Containers::String(Trade::AbstractImporter::*f)(UnsignedInt), UnsignedInt(Trade::AbstractImporter::*bounds)() const> std::string checkOpenedBoundsReturnsString(Trade::AbstractImporter& self, UnsignedInt id) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}
@ -194,7 +194,7 @@ template<Containers::String(Trade::AbstractImporter::*f)(UnsignedInt), UnsignedI
template<class R, Containers::Optional<R>(Trade::AbstractImporter::*f)(UnsignedInt), UnsignedInt(Trade::AbstractImporter::*bounds)() const> R checkOpenedBoundsResult(Trade::AbstractImporter& self, UnsignedInt id) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}
@ -216,7 +216,7 @@ template<class R, Containers::Optional<R>(Trade::AbstractImporter::*f)(UnsignedI
template<class R, Containers::Optional<R>(Trade::AbstractImporter::*f)(UnsignedInt, UnsignedInt), UnsignedInt(Trade::AbstractImporter::*bounds)() const, UnsignedInt(Trade::AbstractImporter::*levelBounds)(UnsignedInt)> R checkOpenedBoundsResult(Trade::AbstractImporter& self, UnsignedInt id, UnsignedInt level) {
if(!self.isOpened()) {
PyErr_SetString(PyExc_RuntimeError, "no file opened");
PyErr_SetString(PyExc_AssertionError, "no file opened");
throw py::error_already_set{};
}

Loading…
Cancel
Save