From 62bbb5c0afd7600dc581a9471a1b0f0d4eef4d91 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 29 May 2021 20:25:25 +0200 Subject: [PATCH] bpo-42972: Fix sqlite3 traverse/clear --- Modules/_sqlite/cache.c | 22 ++++++++++++---------- Modules/_sqlite/connection.c | 22 +++++++++++----------- Modules/_sqlite/connection.h | 2 +- Modules/_sqlite/cursor.c | 29 +++++++++++++++-------------- Modules/_sqlite/row.c | 2 +- Modules/_sqlite/statement.c | 8 ++++---- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index e0a1707348c912..fd4e619f6a0115 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -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; } @@ -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; @@ -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; } diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 47d97d17a91b98..54e477739bd457 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -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; } diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h index 82f6baf6eef3d7..8773c9eac08861 100644 --- a/Modules/_sqlite/connection.h +++ b/Modules/_sqlite/connection.h @@ -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; diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index b3e1ce2c04784b..4eb9c6d28b103e 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -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); } diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index af8be803c805a5..24722be49082cf 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -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; } diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 3be12c79b47d9a..332bf030a9b909 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -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); @@ -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; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy