MCPcopy Create free account
hub / github.com/pybind/pybind11 / load

Function load

tests/test_docs_advanced_cast_custom.cpp:43–62  ·  view source on GitHub ↗

Python -> C++: convert a `PyObject` into a `Point2D` and return false upon failure. The second argument indicates whether implicit conversions should be allowed. The accepted types should reflect the type hint specified by the first argument of `io_name`.

Source from the content-addressed store, hash-verified

41 // The accepted types should reflect the type hint specified by the first argument of
42 // `io_name`.
43 bool load(handle src, bool /*convert*/) {
44 // Check if handle is a Sequence
45 if (!py::isinstance<py::sequence>(src)) {
46 return false;
47 }
48 auto seq = py::reinterpret_borrow<py::sequence>(src);
49 // Check if exactly two values are in the Sequence
50 if (seq.size() != 2) {
51 return false;
52 }
53 // Check if each element is either a float or an int
54 for (auto item : seq) {
55 if (!py::isinstance<py::float_>(item) && !py::isinstance<py::int_>(item)) {
56 return false;
57 }
58 }
59 value.x = seq[0].cast<double>();
60 value.y = seq[1].cast<double>();
61 return true;
62 }
63};
64
65} // namespace detail

Callers

nothing calls this directly

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected