diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 0d6a3467227..5208ae1f65d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1378,13 +1378,12 @@ template class enum_ : public class_ { }, return_value_policy::copy); def(init([](Scalar i) { return static_cast(i); })); def(init([name, m_entries_ptr](std::string value) -> Type { - for (const auto &kv : reinterpret_borrow(m_entries_ptr)) { - std::string key = pybind11::cast(kv.first); - if (value == key) { - return pybind11::cast(kv.second); - } - } - throw value_error("\"" + value + "\" is not a valid value for enum type " + name); + pybind11::dict values = reinterpret_borrow(m_entries_ptr); + pybind11::str key = pybind11::str(value); + if(values.contains(key)) + return pybind11::cast(values[key]); + else + throw value_error("\"" + value + "\" is not a valid value for enum type " + name); })); def("__int__", [](Type value) { return (Scalar) value; }); #if PY_MAJOR_VERSION < 3