Skip to content

Commit

Permalink
Throw ValueError on bad Python function argument (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Mar 2, 2024
1 parent 249c4b4 commit bc1c59b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions jormungandr/cpp/autodiff/BindVariableMatrices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ void BindVariableMatrix(py::module_& autodiff,
self.Block(rowOffset, colOffset, blockRows, blockCols) =
value.cast<int>();
} else {
fmt::println(
stderr,
"error: VariableMatrix.__setitem__ not implemented for value");
throw py::value_error(
"VariableMatrix.__setitem__ not implemented for value");
}
});
variable_matrix.def(
Expand Down Expand Up @@ -340,10 +339,10 @@ void BindVariableMatrix(py::module_& autodiff,

std::string input1_name = inputs[0].attr("__repr__")().cast<py::str>();
std::string input2_name = inputs[1].attr("__repr__")().cast<py::str>();
fmt::println(stderr,
"error: VariableMatrix: numpy method {}, ufunc {} not "
"implemented for ({}, {})",
method_name, ufunc_name, input1_name, input2_name);
throw py::value_error(
fmt::format("VariableMatrix: numpy method {}, ufunc {} not "
"implemented for ({}, {})",
method_name, ufunc_name, input1_name, input2_name));
return py::cast(VariableMatrix{self});
});

Expand Down Expand Up @@ -714,8 +713,8 @@ void BindVariableBlock(
self.Block(rowOffset, colOffset, blockRows, blockCols) =
value.cast<int>();
} else {
fmt::println(
stderr, "error: VariableBlock.__setitem__ not implemented for value");
throw py::value_error(
"VariableBlock.__setitem__ not implemented for value");
}
});
variable_block.def(
Expand Down Expand Up @@ -883,10 +882,10 @@ void BindVariableBlock(

std::string input1_name = inputs[0].attr("__repr__")().cast<py::str>();
std::string input2_name = inputs[1].attr("__repr__")().cast<py::str>();
fmt::println(stderr,
"error: VariableBlock: numpy method {}, ufunc {} not "
"implemented for ({}, {})",
method_name, ufunc_name, input1_name, input2_name);
throw py::value_error(
fmt::format("VariableBlock: numpy method {}, ufunc {} not "
"implemented for ({}, {})",
method_name, ufunc_name, input1_name, input2_name));
return py::cast(VariableMatrix{self});
});

Expand Down
2 changes: 1 addition & 1 deletion jormungandr/cpp/optimization/BindOptimizationProblem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "optimization/BindOptimizationProblem.hpp"

#include <fmt/core.h>
#include <fmt/format.h>
#include <pybind11/functional.h>
#include <pybind11/pytypes.h>
#include <sleipnir/optimization/OptimizationProblem.hpp>
Expand Down

0 comments on commit bc1c59b

Please sign in to comment.