Skip to content

Commit e5409ce

Browse files
committed
Revert "Using simpler entries capture from @jagerman"
This reverts commit 2d440e0.
1 parent 0102e67 commit e5409ce

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

include/pybind11/pybind11.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,23 +1362,23 @@ template <typename Type> class enum_ : public class_<Type> {
13621362

13631363
constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
13641364

1365-
dict& entries = m_entries;
1366-
def("__repr__", [name, entries](Type value) -> pybind11::str {
1367-
for (const auto &kv : entries) {
1365+
auto m_entries_ptr = m_entries.inc_ref().ptr();
1366+
def("__repr__", [name, m_entries_ptr](Type value) -> pybind11::str {
1367+
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
13681368
if (pybind11::cast<Type>(kv.second) == value)
13691369
return pybind11::str("{}.{}").format(name, kv.first);
13701370
}
13711371
return pybind11::str("{}.???").format(name);
13721372
});
1373-
def_property_readonly_static("__members__", [entries](object /* self */) {
1373+
def_property_readonly_static("__members__", [m_entries_ptr](object /* self */) {
13741374
dict m;
1375-
for (const auto &kv : entries)
1375+
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr))
13761376
m[kv.first] = kv.second;
13771377
return m;
13781378
}, return_value_policy::copy);
13791379
def(init([](Scalar i) { return static_cast<Type>(i); }));
1380-
def(init([name, entries](std::string value) -> Type {
1381-
pybind11::dict values = entries;
1380+
def(init([name, m_entries_ptr](std::string value) -> Type {
1381+
pybind11::dict values = reinterpret_borrow<pybind11::dict>(m_entries_ptr);
13821382
pybind11::str key = pybind11::str(value);
13831383
if (values.contains(key))
13841384
return pybind11::cast<Type>(values[key]);

0 commit comments

Comments
 (0)