Skip to content

Commit 2273b79

Browse files
committed
Use Py_CLEAR() in a few more places, and do INCREF's before setting
struct members rather than afterwards.
1 parent e0287c0 commit 2273b79

File tree

8 files changed

+24
-24
lines changed

8 files changed

+24
-24
lines changed

psycopg/adapter_asis.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ asis_setup(asisObject *self, PyObject *obj)
9696
self, ((PyObject *)self)->ob_refcnt
9797
);
9898

99+
Py_INCREF(obj);
99100
self->wrapped = obj;
100-
Py_INCREF(self->wrapped);
101101

102102
Dprintf("asis_setup: good asis object at %p, refcnt = "
103103
FORMAT_CODE_PY_SSIZE_T,
@@ -118,7 +118,7 @@ asis_dealloc(PyObject* obj)
118118
{
119119
asisObject *self = (asisObject *)obj;
120120

121-
Py_XDECREF(self->wrapped);
121+
Py_CLEAR(self->wrapped);
122122

123123
Dprintf("asis_dealloc: deleted asis object at %p, refcnt = "
124124
FORMAT_CODE_PY_SSIZE_T,

psycopg/adapter_binary.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ binary_setup(binaryObject *self, PyObject *str)
257257

258258
self->buffer = NULL;
259259
self->conn = NULL;
260+
Py_INCREF(str);
260261
self->wrapped = str;
261-
Py_INCREF(self->wrapped);
262262

263263
Dprintf("binary_setup: good binary object at %p, refcnt = "
264264
FORMAT_CODE_PY_SSIZE_T,
@@ -282,9 +282,9 @@ binary_dealloc(PyObject* obj)
282282
{
283283
binaryObject *self = (binaryObject *)obj;
284284

285-
Py_XDECREF(self->wrapped);
286-
Py_XDECREF(self->buffer);
287-
Py_XDECREF(self->conn);
285+
Py_CLEAR(self->wrapped);
286+
Py_CLEAR(self->buffer);
287+
Py_CLEAR(self->conn);
288288

289289
Dprintf("binary_dealloc: deleted binary object at %p, refcnt = "
290290
FORMAT_CODE_PY_SSIZE_T,

psycopg/adapter_datetime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ pydatetime_setup(pydatetimeObject *self, PyObject *obj, int type)
131131
self, ((PyObject *)self)->ob_refcnt);
132132

133133
self->type = type;
134+
Py_INCREF(obj);
134135
self->wrapped = obj;
135-
Py_INCREF(self->wrapped);
136136

137137
Dprintf("pydatetime_setup: good pydatetime object at %p, refcnt = "
138138
FORMAT_CODE_PY_SSIZE_T,
@@ -154,7 +154,7 @@ pydatetime_dealloc(PyObject* obj)
154154
{
155155
pydatetimeObject *self = (pydatetimeObject *)obj;
156156

157-
Py_XDECREF(self->wrapped);
157+
Py_CLEAR(self->wrapped);
158158

159159
Dprintf("mpydatetime_dealloc: deleted pydatetime object at %p, "
160160
"refcnt = " FORMAT_CODE_PY_SSIZE_T, obj, obj->ob_refcnt);

psycopg/adapter_list.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ list_prepare(listObject *self, PyObject *args)
108108
reference to it; we'll need it during the recursive adapt() call (the
109109
encoding is here for a future expansion that will make .getquoted()
110110
work even without a connection to the backend. */
111-
Py_XDECREF(self->connection);
111+
Py_CLEAR(self->connection);
112+
Py_INCREF(conn);
112113
self->connection = (PyObject*)conn;
113-
Py_INCREF(self->connection);
114114

115115
Py_INCREF(Py_None);
116116
return Py_None;
@@ -169,8 +169,8 @@ list_setup(listObject *self, PyObject *obj, const char *enc)
169169
if (enc) self->encoding = strdup(enc);
170170

171171
self->connection = NULL;
172+
Py_INCREF(obj);
172173
self->wrapped = obj;
173-
Py_INCREF(self->wrapped);
174174

175175
Dprintf("list_setup: good list object at %p, refcnt = "
176176
FORMAT_CODE_PY_SSIZE_T,
@@ -194,8 +194,8 @@ list_dealloc(PyObject* obj)
194194
{
195195
listObject *self = (listObject *)obj;
196196

197-
Py_XDECREF(self->wrapped);
198-
Py_XDECREF(self->connection);
197+
Py_CLEAR(self->wrapped);
198+
Py_CLEAR(self->connection);
199199
if (self->encoding) free(self->encoding);
200200

201201
Dprintf("list_dealloc: deleted list object at %p, "

psycopg/adapter_mxdatetime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ mxdatetime_setup(mxdatetimeObject *self, PyObject *obj, int type)
152152
);
153153

154154
self->type = type;
155+
Py_INCREF(obj);
155156
self->wrapped = obj;
156-
Py_INCREF(self->wrapped);
157157

158158
Dprintf("mxdatetime_setup: good mxdatetime object at %p, refcnt = "
159159
FORMAT_CODE_PY_SSIZE_T,
@@ -176,7 +176,7 @@ mxdatetime_dealloc(PyObject* obj)
176176
{
177177
mxdatetimeObject *self = (mxdatetimeObject *)obj;
178178

179-
Py_XDECREF(self->wrapped);
179+
Py_CLEAR(self->wrapped);
180180

181181
Dprintf("mxdatetime_dealloc: deleted mxdatetime object at %p, refcnt = "
182182
FORMAT_CODE_PY_SSIZE_T,

psycopg/adapter_pboolean.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ pboolean_setup(pbooleanObject *self, PyObject *obj)
106106
self, ((PyObject *)self)->ob_refcnt
107107
);
108108

109+
Py_INCREF(obj);
109110
self->wrapped = obj;
110-
Py_INCREF(self->wrapped);
111111

112112
Dprintf("pboolean_setup: good pboolean object at %p, refcnt = "
113113
FORMAT_CODE_PY_SSIZE_T,
@@ -130,7 +130,7 @@ pboolean_dealloc(PyObject* obj)
130130
{
131131
pbooleanObject *self = (pbooleanObject *)obj;
132132

133-
Py_XDECREF(self->wrapped);
133+
Py_CLEAR(self->wrapped);
134134

135135
Dprintf("pboolean_dealloc: deleted pboolean object at %p, refcnt = "
136136
FORMAT_CODE_PY_SSIZE_T,

psycopg/adapter_qstring.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ qstring_prepare(qstringObject *self, PyObject *args)
216216
Dprintf("qstring_prepare: set encoding to %s", conn->encoding);
217217
}
218218

219-
Py_XDECREF(self->conn);
219+
Py_CLEAR(self->conn);
220220
if (conn) {
221+
Py_INCREF(conn);
221222
self->conn = (PyObject*)conn;
222-
Py_INCREF(self->conn);
223223
}
224224

225225
Py_INCREF(Py_None);
@@ -280,8 +280,8 @@ qstring_setup(qstringObject *self, PyObject *str, const char *enc)
280280
/* FIXME: remove this orrible strdup */
281281
if (enc) self->encoding = strdup(enc);
282282

283+
Py_INCREF(str);
283284
self->wrapped = str;
284-
Py_INCREF(self->wrapped);
285285

286286
Dprintf("qstring_setup: good qstring object at %p, refcnt = "
287287
FORMAT_CODE_PY_SSIZE_T,
@@ -306,9 +306,9 @@ qstring_dealloc(PyObject* obj)
306306
{
307307
qstringObject *self = (qstringObject *)obj;
308308

309-
Py_XDECREF(self->wrapped);
310-
Py_XDECREF(self->buffer);
311-
Py_XDECREF(self->conn);
309+
Py_CLEAR(self->wrapped);
310+
Py_CLEAR(self->buffer);
311+
Py_CLEAR(self->conn);
312312

313313
if (self->encoding) free(self->encoding);
314314

psycopg/cursor_type.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ cursor_repr(cursorObject *self)
16881688
static int
16891689
cursor_traverse(cursorObject *self, visitproc visit, void *arg)
16901690
{
1691-
Py_VISIT(self->conn);
1691+
Py_VISIT((PyObject *)self->conn);
16921692
Py_VISIT(self->description);
16931693
Py_VISIT(self->pgstatus);
16941694
Py_VISIT(self->casts);

0 commit comments

Comments
 (0)