Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1902,9 +1902,26 @@ class int_ : public object {
template <typename T, detail::enable_if_t<std::is_integral<T>::value, int> = 0>
// NOLINTNEXTLINE(google-explicit-constructor)
operator T() const {
return std::is_unsigned<T>::value ? detail::as_unsigned<T>(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<T>::value,
detail::conditional_t<sizeof(T) <= sizeof(unsigned long),
unsigned long,
unsigned long long>,
detail::conditional_t<sizeof(T) <= sizeof(long), long, long long>>;
// bool uses truthiness, so it has no range limit.
wide_t value = std::is_same<T, bool>::value ? static_cast<wide_t>(PyObject_IsTrue(m_ptr))
: std::is_unsigned<T>::value ? detail::as_unsigned<wide_t>(m_ptr)
: sizeof(T) <= sizeof(long)
? static_cast<wide_t>(PyLong_AsLong(m_ptr))
: static_cast<wide_t>(PYBIND11_LONG_AS_LONGLONG(m_ptr));
if (value == static_cast<wide_t>(-1) && PyErr_Occurred()) {
throw error_already_set();
}
if (static_cast<wide_t>(static_cast<T>(value)) != value) {
PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C++ type");
throw error_already_set();
}
return static_cast<T>(value);
}
};

Expand Down
10 changes: 10 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(o); });
m.def("int_to_long_long", [](const py::int_ &o) { return static_cast<long long>(o); });
m.def("int_to_unsigned", [](const py::int_ &o) { return static_cast<unsigned>(o); });
m.def("int_to_unsigned_long_long",
[](const py::int_ &o) { return static_cast<unsigned long long>(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
Expand Down
22 changes: 22 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Loading