mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-22 00:19:18 +00:00
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:
parent
51d18aa252
commit
e6fd2cd5ab
@ -561,7 +561,7 @@ public:
|
|||||||
(std::is_integral<T>::value && sizeof(py_type) != sizeof(T) &&
|
(std::is_integral<T>::value && sizeof(py_type) != sizeof(T) &&
|
||||||
(py_value < (py_type) std::numeric_limits<T>::min() ||
|
(py_value < (py_type) std::numeric_limits<T>::min() ||
|
||||||
py_value > (py_type) std::numeric_limits<T>::max()))) {
|
py_value > (py_type) std::numeric_limits<T>::max()))) {
|
||||||
#if PY_VERSION_HEX < 0x03000000
|
#if PY_VERSION_HEX < 0x03000000 && !defined(PYPY_VERSION)
|
||||||
bool type_error = PyErr_ExceptionMatches(PyExc_SystemError);
|
bool type_error = PyErr_ExceptionMatches(PyExc_SystemError);
|
||||||
#else
|
#else
|
||||||
bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
|
bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
|
||||||
|
@ -1197,6 +1197,9 @@ public:
|
|||||||
}, return_value_policy::copy);
|
}, return_value_policy::copy);
|
||||||
def("__init__", [](Type& value, Scalar i) { value = (Type)i; });
|
def("__init__", [](Type& value, Scalar i) { value = (Type)i; });
|
||||||
def("__int__", [](Type value) { return (Scalar) value; });
|
def("__int__", [](Type value) { return (Scalar) value; });
|
||||||
|
#if PY_MAJOR_VERSION < 3
|
||||||
|
def("__long__", [](Type value) { return (Scalar) value; });
|
||||||
|
#endif
|
||||||
def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
|
def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
|
||||||
def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
|
def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
|
||||||
if (is_arithmetic) {
|
if (is_arithmetic) {
|
||||||
|
@ -65,4 +65,8 @@ test_initializer enums([](py::module &m) {
|
|||||||
.value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
|
.value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
|
||||||
.value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
|
.value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
|
||||||
.export_values();
|
.export_values();
|
||||||
|
|
||||||
|
m.def("test_enum_to_int", [](int) { });
|
||||||
|
m.def("test_enum_to_uint", [](uint32_t) { });
|
||||||
|
m.def("test_enum_to_long_long", [](long long) { });
|
||||||
});
|
});
|
||||||
|
@ -115,3 +115,14 @@ def test_binary_operators():
|
|||||||
state2 = ~state
|
state2 = ~state
|
||||||
assert state2 == -7
|
assert state2 == -7
|
||||||
assert int(state ^ state2) == -1
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user