PR summary
As mentioned in #32532 and in the nanobind Porting Guide, type casters may not raise exceptions:
Type casters may not raise C++ exceptions. Both from_python() and from_cpp() must be annotated with noexcept.
Since we used exceptions for error messages, I introduced mpl::defer to defer array creation until after a bound function was called. This PR extends mpl::defer to other types using a new mpl::convert function.
mpl::convert has the following signature:
using py::object; // or using nb::object
template <typename T>
void convert(T &dst, const object &src, const char *arg_name)
Each type caster is replaced by a mpl::convert overload:
void convert(agg::trans_affine &dst, const object &src, const char *arg_name)
{
// If None assume identity transform
if (src.is_none()) {
return;
}
auto arr = mpl::array<double, mpl::shape<3, 3>>(src, arg_name);
auto data = arr.data();
dst.sx = data[0];
dst.shx = data[1];
dst.tx = data[2];
dst.shy = data[3];
dst.sy = data[4];
dst.ty = data[5];
}
mpl::defer will will automatically use the appropriate convert() function when value() is called. This allows us to do the following:
static OutputPointArray
Py_transform_points(const mpl::defer<InputPointArray> &points_obj,
const mpl::defer<agg::trans_affine> &trans_obj)
{
auto points = points_obj.value("points");
auto trans = trans_obj.value("trans");
auto result = OutputPointArray(points.shape(0));
transform_points(points, trans, result);
return result;
}
Additional Changes
- I removed some unneeded template parameters (
template <class PathIterator>) and replaced with the actual class (mpl::PathIterator).
- I renamed some template parameters.
- I removed "py_converters.cpp" as the converter is now
inline in "py_converters.h".
- I renamed some function arguments to match those in backend_bases.py.
AI Disclosure
- I generally use AI as a web search engine.
- I asked AI for examples on different approaching to implementing
mpl::convert and hooking it up to mpl::defer. I used a variation of one of these approaches and wrote the code myself.
PR quality check
- Use an expressive title, e.g. "Fix title font property precedence"
- New and changed code is tested
- [/] Plotting related features are demonstrated in an example
- [/] New features and API changes have release notes
- [/] Documentation complies with general and docstring guidelines