@@ -1481,6 +1481,16 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
14811481}
14821482
14831483PYBIND11_NAMESPACE_BEGIN (detail)
1484+
1485+ inline str enum_name (handle arg) {
1486+ dict entries = arg.get_type ().attr (" __entries" );
1487+ for (const auto &kv : entries) {
1488+ if (handle (kv.second [int_ (0 )]).equal (arg))
1489+ return pybind11::str (kv.first );
1490+ }
1491+ return " ???" ;
1492+ }
1493+
14841494struct enum_base {
14851495 enum_base (handle base, handle parent) : m_base(base), m_parent(parent) { }
14861496
@@ -1490,29 +1500,21 @@ struct enum_base {
14901500 auto static_property = handle ((PyObject *) get_internals ().static_property_type );
14911501
14921502 m_base.attr (" __repr__" ) = cpp_function (
1493- [](handle arg) -> str {
1503+ [](object arg) -> str {
14941504 handle type = type::handle_of (arg);
14951505 object type_name = type.attr (" __name__" );
1496- dict entries = type.attr (" __entries" );
1497- for (const auto &kv : entries) {
1498- object other = kv.second [int_ (0 )];
1499- if (other.equal (arg))
1500- return pybind11::str (" {}.{}" ).format (type_name, kv.first );
1501- }
1502- return pybind11::str (" {}.???" ).format (type_name);
1506+ return pybind11::str (" <{}.{}: {}>" ).format (type_name, enum_name (arg), int_ (arg));
15031507 }, name (" __repr__" ), is_method (m_base)
15041508 );
15051509
1506- m_base.attr (" name" ) = property (cpp_function (
1510+ m_base.attr (" name" ) = property (cpp_function (&enum_name, name (" name" ), is_method (m_base)));
1511+
1512+ m_base.attr (" __str__" ) = cpp_function (
15071513 [](handle arg) -> str {
1508- dict entries = type::handle_of (arg).attr (" __entries" );
1509- for (const auto &kv : entries) {
1510- if (handle (kv.second [int_ (0 )]).equal (arg))
1511- return pybind11::str (kv.first );
1512- }
1513- return " ???" ;
1514+ object type_name = type::handle_of (arg).attr (" __name__" );
1515+ return pybind11::str (" {}.{}" ).format (type_name, enum_name (arg));
15141516 }, name (" name" ), is_method (m_base)
1515- )) ;
1517+ );
15161518
15171519 m_base.attr (" __doc__" ) = static_property (cpp_function (
15181520 [](handle arg) -> std::string {
0 commit comments