Skip to content

Commit

Permalink
bpo-42972: Fix sqlite3 traverse/clear functions (GH-26452) (GH-26461)
Browse files Browse the repository at this point in the history
(cherry picked from commit d1124b0)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
  • Loading branch information
miss-islington and Erlend Egeberg Aasland committed May 31, 2021
1 parent d338ce0 commit ff359d7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
22 changes: 12 additions & 10 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pysqlite_new_node(PyObject *key, PyObject *data)
static int
node_traverse(pysqlite_Node *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->key);
Py_VISIT(self->data);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand Down Expand Up @@ -106,22 +106,28 @@ pysqlite_cache_init(pysqlite_Cache *self, PyObject *args, PyObject *kwargs)
static int
cache_traverse(pysqlite_Cache *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->mapping);
if (self->decref_factory) {
Py_VISIT(self->factory);
}

pysqlite_Node *node = self->first;
while (node) {
Py_VISIT(node);
node = node->next;
}
Py_VISIT(self->mapping);
if (self->decref_factory) {
Py_VISIT(self->factory);
}
Py_VISIT(Py_TYPE(self));
return 0;
}

static int
cache_clear(pysqlite_Cache *self)
{
Py_CLEAR(self->mapping);
if (self->decref_factory) {
Py_CLEAR(self->factory);
}

/* iterate over all nodes and deallocate them */
pysqlite_Node *node = self->first;
self->first = NULL;
Expand All @@ -130,10 +136,6 @@ cache_clear(pysqlite_Cache *self)
node = node->next;
Py_CLEAR(delete_node);
}
if (self->decref_factory) {
Py_CLEAR(self->factory);
}
Py_CLEAR(self->mapping);
return 0;
}

Expand Down
22 changes: 11 additions & 11 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,33 +228,33 @@ pysqlite_do_all_statements(pysqlite_Connection *self, int action,
static int
connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
{
Py_VISIT(self->statement_cache);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->isolation_level);
Py_VISIT(self->statement_cache);
Py_VISIT(self->statements);
Py_VISIT(self->cursors);
Py_VISIT(self->row_factory);
Py_VISIT(self->text_factory);
Py_VISIT(self->function_pinboard_trace_callback);
Py_VISIT(self->function_pinboard_progress_handler);
Py_VISIT(self->function_pinboard_authorizer_cb);
Py_VISIT(self->row_factory);
Py_VISIT(self->text_factory);
Py_VISIT(self->collations);
Py_VISIT(self->statements);
Py_VISIT(self->cursors);
Py_VISIT(Py_TYPE(self));
return 0;
}

static int
connection_clear(pysqlite_Connection *self)
{
Py_CLEAR(self->statement_cache);
Py_CLEAR(self->isolation_level);
Py_CLEAR(self->statement_cache);
Py_CLEAR(self->statements);
Py_CLEAR(self->cursors);
Py_CLEAR(self->row_factory);
Py_CLEAR(self->text_factory);
Py_CLEAR(self->function_pinboard_trace_callback);
Py_CLEAR(self->function_pinboard_progress_handler);
Py_CLEAR(self->function_pinboard_authorizer_cb);
Py_CLEAR(self->row_factory);
Py_CLEAR(self->text_factory);
Py_CLEAR(self->collations);
Py_CLEAR(self->statements);
Py_CLEAR(self->cursors);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ typedef struct
/* a dictionary of registered collation name => collation callable mappings */
PyObject* collations;

/* Exception objects */
/* Exception objects: borrowed refs. */
PyObject* Warning;
PyObject* Error;
PyObject* InterfaceError;
Expand Down
29 changes: 15 additions & 14 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,44 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
static int
cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->connection);
Py_VISIT(self->description);
Py_VISIT(self->row_cast_map);
Py_VISIT(self->lastrowid);
Py_VISIT(self->row_factory);
Py_VISIT(self->statement);
Py_VISIT(self->next_row);
Py_VISIT(Py_TYPE(self));
return 0;
}

static int
cursor_clear(pysqlite_Cursor *self)
{
/* Reset the statement if the user has not closed the cursor */
if (self->statement) {
pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
}

Py_CLEAR(self->connection);
Py_CLEAR(self->row_cast_map);
Py_CLEAR(self->description);
Py_CLEAR(self->row_cast_map);
Py_CLEAR(self->lastrowid);
Py_CLEAR(self->row_factory);
Py_CLEAR(self->next_row);

if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
if (self->statement) {
/* Reset the statement if the user has not closed the cursor */
pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
}
Py_CLEAR(self->next_row);

return 0;
}

static void
cursor_dealloc(PyObject *self)
cursor_dealloc(pysqlite_Cursor *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
tp->tp_clear(self);
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
}
tp->tp_clear((PyObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ row_clear(pysqlite_Row *self)
static int
row_traverse(pysqlite_Row *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->data);
Py_VISIT(self->description);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand Down
8 changes: 4 additions & 4 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ stmt_dealloc(pysqlite_Statement *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
}
tp->tp_clear((PyObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
Expand All @@ -389,17 +392,14 @@ stmt_clear(pysqlite_Statement *self)
}

Py_CLEAR(self->sql);
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
}
return 0;
}

static int
stmt_traverse(pysqlite_Statement *self, visitproc visit, void *arg)
{
Py_VISIT(self->sql);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->sql);
return 0;
}

Expand Down

0 comments on commit ff359d7

Please sign in to comment.