Skip to content

Commit

Permalink
Ensure that nb::int_(float/double) creates an integer (fixes #794)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Dec 17, 2024
1 parent 20a367a commit 5eb205c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions include/nanobind/nb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,12 @@ class int_ : public object {
: object(detail::int_from_obj(h.ptr()), detail::steal_t{}) { }

template <typename T, detail::enable_if_t<std::is_arithmetic_v<T>> = 0>
explicit int_(T value)
: object(
detail::type_caster<T>::from_cpp(value, rv_policy::copy, nullptr),
detail::steal_t{}) {
explicit int_(T value) {
if constexpr (std::is_floating_point_v<T>)
m_ptr = PyLong_FromDouble((double) value);
else
m_ptr = detail::type_caster<T>::from_cpp(value, rv_policy::copy, nullptr).ptr();

if (!m_ptr)
raise_python_error();
}
Expand Down

0 comments on commit 5eb205c

Please sign in to comment.