From 5f580b28c6d5921898775f5c38cb59853dbc9ffe Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 20 Jun 2020 12:33:39 +0200 Subject: [PATCH 09/21] python: add a failure label to method wrappers (#44287) To separate wrapping code from unwinding and error handling code. --- bindings/python/lang.py | 17 ++++++++++++++--- bindings/python/wrapper_top.c | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bindings/python/lang.py b/bindings/python/lang.py index cb9d0b4d..b9d86f45 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -908,7 +908,8 @@ register_constants(PyObject *d) self.wrapper_list.append(name) print_('''static PyObject* %s(G_GNUC_UNUSED PyObject *self, PyObject *args) -{''' % name, file=fd) +{ + int ok = 1;''' % name, file=fd) parse_tuple_format = [] parse_tuple_args = [] for arg in m.args: @@ -1034,6 +1035,7 @@ register_constants(PyObject *d) if is_transfer_full(m.return_arg, default=True): self.free_value(fd, m.return_arg, name = 'return_value') + for f, arg in zip(parse_tuple_format, m.args): if is_out(arg): self.return_value(fd, arg, return_var_name = arg[1], return_pyvar_name = 'out_pyvalue') @@ -1053,10 +1055,19 @@ register_constants(PyObject *d) elif not is_transfer_full(arg) and is_xml_node(arg): self.free_value(fd, arg) + print_('failure:', file=fd) + if not m.return_type: - print_(' return noneRef();', file=fd) + print_(' if (ok) {', file=fd) + print_(' return noneRef();', file=fd) else: - print_(' return return_pyvalue;', file=fd) + print_(' if (ok && return_pyvalue) {', file=fd) + print_(' return return_pyvalue;', file=fd) + print_(' } else {', file=fd) + if m.return_type: + print_(' Py_XDECREF(return_pyvalue);', file=fd) + print_(' return NULL;', file=fd) + print_(' }', file=fd) print_('}', file=fd) print_('', file=fd) diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index 2867a893..918e8ea7 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -74,6 +74,7 @@ void PyErr_WarnFormat(PyObject *category, int stacklevel, const char *format, .. #endif #define RETURN_IF_FAIL(op) do { if (! (op)) { return NULL; } } while(0) +#define EXIT_IF_FAIL(op) do { if (! (op)) { ok = 0; goto failure; } } while(0) GQuark lasso_wrapper_key; -- 2.26.2