Make attr and item accessors throw on error instead of returning nullptr

This also adds the `hasattr` and `getattr` functions which are needed
with the new attribute behavior. The new functions behave exactly like
their Python counterparts.

Similarly `object` gets a `contains` method which calls `__contains__`,
i.e. it's the same as the `in` keyword in Python.
This commit is contained in:
Dean Moldovan
2016-09-21 01:06:32 +02:00
parent 37e22e436e
commit 865e43034b
5 changed files with 90 additions and 42 deletions

View File

@@ -39,7 +39,7 @@ PYBIND11_PLUGIN(pybind11_tests) {
for (const auto &initializer : initializers())
initializer(m);
if (!m.attr("have_eigen")) m.attr("have_eigen") = py::cast(false);
if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = py::cast(false);
return m.ptr();
}