From 604bd3671121b51f977de146ed95484c2297fb3e Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 1 Nov 2019 11:09:52 +0100 Subject: PG_FINALLY This gives an alternative way of catching exceptions, for the common case where the cleanup code is the same in the error and non-error cases. So instead of PG_TRY(); { ... code that might throw ereport(ERROR) ... } PG_CATCH(); { cleanup(); PG_RE_THROW(); } PG_END_TRY(); cleanup(); one can write PG_TRY(); { ... code that might throw ereport(ERROR) ... } PG_FINALLY(); { cleanup(); } PG_END_TRY(); Discussion: https://www.postgresql.org/message-id/flat/95a822c3-728b-af0e-d7e5-71890507ae0c%402ndquadrant.com --- src/pl/plpython/plpy_cursorobject.c | 4 +--- src/pl/plpython/plpy_elog.c | 13 +------------ src/pl/plpython/plpy_exec.c | 12 ++---------- src/pl/plpython/plpy_spi.c | 4 +--- src/pl/plpython/plpy_typeio.c | 5 +---- 5 files changed, 6 insertions(+), 32 deletions(-) (limited to 'src/pl/plpython') diff --git a/src/pl/plpython/plpy_cursorobject.c b/src/pl/plpython/plpy_cursorobject.c index e4d543a4d4..b44ce7e225 100644 --- a/src/pl/plpython/plpy_cursorobject.c +++ b/src/pl/plpython/plpy_cursorobject.c @@ -228,13 +228,11 @@ PLy_cursor_plan(PyObject *ob, PyObject *args) plan->values[j] = PLy_output_convert(arg, elem, &isnull); nulls[j] = isnull ? 'n' : ' '; } - PG_CATCH(); + PG_FINALLY(); { Py_DECREF(elem); - PG_RE_THROW(); } PG_END_TRY(); - Py_DECREF(elem); } portal = SPI_cursor_open(NULL, plan->plan, plan->values, nulls, diff --git a/src/pl/plpython/plpy_elog.c b/src/pl/plpython/plpy_elog.c index 25930f99d7..15cc444af8 100644 --- a/src/pl/plpython/plpy_elog.c +++ b/src/pl/plpython/plpy_elog.c @@ -141,7 +141,7 @@ PLy_elog_impl(int elevel, const char *fmt,...) (constraint_name) ? err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0)); } - PG_CATCH(); + PG_FINALLY(); { if (fmt) pfree(emsg.data); @@ -151,19 +151,8 @@ PLy_elog_impl(int elevel, const char *fmt,...) pfree(tbmsg); Py_XDECREF(exc); Py_XDECREF(val); - - PG_RE_THROW(); } PG_END_TRY(); - - if (fmt) - pfree(emsg.data); - if (xmsg) - pfree(xmsg); - if (tbmsg) - pfree(tbmsg); - Py_XDECREF(exc); - Py_XDECREF(val); } /* diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index 920322e912..6994d7c10b 100644 --- a/src/pl/plpython/plpy_exec.c +++ b/src/pl/plpython/plpy_exec.c @@ -403,18 +403,13 @@ PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc) } } } - PG_CATCH(); + PG_FINALLY(); { Py_XDECREF(plargs); Py_XDECREF(plrv); - - PG_RE_THROW(); } PG_END_TRY(); - Py_DECREF(plargs); - Py_DECREF(plrv); - return rv; } @@ -1052,15 +1047,12 @@ PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs) */ Assert(list_length(explicit_subtransactions) >= save_subxact_level); } - PG_CATCH(); + PG_FINALLY(); { PLy_abort_open_subtransactions(save_subxact_level); - PG_RE_THROW(); } PG_END_TRY(); - PLy_abort_open_subtransactions(save_subxact_level); - /* If the Python code returned an error, propagate it */ if (rv == NULL) PLy_elog(ERROR, NULL); diff --git a/src/pl/plpython/plpy_spi.c b/src/pl/plpython/plpy_spi.c index 2fe435d42b..ed2eee0603 100644 --- a/src/pl/plpython/plpy_spi.c +++ b/src/pl/plpython/plpy_spi.c @@ -249,13 +249,11 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) plan->values[j] = PLy_output_convert(arg, elem, &isnull); nulls[j] = isnull ? 'n' : ' '; } - PG_CATCH(); + PG_FINALLY(); { Py_DECREF(elem); - PG_RE_THROW(); } PG_END_TRY(); - Py_DECREF(elem); } rv = SPI_execute_plan(plan->plan, plan->values, nulls, diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c index 371e534fd2..589c76e7a7 100644 --- a/src/pl/plpython/plpy_typeio.c +++ b/src/pl/plpython/plpy_typeio.c @@ -925,15 +925,12 @@ PLyObject_ToBytea(PLyObToDatum *arg, PyObject *plrv, memcpy(VARDATA(result), plrv_sc, len); rv = PointerGetDatum(result); } - PG_CATCH(); + PG_FINALLY(); { Py_XDECREF(plrv_so); - PG_RE_THROW(); } PG_END_TRY(); - Py_XDECREF(plrv_so); - return rv; } -- cgit v1.2.1