diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 455d89313..e23df9a7e 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -461,4 +461,11 @@ public: } }; +inline size_t len(handle h) { + ssize_t result = PyObject_Length(h.ptr()); + if (result < 0) + pybind11_fail("Unable to compute length of object"); + return (size_t) result; +} + NAMESPACE_END(pybind11)