Skip to content

Commit d8ddcaf

Browse files
Add convenience function for null-checking a smart pointer wrapping a
PyObject*.
1 parent 0737f65 commit d8ddcaf

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

dynd/include/utility_functions.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,19 @@ inline py_ref capture_if_not_null(PyObject *o)
388388
// return py_ref(o, true);
389389
}
390390

391+
/* Convert to a non-null reference.
392+
* If the input type allows nulls, explicitly check for null and raise an exception.
393+
* If the input type does not allow null, this function is marked as noexcept and
394+
* only checks for via an assert statement in debug builds.
395+
*/
396+
template <bool owns_ref, bool not_null>
397+
inline py_ref_tmpl<owns_ref, true> nullcheck(py_ref_tmpl<owns_ref, not_null> &&obj) noexcept(not_null)
398+
{
399+
// Route this through the assignment operator since the semantics are the same.
400+
py_ref_tmpl<owns_ref, true> out = obj;
401+
return out;
402+
}
403+
391404
// RAII class to acquire GIL.
392405
class with_gil {
393406
PyGILState_STATE m_gstate;

0 commit comments

Comments
 (0)