From 69b2fa282cc53666a3c029827147ee7dff2cdc80 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 26 Feb 2014 19:31:27 +0000 Subject: [PATCH] Use the do-while 0 idiom for cursor guards macro --- psycopg/cursor.h | 45 +++++++++++++++++++++++++++---------------- psycopg/cursor_type.c | 2 +- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/psycopg/cursor.h b/psycopg/cursor.h index 7940e7b4..f96c657c 100644 --- a/psycopg/cursor.h +++ b/psycopg/cursor.h @@ -97,30 +97,41 @@ HIDDEN int psyco_curs_scrollable_set(cursorObject *self, PyObject *pyvalue); /* exception-raising macros */ #define EXC_IF_CURS_CLOSED(self) \ -if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \ - PyErr_SetString(InterfaceError, "cursor already closed"); \ - return NULL; } +do \ + if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \ + PyErr_SetString(InterfaceError, "cursor already closed"); \ + return NULL; } \ +while (0) #define EXC_IF_NO_TUPLES(self) \ -if ((self)->notuples && (self)->name == NULL) { \ - PyErr_SetString(ProgrammingError, "no results to fetch"); \ - return NULL; } +do \ + if ((self)->notuples && (self)->name == NULL) { \ + PyErr_SetString(ProgrammingError, "no results to fetch"); \ + return NULL; } \ +while (0) #define EXC_IF_NO_MARK(self) \ -if ((self)->mark != (self)->conn->mark && (self)->withhold == 0) { \ - PyErr_SetString(ProgrammingError, "named cursor isn't valid anymore"); \ - return NULL; } +do \ + if ((self)->mark != (self)->conn->mark && (self)->withhold == 0) { \ + PyErr_SetString(ProgrammingError, "named cursor isn't valid anymore"); \ + return NULL; } \ +while (0) -#define EXC_IF_CURS_ASYNC(self, cmd) if ((self)->conn->async == 1) { \ - PyErr_SetString(ProgrammingError, #cmd " cannot be used " \ - "in asynchronous mode"); \ - return NULL; } +#define EXC_IF_CURS_ASYNC(self, cmd) \ +do \ + if ((self)->conn->async == 1) { \ + PyErr_SetString(ProgrammingError, \ + #cmd " cannot be used in asynchronous mode"); \ + return NULL; } \ +while (0) #define EXC_IF_ASYNC_IN_PROGRESS(self, cmd) \ -if ((self)->conn->async_cursor != NULL) { \ - PyErr_SetString(ProgrammingError, #cmd " cannot be used " \ - "while an asynchronous query is underway"); \ - return NULL; } +do \ + if ((self)->conn->async_cursor != NULL) { \ + PyErr_SetString(ProgrammingError, \ + #cmd " cannot be used while an asynchronous query is underway"); \ + return NULL; } \ +while (0) #ifdef __cplusplus } diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 0c2acbe2..877e3390 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -1169,7 +1169,7 @@ psyco_curs_scroll(cursorObject *self, PyObject *args, PyObject *kwargs) char buffer[128]; EXC_IF_NO_MARK(self); - EXC_IF_ASYNC_IN_PROGRESS(self, scroll) + EXC_IF_ASYNC_IN_PROGRESS(self, scroll); EXC_IF_TPC_PREPARED(self->conn, scroll); if (strcmp(mode, "absolute") == 0) {