From 45318ac2e799284b50c41d0ccc092a625186213c Mon Sep 17 00:00:00 2001 From: igo95862 Date: Sat, 3 Apr 2021 17:37:33 +0300 Subject: [PATCH] python: Add special macros for error handling Can only be used on clang or gcc but I think the library already requires them. --- python/notcurses/notcurses-python.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/notcurses/notcurses-python.h b/python/notcurses/notcurses-python.h index a64d18706..b5454a286 100644 --- a/python/notcurses/notcurses-python.h +++ b/python/notcurses/notcurses-python.h @@ -135,3 +135,20 @@ typedef struct PyObject_HEAD; struct palette256 palette256; } Palette256Object; + +static inline void PyObject_cleanup(PyObject **object) +{ + Py_XDECREF(*object); +} + +#define CLEANUP_PY_OBJ __attribute__((cleanup(PyObject_cleanup))) + +#define PY_CHECK(py_function) \ + ({ \ + PyObject *new_object = py_function; \ + if (new_object == NULL) \ + { \ + return NULL; \ + } \ + new_object; \ + })