@@ -1481,6 +1481,16 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
1481
1481
}
1482
1482
1483
1483
PYBIND11_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
+
1484
1494
struct enum_base {
1485
1495
enum_base (handle base, handle parent) : m_base(base), m_parent(parent) { }
1486
1496
@@ -1490,29 +1500,21 @@ struct enum_base {
1490
1500
auto static_property = handle ((PyObject *) get_internals ().static_property_type );
1491
1501
1492
1502
m_base.attr (" __repr__" ) = cpp_function (
1493
- [](handle arg) -> str {
1503
+ [](object arg) -> str {
1494
1504
handle type = type::handle_of (arg);
1495
1505
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));
1503
1507
}, name (" __repr__" ), is_method (m_base)
1504
1508
);
1505
1509
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 (
1507
1513
[](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));
1514
1516
}, name (" name" ), is_method (m_base)
1515
- )) ;
1517
+ );
1516
1518
1517
1519
m_base.attr (" __doc__" ) = static_property (cpp_function (
1518
1520
[](handle arg) -> std::string {
0 commit comments