@@ -1426,6 +1426,16 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
14261426}
14271427
14281428PYBIND11_NAMESPACE_BEGIN (detail)
1429+
1430+ inline str enum_name (handle arg) {
1431+ dict entries = arg.get_type ().attr (" __entries" );
1432+ for (const auto &kv : entries) {
1433+ if (handle (kv.second [int_ (0 )]).equal (arg))
1434+ return pybind11::str (kv.first );
1435+ }
1436+ return " ???" ;
1437+ }
1438+
14291439struct enum_base {
14301440 enum_base (handle base, handle parent) : m_base(base), m_parent(parent) { }
14311441
@@ -1435,29 +1445,22 @@ struct enum_base {
14351445 auto static_property = handle ((PyObject *) get_internals ().static_property_type );
14361446
14371447 m_base.attr (" __repr__" ) = cpp_function (
1438- [](handle arg) -> str {
1448+ [](object arg) -> str {
14391449 handle type = arg.get_type ();
14401450 object type_name = type.attr (" __name__" );
1441- dict entries = type.attr (" __entries" );
1442- for (const auto &kv : entries) {
1443- object other = kv.second [int_ (0 )];
1444- if (other.equal (arg))
1445- return pybind11::str (" {}.{}" ).format (type_name, kv.first );
1446- }
1447- return pybind11::str (" {}.???" ).format (type_name);
1451+ return pybind11::str (" <{}.{}: {}>" ).format (type_name, enum_name (arg), int_ (arg));
14481452 }, name (" __repr__" ), is_method (m_base)
14491453 );
14501454
1451- m_base.attr (" name" ) = property (cpp_function (
1455+ m_base.attr (" name" ) = property (cpp_function (&enum_name, name (" name" ), is_method (m_base)));
1456+
1457+ m_base.attr (" __str__" ) = cpp_function (
14521458 [](handle arg) -> str {
1453- dict entries = arg.get_type ().attr (" __entries" );
1454- for (const auto &kv : entries) {
1455- if (handle (kv.second [int_ (0 )]).equal (arg))
1456- return pybind11::str (kv.first );
1457- }
1458- return " ???" ;
1459+ handle type = arg.get_type ();
1460+ object type_name = type.attr (" __name__" );
1461+ return pybind11::str (" {}.{}" ).format (type_name, enum_name (arg));
14591462 }, name (" name" ), is_method (m_base)
1460- )) ;
1463+ );
14611464
14621465 m_base.attr (" __doc__" ) = static_property (cpp_function (
14631466 [](handle arg) -> std::string {
0 commit comments