diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index fc2612b6e..6ca49a89e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -561,7 +561,7 @@ public: (std::is_integral::value && sizeof(py_type) != sizeof(T) && (py_value < (py_type) std::numeric_limits::min() || py_value > (py_type) std::numeric_limits::max()))) { -#if PY_VERSION_HEX < 0x03000000 +#if PY_VERSION_HEX < 0x03000000 && !defined(PYPY_VERSION) bool type_error = PyErr_ExceptionMatches(PyExc_SystemError); #else bool type_error = PyErr_ExceptionMatches(PyExc_TypeError); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 03a709b26..62ee78624 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1197,6 +1197,9 @@ public: }, return_value_policy::copy); def("__init__", [](Type& value, Scalar i) { value = (Type)i; }); 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("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; }); if (is_arithmetic) { diff --git a/tests/test_enum.cpp b/tests/test_enum.cpp index 09f334cdb..67341f4b0 100644 --- a/tests/test_enum.cpp +++ b/tests/test_enum.cpp @@ -65,4 +65,8 @@ test_initializer enums([](py::module &m) { .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode) .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode) .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) { }); }); diff --git a/tests/test_enum.py b/tests/test_enum.py index ba7e22ab0..46292f474 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -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)