From 5eb205c134fad47052d96a8d3cc84458c4f7b3b2 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Tue, 17 Dec 2024 22:58:46 +0900 Subject: [PATCH] Ensure that ``nb::int_(float/double)`` creates an integer (fixes #794) --- include/nanobind/nb_types.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/nanobind/nb_types.h b/include/nanobind/nb_types.h index 1a8c35e4..f384e621 100644 --- a/include/nanobind/nb_types.h +++ b/include/nanobind/nb_types.h @@ -371,10 +371,12 @@ class int_ : public object { : object(detail::int_from_obj(h.ptr()), detail::steal_t{}) { } template > = 0> - explicit int_(T value) - : object( - detail::type_caster::from_cpp(value, rv_policy::copy, nullptr), - detail::steal_t{}) { + explicit int_(T value) { + if constexpr (std::is_floating_point_v) + m_ptr = PyLong_FromDouble((double) value); + else + m_ptr = detail::type_caster::from_cpp(value, rv_policy::copy, nullptr).ptr(); + if (!m_ptr) raise_python_error(); }