Skip to content

Commit

Permalink
added a wrapper type for weak references
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Sep 10, 2023
1 parent 2c56475 commit 36b9778
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/api_core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,6 @@ Wrapper classes

Return the size in bytes.


.. cpp:class:: type_object: public object

Wrapper class representing Python ``type`` instances.
Expand Down Expand Up @@ -1020,6 +1019,15 @@ Wrapper classes

Wrapper class representing a callable Python object.

.. cpp:class:: weakref: public object

Wrapper class representing a Python weak reference object.

.. cpp:function:: explicit weakref(handle obj, handle callback = { })

Construct a new weak reference that points to `obj`. If provided,
Python will invoke the callable `callback` when `obj` expires.

.. cpp:class:: args : public tuple

Variable argument keyword list for use in function argument declarations.
Expand Down
1 change: 1 addition & 0 deletions docs/exchanging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ directives:
:cpp:class:`iterator`, :cpp:class:`list`, :cpp:class:`mapping`,
:cpp:class:`module_`, :cpp:class:`object`, :cpp:class:`sequence`,
:cpp:class:`slice`, :cpp:class:`str`, :cpp:class:`tuple`,
:cpp:class:`weakref`,
:cpp:class:`type_object`, :cpp:class:`type_object_t\<T\> <handle_t>`,
:cpp:class:`args`, and :cpp:class:`kwargs`.

Expand Down
11 changes: 11 additions & 0 deletions include/nanobind/nb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,17 @@ class callable : public object {
using object::object;
};

class weakref : public object {
public:
NB_OBJECT(weakref, object, "weakref", PyWeakref_Check)

explicit weakref(handle obj, handle callback = {})
: object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), detail::steal_t{}) {
if (!m_ptr)
detail::raise_python_error();
}
};

template <typename T> class handle_t : public handle {
public:
static constexpr auto Name = detail::make_caster<T>::Name;
Expand Down

0 comments on commit 36b9778

Please sign in to comment.