From f07b9b6380e4be0c3ed9c23f31d457ea16bf14a9 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 12 Nov 2026 20:58:25 -0500 Subject: [PATCH 1/2] fix: raise OverflowError in py::int_ integer conversion int_::operator T() ignored errors from PyLong_As* and truncated values that did not fit in types narrower than long. It now throws error_already_set on failure and raises OverflowError when the value is out of range for T. Fixes #2786. Assisted-by: ClaudeCode:claude-opus-5-5 --- include/pybind11/pytypes.h | 22 +++++++++++++++++++--- tests/test_pytypes.cpp | 5 +++++ tests/test_pytypes.py | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index adf512cc23..53e24410bc 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1902,9 +1902,25 @@ 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>; + wide_t value = 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(); + } + // bool keeps its truthiness semantics instead of a range check. + if (!std::is_same::value && 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..78455e401f 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -186,6 +186,11 @@ 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); }); // 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..0c9e5c9bf0 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -30,6 +30,23 @@ 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) + + def test_iterator(doc): assert doc(m.get_iterator) == "get_iterator() -> collections.abc.Iterator" From 167ce1d0d016b245dceeee4cdf7b799c2d9ce5f1 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 21 Nov 2026 13:25:03 -0500 Subject: [PATCH 2/2] fix: use truthiness for py::int_ conversion to bool The bool conversion read the value as unsigned long first, so negative and very large integers raised OverflowError. Assisted-by: ClaudeCode:claude-fable-5-2 --- include/pybind11/pytypes.h | 7 ++++--- tests/test_pytypes.cpp | 5 +++++ tests/test_pytypes.py | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 53e24410bc..e307aa3413 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1908,15 +1908,16 @@ class int_ : public object { unsigned long, unsigned long long>, detail::conditional_t>; - wide_t value = std::is_unsigned::value ? detail::as_unsigned(m_ptr) + // 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(); } - // bool keeps its truthiness semantics instead of a range check. - if (!std::is_same::value && static_cast(static_cast(value)) != value) { + if (static_cast(static_cast(value)) != value) { PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C++ type"); throw error_already_set(); } diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 78455e401f..4250344cc5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -191,6 +191,11 @@ TEST_SUBMODULE(pytypes, m) { 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 0c9e5c9bf0..4995e644b5 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -47,6 +47,11 @@ def test_int_conversion_overflow(func, lo, hi): 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"