@@ -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 cast<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,8 +1445,10 @@ template <typename Type> class enum_ : public class_<Type> {
14361445private:
14371446 dict m_entries;
14381447 handle m_parent;
1448+ std::string m_name;
14391449};
14401450
1451+
14411452NAMESPACE_BEGIN (detail)
14421453
14431454
@@ -1695,6 +1706,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
16951706}
16961707NAMESPACE_END (detail)
16971708
1709+
16981710template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
16991711void print(Args &&...args) {
17001712 auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
0 commit comments