Skip to content

Commit 9c75fd0

Browse files
committed
Support lvalue conversion of enum_ to handle
1 parent 9f06964 commit 9c75fd0

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

include/pybind11/pybind11.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,10 @@ template <typename Type> class enum_ {
12741274
return cls;
12751275
}
12761276

1277+
operator handle() & {
1278+
return cls;
1279+
}
1280+
12771281
private:
12781282
class_<Type> cls;
12791283
dict m_entries;

tests/test_enum.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ std::string test_scoped_enum(ScopedEnum z) {
4444
test_initializer enums([](py::module &m) {
4545
m.def("test_scoped_enum", &test_scoped_enum);
4646

47-
py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic())
47+
auto e = py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic())
4848
.value("EOne", EOne)
4949
.value("ETwo", ETwo)
5050
.export_values()
@@ -59,6 +59,8 @@ test_initializer enums([](py::module &m) {
5959
.value("Two", ScopedEnum::Two)
6060
.value("Three", ScopedEnum::Three);
6161

62+
py::setattr(e, "Alias", scoped_enum);
63+
6264
py::enum_<Flags>(m, "Flags", py::arithmetic())
6365
.value("Read", Flags::Read)
6466
.value("Write", Flags::Write)

tests/test_enum.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def test_unscoped_enum():
4040
assert not (2 < UnscopedEnum.EOne)
4141

4242

43+
def test_enum_as_handle():
44+
from pybind11_tests import UnscopedEnum, ScopedEnum
45+
assert UnscopedEnum.Alias is ScopedEnum
46+
47+
4348
def test_extra_defs():
4449
from pybind11_tests import UnscopedEnum
4550
assert UnscopedEnum.EOne.x() == 2 and UnscopedEnum.ETwo.x() == 3

0 commit comments

Comments
 (0)