@@ -1486,6 +1486,16 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
14861486}
14871487
14881488PYBIND11_NAMESPACE_BEGIN (detail)
1489+
1490+ inline str enum_name (handle arg) {
1491+ dict entries = arg.get_type ().attr (" __entries" );
1492+ for (const auto &kv : entries) {
1493+ if (handle (kv.second [int_ (0 )]).equal (arg))
1494+ return pybind11::str (kv.first );
1495+ }
1496+ return " ???" ;
1497+ }
1498+
14891499struct enum_base {
14901500 enum_base (handle base, handle parent) : m_base(base), m_parent(parent) { }
14911501
@@ -1495,29 +1505,21 @@ struct enum_base {
14951505 auto static_property = handle ((PyObject *) get_internals ().static_property_type );
14961506
14971507 m_base.attr (" __repr__" ) = cpp_function (
1498- [](handle arg) -> str {
1508+ [](object arg) -> str {
14991509 handle type = type::handle_of (arg);
15001510 object type_name = type.attr (" __name__" );
1501- dict entries = type.attr (" __entries" );
1502- for (auto kv : entries) {
1503- object other = kv.second [int_ (0 )];
1504- if (other.equal (arg))
1505- return pybind11::str (" {}.{}" ).format (type_name, kv.first );
1506- }
1507- return pybind11::str (" {}.???" ).format (type_name);
1511+ return pybind11::str (" <{}.{}: {}>" ).format (type_name, enum_name (arg), int_ (arg));
15081512 }, name (" __repr__" ), is_method (m_base)
15091513 );
15101514
1511- m_base.attr (" name" ) = property (cpp_function (
1515+ m_base.attr (" name" ) = property (cpp_function (&enum_name, name (" name" ), is_method (m_base)));
1516+
1517+ m_base.attr (" __str__" ) = cpp_function (
15121518 [](handle arg) -> str {
1513- dict entries = type::handle_of (arg).attr (" __entries" );
1514- for (auto kv : entries) {
1515- if (handle (kv.second [int_ (0 )]).equal (arg))
1516- return pybind11::str (kv.first );
1517- }
1518- return " ???" ;
1519+ object type_name = type::handle_of (arg).attr (" __name__" );
1520+ return pybind11::str (" {}.{}" ).format (type_name, enum_name (arg));
15191521 }, name (" name" ), is_method (m_base)
1520- )) ;
1522+ );
15211523
15221524 m_base.attr (" __doc__" ) = static_property (cpp_function (
15231525 [](handle arg) -> std::string {
0 commit comments