Skip to content

Commit 6b59d1e

Browse files
Add method to py_ref_t to copy the current reference.
1 parent a8c64c0 commit 6b59d1e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

dynd/include/utility_functions.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ class py_ref_t {
404404
return py_ref_t<true, not_null>(o, false);
405405
}
406406

407+
// Copy the current reference.
408+
py_ref_t<owns_ref, not_null> copy() noexcept
409+
{
410+
// Assert that the wrapped pointer is not null if it shouldn't be.
411+
PYDYND_ASSERT_IF(not_null, o != nullptr);
412+
// Assert that the wrapped reference is actually valid if it isn't null.
413+
PYDYND_ASSERT_IF(o != nullptr, Py_REFCNT(o) > 0);
414+
return py_ref_t<owns_ref, not_null>(o, false);
415+
}
416+
407417
// Return a reference to the encapsulated PyObject as a raw pointer.
408418
// Set the encapsulated pointer to NULL.
409419
// If this is a type that owns its reference, an owned reference is returned.

0 commit comments

Comments
 (0)