enum_: fix implicit conversion on Python 2.7

Enumerations on Python 2.7 were not always implicitly converted to
integers (depending on the target size). This patch adds a __long__
conversion function (only enabled on 2.7) which fixes this issue.

The attached test case fails without this patch.
This commit is contained in:
Wenzel Jakob
2017-04-28 14:46:52 +02:00
parent 51d18aa252
commit e6fd2cd5ab
4 changed files with 19 additions and 1 deletions

View File

@@ -115,3 +115,14 @@ def test_binary_operators():
state2 = ~state
assert state2 == -7
assert int(state ^ state2) == -1
def test_enum_to_int():
from pybind11_tests import Flags, ClassWithUnscopedEnum
from pybind11_tests import test_enum_to_int, test_enum_to_uint, test_enum_to_long_long
test_enum_to_int(Flags.Read)
test_enum_to_int(ClassWithUnscopedEnum.EMode.EFirstMode)
test_enum_to_uint(Flags.Read)
test_enum_to_uint(ClassWithUnscopedEnum.EMode.EFirstMode)
test_enum_to_long_long(Flags.Read)
test_enum_to_long_long(ClassWithUnscopedEnum.EMode.EFirstMode)