Skip to content

Commit

Permalink
Remove noexcept flag from api<T>::inc_ref/dec_ref()
Browse files Browse the repository at this point in the history
That's because they call ``operator handle()``, which may raise an
exception.
  • Loading branch information
wjakob committed Oct 11, 2023
1 parent 5f25ae0 commit 249adc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 12 additions & 2 deletions docs/api_core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ following mixin class that lives in the ``nanobind::detail`` namespace.

Obtain a const reference to the derived class.

.. cpp:function:: handle inc_ref() const noexcept
.. cpp:function:: handle inc_ref() const

Increases the reference count and returns a reference to the Python object.

.. cpp:function:: handle ref_ref() const noexcept
.. cpp:function:: handle ref_ref() const

Decreases the reference count and returns a reference to the Python object.

Expand Down Expand Up @@ -354,6 +354,16 @@ Without reference counting
Check if the handle refers to a valid Python object. Equivalent to
:cpp:func:`detail::api::is_valid()`

.. cpp:function:: handle inc_ref() const noexcept

Increases the reference count and returns a reference to the Python object.
Never raises an exception.

.. cpp:function:: handle ref_ref() const noexcept

Decreases the reference count and returns a reference to the Python object.
Never raises an exception.

.. cpp:function:: PyObject * ptr() const

Return the underlying ``PyObject*`` pointer.
Expand Down
11 changes: 7 additions & 4 deletions include/nanobind/nb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ template <typename Derived> class api : public api_tag {
NB_INLINE bool is_none() const { return derived().ptr() == Py_None; }
NB_INLINE bool is_type() const { return PyType_Check(derived().ptr()); }
NB_INLINE bool is_valid() const { return derived().ptr() != nullptr; }
NB_INLINE handle inc_ref() const & noexcept;
NB_INLINE handle dec_ref() const & noexcept;
NB_INLINE handle inc_ref() const &;
NB_INLINE handle dec_ref() const &;
iterator begin() const;
iterator end() const;

Expand Down Expand Up @@ -680,11 +680,14 @@ template <typename Derived> NB_INLINE handle api<Derived>::type() const {
return (PyObject *) Py_TYPE(derived().ptr());
}

template <typename Derived>
NB_INLINE handle api<Derived>::inc_ref() const &noexcept {
template <typename Derived> NB_INLINE handle api<Derived>::inc_ref() const & {
return operator handle().inc_ref();
}

template <typename Derived> NB_INLINE handle api<Derived>::dec_ref() const & {
return operator handle().dec_ref();
}

template <typename Derived>
NB_INLINE bool api<Derived>::is(handle value) const {
return derived().ptr() == value.ptr();
Expand Down

0 comments on commit 249adc0

Please sign in to comment.