Skip to content

Commit 618f7e4

Browse files
committed
Use the do-while 0 idiom for cursor guards macro
1 parent 3c89360 commit 618f7e4

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

psycopg/cursor.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,41 @@ HIDDEN int psyco_curs_scrollable_set(cursorObject *self, PyObject *pyvalue);
9797

9898
/* exception-raising macros */
9999
#define EXC_IF_CURS_CLOSED(self) \
100-
if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \
101-
PyErr_SetString(InterfaceError, "cursor already closed"); \
102-
return NULL; }
100+
do \
101+
if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \
102+
PyErr_SetString(InterfaceError, "cursor already closed"); \
103+
return NULL; } \
104+
while (0)
103105

104106
#define EXC_IF_NO_TUPLES(self) \
105-
if ((self)->notuples && (self)->name == NULL) { \
106-
PyErr_SetString(ProgrammingError, "no results to fetch"); \
107-
return NULL; }
107+
do \
108+
if ((self)->notuples && (self)->name == NULL) { \
109+
PyErr_SetString(ProgrammingError, "no results to fetch"); \
110+
return NULL; } \
111+
while (0)
108112

109113
#define EXC_IF_NO_MARK(self) \
110-
if ((self)->mark != (self)->conn->mark && (self)->withhold == 0) { \
111-
PyErr_SetString(ProgrammingError, "named cursor isn't valid anymore"); \
112-
return NULL; }
113-
114-
#define EXC_IF_CURS_ASYNC(self, cmd) if ((self)->conn->async == 1) { \
115-
PyErr_SetString(ProgrammingError, #cmd " cannot be used " \
116-
"in asynchronous mode"); \
117-
return NULL; }
114+
do \
115+
if ((self)->mark != (self)->conn->mark && (self)->withhold == 0) { \
116+
PyErr_SetString(ProgrammingError, "named cursor isn't valid anymore"); \
117+
return NULL; } \
118+
while (0)
119+
120+
#define EXC_IF_CURS_ASYNC(self, cmd) \
121+
do \
122+
if ((self)->conn->async == 1) { \
123+
PyErr_SetString(ProgrammingError, \
124+
#cmd " cannot be used in asynchronous mode"); \
125+
return NULL; } \
126+
while (0)
118127

119128
#define EXC_IF_ASYNC_IN_PROGRESS(self, cmd) \
120-
if ((self)->conn->async_cursor != NULL) { \
121-
PyErr_SetString(ProgrammingError, #cmd " cannot be used " \
122-
"while an asynchronous query is underway"); \
123-
return NULL; }
129+
do \
130+
if ((self)->conn->async_cursor != NULL) { \
131+
PyErr_SetString(ProgrammingError, \
132+
#cmd " cannot be used while an asynchronous query is underway"); \
133+
return NULL; } \
134+
while (0)
124135

125136
#ifdef __cplusplus
126137
}

psycopg/cursor_type.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ psyco_curs_scroll(cursorObject *self, PyObject *args, PyObject *kwargs)
11691169
char buffer[128];
11701170

11711171
EXC_IF_NO_MARK(self);
1172-
EXC_IF_ASYNC_IN_PROGRESS(self, scroll)
1172+
EXC_IF_ASYNC_IN_PROGRESS(self, scroll);
11731173
EXC_IF_TPC_PREPARED(self->conn, scroll);
11741174

11751175
if (strcmp(mode, "absolute") == 0) {

0 commit comments

Comments
 (0)