diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index adf512cc23..e307aa3413 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1902,9 +1902,26 @@ class int_ : public object { template ::value, int> = 0> // NOLINTNEXTLINE(google-explicit-constructor) operator T() const { - return std::is_unsigned::value ? detail::as_unsigned(m_ptr) - : sizeof(T) <= sizeof(long) ? (T) PyLong_AsLong(m_ptr) - : (T) PYBIND11_LONG_AS_LONGLONG(m_ptr); + using wide_t = detail::conditional_t< + std::is_unsigned::value, + detail::conditional_t, + detail::conditional_t>; + // bool uses truthiness, so it has no range limit. + wide_t value = std::is_same::value ? static_cast(PyObject_IsTrue(m_ptr)) + : std::is_unsigned::value ? detail::as_unsigned(m_ptr) + : sizeof(T) <= sizeof(long) + ? static_cast(PyLong_AsLong(m_ptr)) + : static_cast(PYBIND11_LONG_AS_LONGLONG(m_ptr)); + if (value == static_cast(-1) && PyErr_Occurred()) { + throw error_already_set(); + } + if (static_cast(static_cast(value)) != value) { + PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C++ type"); + throw error_already_set(); + } + return static_cast(value); } }; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index afa344536f..4250344cc5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -186,6 +186,16 @@ TEST_SUBMODULE(pytypes, m) { m.def("get_bool", [] { return py::bool_(false); }); // test_int m.def("get_int", [] { return py::int_(0); }); + m.def("int_to_int", [](const py::int_ &o) { return static_cast(o); }); + m.def("int_to_long_long", [](const py::int_ &o) { return static_cast(o); }); + m.def("int_to_unsigned", [](const py::int_ &o) { return static_cast(o); }); + m.def("int_to_unsigned_long_long", + [](const py::int_ &o) { return static_cast(o); }); + // Copy-initialization: an explicit cast selects handle::operator bool (a null check). + m.def("int_to_bool", [](const py::int_ &o) { + bool result = o; + return result; + }); // test_iterator m.def("get_iterator", [] { return py::iterator(); }); // test_iterable diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 7e419b98e0..4995e644b5 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -30,6 +30,28 @@ def test_int(doc): assert doc(m.get_int) == "get_int() -> int" +@pytest.mark.parametrize( + ("func", "lo", "hi"), + [ + (m.int_to_int, -(2**31), 2**31 - 1), + (m.int_to_long_long, -(2**63), 2**63 - 1), + (m.int_to_unsigned, 0, 2**32 - 1), + (m.int_to_unsigned_long_long, 0, 2**64 - 1), + ], +) +def test_int_conversion_overflow(func, lo, hi): + assert func(lo) == lo + assert func(hi) == hi + for value in (lo - 1, hi + 1, 2**100, -(2**100)): + with pytest.raises(OverflowError): + func(value) + + +@pytest.mark.parametrize("value", [0, 1, -1, 2**64 - 1, 2**100, -(2**100)]) +def test_int_conversion_bool(value): + assert m.int_to_bool(value) is bool(value) + + def test_iterator(doc): assert doc(m.get_iterator) == "get_iterator() -> collections.abc.Iterator"