Mercurial > hgrepos > Python > libs > ConfigMix
diff configmix/_py_helper.h @ 560:81238ea2dbe3
Implement and use more helper functions.
Improve comments.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 06 Jan 2022 19:37:03 +0100 |
| parents | bb160a1e67d7 |
| children | f75c5b13a1d7 |
line wrap: on
line diff
--- a/configmix/_py_helper.h Thu Jan 06 18:50:09 2022 +0100 +++ b/configmix/_py_helper.h Thu Jan 06 19:37:03 2022 +0100 @@ -13,6 +13,9 @@ #if PY_VERSION_HEX < 0x030A0000 +/* + * Return a new owned reference to an object + */ static inline PyObject * Py_NewRef(PyObject *obj) @@ -22,6 +25,9 @@ } +/* + * Return a new owned reference to an object when the input can be NULL + */ static inline PyObject * Py_XNewRef(PyObject *obj) @@ -67,4 +73,74 @@ return (obj1 != obj2); } + +/** + * Copy from source to destination and make an owned reference. + * Also safely clear the destination before. + */ +static inline +void +py_assign(PyObject **dest, PyObject *src) +{ + Py_XDECREF(*dest); + *dest = Py_NewRef(src); +} + + +/** + * Copy from source to destination and make an owned reference. + * Also safely clear the destination before. The source object may be NULL. + */ +static inline +void +py_assign_x(PyObject **dest, PyObject *src) +{ + Py_XDECREF(*dest); + *dest = Py_XNewRef(src); +} + + +/* + * Transfer from a borrowed reference to an owned one and clear the source. + * Also safely clear the destination before. + */ +static inline +void +py_transfer_borrowed(PyObject **dest, PyObject **src) +{ + Py_XDECREF(*dest); + *dest = Py_NewRef(*src); + *src = NULL; +} + + +/* + * Transfer from a borrowed reference to an owned one and clear the source. + * Also safely clear the destination before. The source object may be NULL. + */ +static inline +void +py_transfer_x_borrowed(PyObject **dest, PyObject **src) +{ + Py_XDECREF(*dest); + *dest = Py_XNewRef(*src); + *src = NULL; +} + + +/* + * Transfer ownership from a owned reference to an owned one and clear the + * source. + * Also safely clear the destination before. + */ +static inline +void +py_transfer_owned(PyObject **dest, PyObject **src) +{ + Py_XDECREF(*dest); + *dest = *src; + *src = NULL; +} + + #endif
