@@ -1426,6 +1426,16 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
1426
1426
}
1427
1427
1428
1428
PYBIND11_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
+
1429
1439
struct enum_base {
1430
1440
enum_base (handle base, handle parent) : m_base(base), m_parent(parent) { }
1431
1441
@@ -1435,29 +1445,22 @@ struct enum_base {
1435
1445
auto static_property = handle ((PyObject *) get_internals ().static_property_type );
1436
1446
1437
1447
m_base.attr (" __repr__" ) = cpp_function (
1438
- [](handle arg) -> str {
1448
+ [](object arg) -> str {
1439
1449
handle type = arg.get_type ();
1440
1450
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));
1448
1452
}, name (" __repr__" ), is_method (m_base)
1449
1453
);
1450
1454
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 (
1452
1458
[](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));
1459
1462
}, name (" name" ), is_method (m_base)
1460
- )) ;
1463
+ );
1461
1464
1462
1465
m_base.attr (" __doc__" ) = static_property (cpp_function (
1463
1466
[](handle arg) -> std::string {
0 commit comments