@@ -1358,7 +1358,7 @@ template <typename Type> class enum_ : public class_<Type> {
13581358
13591359 template <typename ... Extra>
13601360 enum_ (const handle &scope, const char *name, const Extra&... extra)
1361- : class_<Type>(scope, name, extra...), m_entries(), m_parent(scope) {
1361+ : class_<Type>(scope, name, extra...), m_entries(), m_parent(scope), m_name(name) {
13621362
13631363 constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
13641364
@@ -1377,6 +1377,15 @@ template <typename Type> class enum_ : public class_<Type> {
13771377 return m;
13781378 }, return_value_policy::copy);
13791379 def (init ([](Scalar i) { return static_cast <Type>(i); }));
1380+ def (init ([this , m_entries_ptr](std::string value) -> Type {
1381+ for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
1382+ std::string key = cast<str>(kv.first );
1383+ if (value == key || key == m_name + " ::" + value) {
1384+ return reinterpret_borrow<Type>(kv.second );
1385+ }
1386+ }
1387+ throw value_error (" \" " + value + " \" is not a valid value for enum type " + m_name);
1388+ }));
13801389 def (" __int__" , [](Type value) { return (Scalar) value; });
13811390 #if PY_MAJOR_VERSION < 3
13821391 def (" __long__" , [](Type value) { return (Scalar) value; });
@@ -1436,6 +1445,7 @@ template <typename Type> class enum_ : public class_<Type> {
14361445private:
14371446 dict m_entries;
14381447 handle m_parent;
1448+ std::string m_name;
14391449};
14401450
14411451NAMESPACE_BEGIN (detail)
0 commit comments