Skip to content

Commit

Permalink
Implicit conversion from enum to int for Python 3.8 (fix by @sizmailov)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Sep 20, 2019
1 parent 5fd187e commit 31680e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 4 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,10 @@ template <typename Type> class enum_ : public class_<Type> {
#if PY_MAJOR_VERSION < 3
def("__long__", [](Type value) { return (Scalar) value; });
#endif
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8
def("__index__", [](Type value) { return (Scalar) value; });
#endif

cpp_function setstate(
[](Type &value, Scalar arg) { value = static_cast<Type>(arg); },
is_method(*this));
Expand Down
15 changes: 6 additions & 9 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,12 @@ def test_binary_operators():


def test_enum_to_int():
import sys
# Implicit conversion to integers is deprecated in Python >= 3.8
if sys.version_info < (3, 8):
m.test_enum_to_int(m.Flags.Read)
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
m.test_enum_to_uint(m.Flags.Read)
m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
m.test_enum_to_long_long(m.Flags.Read)
m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
m.test_enum_to_int(m.Flags.Read)
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
m.test_enum_to_uint(m.Flags.Read)
m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
m.test_enum_to_long_long(m.Flags.Read)
m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)


def test_duplicate_enum_name():
Expand Down

0 comments on commit 31680e6

Please sign in to comment.