Skip to content

Commit 62bbb5c

Browse files
author
Erlend E. Aasland
committed
bpo-42972: Fix sqlite3 traverse/clear
1 parent af5a324 commit 62bbb5c

File tree

6 files changed

+44
-41
lines changed

6 files changed

+44
-41
lines changed

Modules/_sqlite/cache.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pysqlite_new_node(PyObject *key, PyObject *data)
4747
static int
4848
node_traverse(pysqlite_Node *self, visitproc visit, void *arg)
4949
{
50+
Py_VISIT(Py_TYPE(self));
5051
Py_VISIT(self->key);
5152
Py_VISIT(self->data);
52-
Py_VISIT(Py_TYPE(self));
5353
return 0;
5454
}
5555

@@ -106,22 +106,28 @@ pysqlite_cache_init(pysqlite_Cache *self, PyObject *args, PyObject *kwargs)
106106
static int
107107
cache_traverse(pysqlite_Cache *self, visitproc visit, void *arg)
108108
{
109+
Py_VISIT(Py_TYPE(self));
110+
Py_VISIT(self->mapping);
111+
if (self->decref_factory) {
112+
Py_VISIT(self->factory);
113+
}
114+
109115
pysqlite_Node *node = self->first;
110116
while (node) {
111117
Py_VISIT(node);
112118
node = node->next;
113119
}
114-
Py_VISIT(self->mapping);
115-
if (self->decref_factory) {
116-
Py_VISIT(self->factory);
117-
}
118-
Py_VISIT(Py_TYPE(self));
119120
return 0;
120121
}
121122

122123
static int
123124
cache_clear(pysqlite_Cache *self)
124125
{
126+
Py_CLEAR(self->mapping);
127+
if (self->decref_factory) {
128+
Py_CLEAR(self->factory);
129+
}
130+
125131
/* iterate over all nodes and deallocate them */
126132
pysqlite_Node *node = self->first;
127133
self->first = NULL;
@@ -130,10 +136,6 @@ cache_clear(pysqlite_Cache *self)
130136
node = node->next;
131137
Py_CLEAR(delete_node);
132138
}
133-
if (self->decref_factory) {
134-
Py_CLEAR(self->factory);
135-
}
136-
Py_CLEAR(self->mapping);
137139
return 0;
138140
}
139141

