File tree 2 files changed +11
-5
lines changed 2 files changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -1469,8 +1469,11 @@ struct enum_base {
1469
1469
#undef PYBIND11_ENUM_OP_CONV
1470
1470
#undef PYBIND11_ENUM_OP_STRICT
1471
1471
1472
- m_base. attr ( " __hash__ " ) = cpp_function (
1472
+ object getstate = cpp_function (
1473
1473
[](object arg) { return int_ (arg); }, is_method (m_base));
1474
+
1475
+ m_base.attr (" __getstate__" ) = getstate;
1476
+ m_base.attr (" __hash__" ) = getstate;
1474
1477
}
1475
1478
1476
1479
PYBIND11_NOINLINE void value (char const * name_, object value, const char *doc = nullptr ) {
@@ -1517,10 +1520,7 @@ template <typename Type> class enum_ : public class_<Type> {
1517
1520
#if PY_MAJOR_VERSION < 3
1518
1521
def (" __long__" , [](Type value) { return (Scalar) value; });
1519
1522
#endif
1520
-
1521
- // Pickling and unpickling -- needed for use with the 'multiprocessing' module
1522
- def (pickle ([](const Type &value) { return pybind11::make_tuple ((Scalar) value); },
1523
- [](tuple t) { return static_cast <Type>(t[0 ].cast <Scalar>()); }));
1523
+ def (" __setstate__" , [](Type &value, Scalar arg) { value = static_cast <Type>(arg); });
1524
1524
}
1525
1525
1526
1526
// / Export enumeration entries into the parent scope
Original file line number Diff line number Diff line change @@ -34,3 +34,9 @@ def test_roundtrip_with_dict(cls_name):
34
34
assert p2 .value == p .value
35
35
assert p2 .extra == p .extra
36
36
assert p2 .dynamic == p .dynamic
37
+
38
+
39
+ def test_enum_pickle ():
40
+ from pybind11_tests import enums as e
41
+ data = pickle .dumps (e .EOne , 2 )
42
+ assert e .EOne == pickle .loads (data )
You can’t perform that action at this time.
0 commit comments