Skip to content

Commit ccbe68b

Browse files
committed
added binding delattr() -> PyObject_DelAttr analogous to hasattr()
1 parent d1f64fa commit ccbe68b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

include/pybind11/pytypes.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ inline bool hasattr(handle obj, const char *name) {
388388
return PyObject_HasAttrString(obj.ptr(), name) == 1;
389389
}
390390

391+
inline void delattr(handle obj, handle name) {
392+
if (PyObject_DelAttr(obj.ptr(), name.ptr()) != 0) { throw error_already_set(); }
393+
}
394+
395+
inline void delattr(handle obj, const char *name) {
396+
if (PyObject_DelAttrString(obj.ptr(), name) != 0) { throw error_already_set(); }
397+
}
398+
391399
inline object getattr(handle obj, handle name) {
392400
PyObject *result = PyObject_GetAttr(obj.ptr(), name.ptr());
393401
if (!result) { throw error_already_set(); }
@@ -459,7 +467,6 @@ object object_or_cast(T &&o);
459467
// Match a PyObject*, which we want to convert directly to handle via its converting constructor
460468
inline handle object_or_cast(PyObject *ptr) { return ptr; }
461469

462-
463470
template <typename Policy>
464471
class accessor : public object_api<accessor<Policy>> {
465472
using key_type = typename Policy::key_type;

0 commit comments

Comments
 (0)