Modules/_sqlite/connection.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -228,33 +228,33 @@ pysqlite_do_all_statements(pysqlite_Connection *self, int action,
228228
static int
229229
connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
230230
{
231-
Py_VISIT(self->statement_cache);
231+
Py_VISIT(Py_TYPE(self));
232232
Py_VISIT(self->isolation_level);
233+
Py_VISIT(self->statement_cache);
234+
Py_VISIT(self->statements);
235+
Py_VISIT(self->cursors);
236+
Py_VISIT(self->row_factory);
237+
Py_VISIT(self->text_factory);
233238
Py_VISIT(self->function_pinboard_trace_callback);
234239
Py_VISIT(self->function_pinboard_progress_handler);
235240
Py_VISIT(self->function_pinboard_authorizer_cb);
236-
Py_VISIT(self->row_factory);
237-
Py_VISIT(self->text_factory);
238241
Py_VISIT(self->collations);
239-
Py_VISIT(self->statements);
240-
Py_VISIT(self->cursors);
241-
Py_VISIT(Py_TYPE(self));
242242
return 0;
243243
}
244244

245245
static int
246246
connection_clear(pysqlite_Connection *self)
247247
{
248-
Py_CLEAR(self->statement_cache);
249248
Py_CLEAR(self->isolation_level);
249+
Py_CLEAR(self->statement_cache);
250+
Py_CLEAR(self->statements);
251+
Py_CLEAR(self->cursors);
252+
Py_CLEAR(self->row_factory);
253+
Py_CLEAR(self->text_factory);
250254
Py_CLEAR(self->function_pinboard_trace_callback);
251255
Py_CLEAR(self->function_pinboard_progress_handler);
252256
Py_CLEAR(self->function_pinboard_authorizer_cb);
253-
Py_CLEAR(self->row_factory);
254-
Py_CLEAR(self->text_factory);
255257
Py_CLEAR(self->collations);
256-
Py_CLEAR(self->statements);
257-
Py_CLEAR(self->cursors);
258258
return 0;
259259
}
260260

Modules/_sqlite/connection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ typedef struct
9393
/* a dictionary of registered collation name => collation callable mappings */
9494
PyObject* collations;
9595

96-
/* Exception objects */
96+
/* Exception objects: borrowed refs. */
9797
PyObject* Warning;
9898
PyObject* Error;
9999
PyObject* InterfaceError;

Modules/_sqlite/cursor.c

+15-14
Original file line numberDiff line numberDiff line change
@@ -84,43 +84,44 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
8484
static int
8585
cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
8686
{
87+
Py_VISIT(Py_TYPE(self));
8788
Py_VISIT(self->connection);
89+
Py_VISIT(self->description);
8890
Py_VISIT(self->row_cast_map);
91+
Py_VISIT(self->lastrowid);
8992
Py_VISIT(self->row_factory);
93+
Py_VISIT(self->statement);
9094
Py_VISIT(self->next_row);
91-
Py_VISIT(Py_TYPE(self));
9295
return 0;
9396
}
9497

9598
static int
9699
cursor_clear(pysqlite_Cursor *self)
97100
{
98-
/* Reset the statement if the user has not closed the cursor */
99-
if (self->statement) {
100-
pysqlite_statement_reset(self->statement);
101-
Py_CLEAR(self->statement);
102-
}
103-
104101
Py_CLEAR(self->connection);
105-
Py_CLEAR(self->row_cast_map);
106102
Py_CLEAR(self->description);
103+
Py_CLEAR(self->row_cast_map);
107104
Py_CLEAR(self->lastrowid);
108105
Py_CLEAR(self->row_factory);
109-
Py_CLEAR(self->next_row);
110-
111-
if (self->in_weakreflist != NULL) {
112-
PyObject_ClearWeakRefs((PyObject*)self);
106+
if (self->statement) {
107+
/* Reset the statement if the user has not closed the cursor */
108+
pysqlite_statement_reset(self->statement);
109+
Py_CLEAR(self->statement);
113110
}
111+
Py_CLEAR(self->next_row);
114112

115113
return 0;
116114
}
117115

118116
static void
119-
cursor_dealloc(PyObject *self)
117+
cursor_dealloc(pysqlite_Cursor *self)
120118
{
121119
PyTypeObject *tp = Py_TYPE(self);
122120
PyObject_GC_UnTrack(self);
123-
tp->tp_clear(self);
121+
if (self->in_weakreflist != NULL) {
122+
PyObject_ClearWeakRefs((PyObject*)self);
123+
}
124+
tp->tp_clear((PyObject *)self);
124125
tp->tp_free(self);
125126
Py_DECREF(tp);
126127
}

Modules/_sqlite/row.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ row_clear(pysqlite_Row *self)
4242
static int
4343
row_traverse(pysqlite_Row *self, visitproc visit, void *arg)
4444
{
45+
Py_VISIT(Py_TYPE(self));
4546
Py_VISIT(self->data);
4647
Py_VISIT(self->description);
47-
Py_VISIT(Py_TYPE(self));
4848
return 0;
4949
}
5050

Modules/_sqlite/statement.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ stmt_dealloc(pysqlite_Statement *self)
373373
{
374374
PyTypeObject *tp = Py_TYPE(self);
375375
PyObject_GC_UnTrack(self);
376+
if (self->in_weakreflist != NULL) {
377+
PyObject_ClearWeakRefs((PyObject*)self);
378+
}
376379
tp->tp_clear((PyObject *)self);
377380
tp->tp_free(self);
378381
Py_DECREF(tp);
@@ -389,17 +392,14 @@ stmt_clear(pysqlite_Statement *self)
389392
}
390393

391394
Py_CLEAR(self->sql);
392-
if (self->in_weakreflist != NULL) {
393-
PyObject_ClearWeakRefs((PyObject*)self);
394-
}
395395
return 0;
396396
}
397397

398398
static int
399399
stmt_traverse(pysqlite_Statement *self, visitproc visit, void *arg)
400400
{
401-
Py_VISIT(self->sql);
402401
Py_VISIT(Py_TYPE(self));
402+
Py_VISIT(self->sql);
403403
return 0;
404404
}
405405

0 commit comments

Comments
 (0)