Skip to content

Commit 4a94247

Browse files
committed
Implement an enum_ property "name"
The property returns the enum_ value as a string. For example: >>> import module >>> module.enum.VALUE enum.VALUE >>> str(module.enum.VALUE) 'enum.VALUE' >>> module.enum.VALUE.name 'VALUE' This is actually the equivalent of Boost.Python "name" property.
1 parent 6d0b470 commit 4a94247

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

include/pybind11/pybind11.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,7 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
13641364
template <typename Type> class enum_ : public class_<Type> {
13651365
public:
13661366
using class_<Type>::def;
1367+
using class_<Type>::def_property_readonly;
13671368
using class_<Type>::def_property_readonly_static;
13681369
using Scalar = typename std::underlying_type<Type>::type;
13691370

@@ -1381,6 +1382,13 @@ template <typename Type> class enum_ : public class_<Type> {
13811382
}
13821383
return pybind11::str("{}.???").format(name);
13831384
});
1385+
def_property_readonly("name", [m_entries_ptr](Type value) -> pybind11::str {
1386+
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
1387+
if (pybind11::cast<Type>(kv.second[int_(0)]) == value)
1388+
return pybind11::str("{}").format(kv.first);
1389+
}
1390+
return pybind11::str("???");
1391+
});
13841392
def_property_readonly_static("__doc__", [m_entries_ptr](handle self) {
13851393
std::string docstring;
13861394
const char *tp_doc = ((PyTypeObject *) self.ptr())->tp_doc;

tests/test_enum.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ def test_unscoped_enum():
66
assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
77
assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
88
assert str(m.EOne) == "UnscopedEnum.EOne"
9+
10+
# name property
11+
assert m.UnscopedEnum.EOne.name == "EOne"
12+
assert m.UnscopedEnum.ETwo.name == "ETwo"
13+
assert m.EOne.name == "EOne"
14+
# name readonly
15+
with pytest.raises(AttributeError):
16+
m.UnscopedEnum.EOne.name = ""
17+
# name returns a copy
18+
foo = m.UnscopedEnum.EOne.name
19+
foo = "bar"
20+
assert m.UnscopedEnum.EOne.name == "EOne"
21+
922
# __members__ property
1023
assert m.UnscopedEnum.__members__ == \
1124
{"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo}

0 commit comments

Comments
 (0)