From fc2002422a22be75a93ac9ac799abddba2793839 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 22 May 2020 23:58:13 +0200 Subject: [PATCH 01/15] Use PyModule_AddObject() correctly in sqlite3 According to the docs, PyModule_AddObject() only decrements the reference count of value _on success_. Calling code must Py_DECREF() manually on error. --- Modules/_sqlite/module.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 4d9d3d41c7b71b9..ffa88e69b39bb5a 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -346,6 +346,16 @@ static struct PyModuleDef _sqlite3module = { NULL }; +#define ADD_OBJECT(module, name, type) \ +do { \ + Py_INCREF(&type); \ + if (PyModule_AddObject(module, name, (PyObject *)&type) < 0) { \ + Py_DECREF(module); \ + Py_DECREF(&type); \ + return NULL; \ + } \ +} while (0) + PyMODINIT_FUNC PyInit__sqlite3(void) { PyObject *module, *dict; @@ -366,14 +376,10 @@ PyMODINIT_FUNC PyInit__sqlite3(void) return NULL; } - Py_INCREF(&pysqlite_ConnectionType); - PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType); - Py_INCREF(&pysqlite_CursorType); - PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType); - Py_INCREF(&pysqlite_PrepareProtocolType); - PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType); - Py_INCREF(&pysqlite_RowType); - PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType); + ADD_OBJECT(module, "Connection", pysqlite_ConnectionType); + ADD_OBJECT(module, "Cursor", pysqlite_CursorType); + ADD_OBJECT(module, "PrepareProtocol", pysqlite_PrepareProtocolType); + ADD_OBJECT(module, "Row", pysqlite_RowType); if (!(dict = PyModule_GetDict(module))) { goto error; From a51d38066c81fae4486274fb6aecfb62106f2c79 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 23 May 2020 00:22:18 +0200 Subject: [PATCH 02/15] Add NEWS entry --- .../NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst diff --git a/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst b/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst new file mode 100644 index 000000000000000..f9a1a234104e4f8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst @@ -0,0 +1 @@ +Handle PyModule_AddObject() errors in sqlite3 module init correctly. From df83c42765698f50306050eacfdc0a08e879ddfc Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Mon, 25 May 2020 17:44:49 +0200 Subject: [PATCH 03/15] Update Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst Co-authored-by: Dong-hee Na --- .../next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst b/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst index f9a1a234104e4f8..f068d3a091a03b0 100644 --- a/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst +++ b/Misc/NEWS.d/next/Library/2020-05-23-00-22-11.bpo-40737.iph-CM.rst @@ -1 +1 @@ -Handle PyModule_AddObject() errors in sqlite3 module init correctly. +Fix possible reference leak for :mod:`sqlite3` initialization. From 1b5def8176370e7efba1790ff383fb12eaf0f3bf Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Mon, 25 May 2020 17:49:11 +0200 Subject: [PATCH 04/15] Update Modules/_sqlite/module.c Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index ffa88e69b39bb5a..13e257163065859 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -346,7 +346,7 @@ static struct PyModuleDef _sqlite3module = { NULL }; -#define ADD_OBJECT(module, name, type) \ +#define ADD_TYPE(module, type) \ do { \ Py_INCREF(&type); \ if (PyModule_AddObject(module, name, (PyObject *)&type) < 0) { \ From 9e954fb9878592c742bfee3ba20b50bb59e68947 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Mon, 25 May 2020 17:49:20 +0200 Subject: [PATCH 05/15] Update Modules/_sqlite/module.c Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 13e257163065859..8c51b67f7d8d43d 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -376,7 +376,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) return NULL; } - ADD_OBJECT(module, "Connection", pysqlite_ConnectionType); + ADD_TYPE(module, pysqlite_ConnectionType); ADD_OBJECT(module, "Cursor", pysqlite_CursorType); ADD_OBJECT(module, "PrepareProtocol", pysqlite_PrepareProtocolType); ADD_OBJECT(module, "Row", pysqlite_RowType); From 59f6cfb8037192489a66eab66495ada9d30065b5 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Mon, 25 May 2020 17:49:28 +0200 Subject: [PATCH 06/15] Update Modules/_sqlite/module.c Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 8c51b67f7d8d43d..ade78e51609bcbd 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -377,7 +377,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) } ADD_TYPE(module, pysqlite_ConnectionType); - ADD_OBJECT(module, "Cursor", pysqlite_CursorType); + ADD_TYPE(module, pysqlite_CursorType); ADD_OBJECT(module, "PrepareProtocol", pysqlite_PrepareProtocolType); ADD_OBJECT(module, "Row", pysqlite_RowType); From 1ea05c95b24c5c7be999d26da6b54ef67e9ab11f Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Mon, 25 May 2020 17:49:35 +0200 Subject: [PATCH 07/15] Update Modules/_sqlite/module.c Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index ade78e51609bcbd..848b77beb5dfd70 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -378,7 +378,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, pysqlite_CursorType); - ADD_OBJECT(module, "PrepareProtocol", pysqlite_PrepareProtocolType); + ADD_TYPE(module, pysqlite_PrepareProtocolType); ADD_OBJECT(module, "Row", pysqlite_RowType); if (!(dict = PyModule_GetDict(module))) { From 37403bc0e9ac3e556cd82ae61d0914a859117204 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Mon, 25 May 2020 17:49:42 +0200 Subject: [PATCH 08/15] Update Modules/_sqlite/module.c Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 848b77beb5dfd70..ae82764e284ebd7 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -379,7 +379,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, pysqlite_CursorType); ADD_TYPE(module, pysqlite_PrepareProtocolType); - ADD_OBJECT(module, "Row", pysqlite_RowType); + ADD_TYPE(module, pysqlite_RowType); if (!(dict = PyModule_GetDict(module))) { goto error; From 8d6e1167a19bb9aa35ceecddb0f7f7ed59a4f93d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 25 May 2020 18:12:43 +0200 Subject: [PATCH 09/15] Use PyModule_AddType iso. PyModule_AddObject --- Modules/_sqlite/cache.c | 12 +--------- Modules/_sqlite/cache.h | 2 +- Modules/_sqlite/connection.c | 3 +-- Modules/_sqlite/connection.h | 2 +- Modules/_sqlite/cursor.c | 3 +-- Modules/_sqlite/cursor.h | 2 +- Modules/_sqlite/module.c | 35 +++++++++++++----------------- Modules/_sqlite/prepare_protocol.c | 3 +-- Modules/_sqlite/prepare_protocol.h | 2 +- Modules/_sqlite/row.c | 3 +-- Modules/_sqlite/row.h | 2 +- Modules/_sqlite/statement.c | 3 +-- Modules/_sqlite/statement.h | 2 +- 13 files changed, 27 insertions(+), 47 deletions(-) diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 758fc022f781087..2540be4d29efd62 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -345,18 +345,8 @@ PyTypeObject pysqlite_CacheType = { 0 /* tp_free */ }; -extern int pysqlite_cache_setup_types(void) +extern void pysqlite_cache_setup_types(void) { - int rc; - pysqlite_NodeType.tp_new = PyType_GenericNew; pysqlite_CacheType.tp_new = PyType_GenericNew; - - rc = PyType_Ready(&pysqlite_NodeType); - if (rc < 0) { - return rc; - } - - rc = PyType_Ready(&pysqlite_CacheType); - return rc; } diff --git a/Modules/_sqlite/cache.h b/Modules/_sqlite/cache.h index 529010967c4f3a9..d3ae8916e2745fb 100644 --- a/Modules/_sqlite/cache.h +++ b/Modules/_sqlite/cache.h @@ -69,6 +69,6 @@ int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs); void pysqlite_cache_dealloc(pysqlite_Cache* self); PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args); -int pysqlite_cache_setup_types(void); +void pysqlite_cache_setup_types(void); #endif diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 958be7d869794af..549dc16295cafef 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1872,8 +1872,7 @@ PyTypeObject pysqlite_ConnectionType = { 0 /* tp_free */ }; -extern int pysqlite_connection_setup_types(void) +extern void pysqlite_connection_setup_types(void) { pysqlite_ConnectionType.tp_new = PyType_GenericNew; - return PyType_Ready(&pysqlite_ConnectionType); } diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h index 206085e00a00c70..e45609f0cf4c482 100644 --- a/Modules/_sqlite/connection.h +++ b/Modules/_sqlite/connection.h @@ -122,6 +122,6 @@ int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObjec int pysqlite_check_thread(pysqlite_Connection* self); int pysqlite_check_connection(pysqlite_Connection* con); -int pysqlite_connection_setup_types(void); +void pysqlite_connection_setup_types(void); #endif diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 5cfb4b97d61dfe3..7cddc6d5e28222b 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -946,8 +946,7 @@ PyTypeObject pysqlite_CursorType = { 0 /* tp_free */ }; -extern int pysqlite_cursor_setup_types(void) +extern void pysqlite_cursor_setup_types(void) { pysqlite_CursorType.tp_new = PyType_GenericNew; - return PyType_Ready(&pysqlite_CursorType); } diff --git a/Modules/_sqlite/cursor.h b/Modules/_sqlite/cursor.h index 4a20e756f7829d9..33ddbc40a81af3f 100644 --- a/Modules/_sqlite/cursor.h +++ b/Modules/_sqlite/cursor.h @@ -64,7 +64,7 @@ PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args); PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args); PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args); -int pysqlite_cursor_setup_types(void); +void pysqlite_cursor_setup_types(void); #define UNKNOWN (-1) #endif diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index ae82764e284ebd7..d9de3b159d8f4d1 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -346,14 +346,12 @@ static struct PyModuleDef _sqlite3module = { NULL }; -#define ADD_TYPE(module, type) \ -do { \ - Py_INCREF(&type); \ - if (PyModule_AddObject(module, name, (PyObject *)&type) < 0) { \ - Py_DECREF(module); \ - Py_DECREF(&type); \ - return NULL; \ - } \ +#define ADD_TYPE(module, type) \ +do { \ + if (PyModule_AddType(module, &type) < 0) { \ + Py_DECREF(module); \ + return NULL; \ + } \ } while (0) PyMODINIT_FUNC PyInit__sqlite3(void) @@ -362,21 +360,18 @@ PyMODINIT_FUNC PyInit__sqlite3(void) PyObject *tmp_obj; int i; - module = PyModule_Create(&_sqlite3module); - - if (!module || - (pysqlite_row_setup_types() < 0) || - (pysqlite_cursor_setup_types() < 0) || - (pysqlite_connection_setup_types() < 0) || - (pysqlite_cache_setup_types() < 0) || - (pysqlite_statement_setup_types() < 0) || - (pysqlite_prepare_protocol_setup_types() < 0) - ) { - Py_XDECREF(module); + if (!(module = PyModule_Create(&_sqlite3module))) { return NULL; } - ADD_TYPE(module, pysqlite_ConnectionType); + pysqlite_row_setup_types(); + pysqlite_cursor_setup_types(); + pysqlite_connection_setup_types(); + pysqlite_cache_setup_types(); + pysqlite_statement_setup_types(); + pysqlite_prepare_protocol_setup_types(); + + ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, pysqlite_CursorType); ADD_TYPE(module, pysqlite_PrepareProtocolType); ADD_TYPE(module, pysqlite_RowType); diff --git a/Modules/_sqlite/prepare_protocol.c b/Modules/_sqlite/prepare_protocol.c index 05a2ca5a652f5e3..135f5ae882bf892 100644 --- a/Modules/_sqlite/prepare_protocol.c +++ b/Modules/_sqlite/prepare_protocol.c @@ -75,9 +75,8 @@ PyTypeObject pysqlite_PrepareProtocolType= { 0 /* tp_free */ }; -extern int pysqlite_prepare_protocol_setup_types(void) +extern void pysqlite_prepare_protocol_setup_types(void) { pysqlite_PrepareProtocolType.tp_new = PyType_GenericNew; Py_SET_TYPE(&pysqlite_PrepareProtocolType, &PyType_Type); - return PyType_Ready(&pysqlite_PrepareProtocolType); } diff --git a/Modules/_sqlite/prepare_protocol.h b/Modules/_sqlite/prepare_protocol.h index 3998a55e51cafee..586e37d3599e87d 100644 --- a/Modules/_sqlite/prepare_protocol.h +++ b/Modules/_sqlite/prepare_protocol.h @@ -36,7 +36,7 @@ extern PyTypeObject pysqlite_PrepareProtocolType; int pysqlite_prepare_protocol_init(pysqlite_PrepareProtocol* self, PyObject* args, PyObject* kwargs); void pysqlite_prepare_protocol_dealloc(pysqlite_PrepareProtocol* self); -int pysqlite_prepare_protocol_setup_types(void); +void pysqlite_prepare_protocol_setup_types(void); #define UNKNOWN (-1) #endif diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index 4b47108278a0ab4..f83e2608c925ba7 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -269,10 +269,9 @@ PyTypeObject pysqlite_RowType = { 0 /* tp_free */ }; -extern int pysqlite_row_setup_types(void) +extern void pysqlite_row_setup_types(void) { pysqlite_RowType.tp_new = pysqlite_row_new; pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping; pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence; - return PyType_Ready(&pysqlite_RowType); } diff --git a/Modules/_sqlite/row.h b/Modules/_sqlite/row.h index 4ad506f8dd96873..81af050e5fabeda 100644 --- a/Modules/_sqlite/row.h +++ b/Modules/_sqlite/row.h @@ -35,6 +35,6 @@ typedef struct _Row extern PyTypeObject pysqlite_RowType; -int pysqlite_row_setup_types(void); +void pysqlite_row_setup_types(void); #endif diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 9de8f9b67228f5c..09ddd793ed942d5 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -497,8 +497,7 @@ PyTypeObject pysqlite_StatementType = { 0 /* tp_free */ }; -extern int pysqlite_statement_setup_types(void) +extern void pysqlite_statement_setup_types(void) { pysqlite_StatementType.tp_new = PyType_GenericNew; - return PyType_Ready(&pysqlite_StatementType); } diff --git a/Modules/_sqlite/statement.h b/Modules/_sqlite/statement.h index 5002f02dc5b3926..7b88fca946ddfae 100644 --- a/Modules/_sqlite/statement.h +++ b/Modules/_sqlite/statement.h @@ -55,6 +55,6 @@ int pysqlite_statement_finalize(pysqlite_Statement* self); int pysqlite_statement_reset(pysqlite_Statement* self); void pysqlite_statement_mark_dirty(pysqlite_Statement* self); -int pysqlite_statement_setup_types(void); +void pysqlite_statement_setup_types(void); #endif From 01e89c4845afc82d4ca9576cb85ec27210f46953 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 25 May 2020 19:48:52 +0200 Subject: [PATCH 10/15] Fix dot in Node name --- Modules/_sqlite/cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 2540be4d29efd62..7f2c581843a31d8 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -263,7 +263,7 @@ static PyMethodDef cache_methods[] = { PyTypeObject pysqlite_NodeType = { PyVarObject_HEAD_INIT(NULL, 0) - MODULE_NAME "Node", /* tp_name */ + MODULE_NAME ".Node", /* tp_name */ sizeof(pysqlite_Node), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)pysqlite_node_dealloc, /* tp_dealloc */ From 40bab484fb4dc37b22ff0b8c9befa4d7a4c01e47 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 25 May 2020 19:55:54 +0200 Subject: [PATCH 11/15] Set up _all_ types --- Modules/_sqlite/module.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index d9de3b159d8f4d1..350ee1295d5b77e 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -364,17 +364,20 @@ PyMODINIT_FUNC PyInit__sqlite3(void) return NULL; } - pysqlite_row_setup_types(); - pysqlite_cursor_setup_types(); - pysqlite_connection_setup_types(); pysqlite_cache_setup_types(); - pysqlite_statement_setup_types(); + pysqlite_connection_setup_types(); + pysqlite_cursor_setup_types(); pysqlite_prepare_protocol_setup_types(); + pysqlite_row_setup_types(); + pysqlite_statement_setup_types(); + ADD_TYPE(module, pysqlite_CacheType); ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, pysqlite_CursorType); + ADD_TYPE(module, pysqlite_NodeType); ADD_TYPE(module, pysqlite_PrepareProtocolType); ADD_TYPE(module, pysqlite_RowType); + ADD_TYPE(module, pysqlite_StatementType); if (!(dict = PyModule_GetDict(module))) { goto error; From d96c5e40a9286eeaeddedbdd1d6276fb0eb1a142 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 26 May 2020 08:38:45 +0200 Subject: [PATCH 12/15] Revert changing initialization functions This reverts parts of commit 8d6e1167a19bb9aa35ceecddb0f7f7ed59a4f93d. --- Modules/_sqlite/cache.c | 12 +++++++++++- Modules/_sqlite/cache.h | 2 +- Modules/_sqlite/connection.c | 3 ++- Modules/_sqlite/connection.h | 2 +- Modules/_sqlite/cursor.c | 3 ++- Modules/_sqlite/cursor.h | 2 +- Modules/_sqlite/module.c | 20 ++++++++++++-------- Modules/_sqlite/prepare_protocol.c | 3 ++- Modules/_sqlite/prepare_protocol.h | 2 +- Modules/_sqlite/row.c | 3 ++- Modules/_sqlite/row.h | 2 +- Modules/_sqlite/statement.c | 3 ++- Modules/_sqlite/statement.h | 2 +- 13 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 7f2c581843a31d8..4695c2ab2b7c24c 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -345,8 +345,18 @@ PyTypeObject pysqlite_CacheType = { 0 /* tp_free */ }; -extern void pysqlite_cache_setup_types(void) +extern int pysqlite_cache_setup_types(void) { + int rc; + pysqlite_NodeType.tp_new = PyType_GenericNew; pysqlite_CacheType.tp_new = PyType_GenericNew; + + rc = PyType_Ready(&pysqlite_NodeType); + if (rc < 0) { + return rc; + } + + rc = PyType_Ready(&pysqlite_CacheType); + return rc; } diff --git a/Modules/_sqlite/cache.h b/Modules/_sqlite/cache.h index d3ae8916e2745fb..529010967c4f3a9 100644 --- a/Modules/_sqlite/cache.h +++ b/Modules/_sqlite/cache.h @@ -69,6 +69,6 @@ int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs); void pysqlite_cache_dealloc(pysqlite_Cache* self); PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args); -void pysqlite_cache_setup_types(void); +int pysqlite_cache_setup_types(void); #endif diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 549dc16295cafef..958be7d869794af 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1872,7 +1872,8 @@ PyTypeObject pysqlite_ConnectionType = { 0 /* tp_free */ }; -extern void pysqlite_connection_setup_types(void) +extern int pysqlite_connection_setup_types(void) { pysqlite_ConnectionType.tp_new = PyType_GenericNew; + return PyType_Ready(&pysqlite_ConnectionType); } diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h index e45609f0cf4c482..206085e00a00c70 100644 --- a/Modules/_sqlite/connection.h +++ b/Modules/_sqlite/connection.h @@ -122,6 +122,6 @@ int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObjec int pysqlite_check_thread(pysqlite_Connection* self); int pysqlite_check_connection(pysqlite_Connection* con); -void pysqlite_connection_setup_types(void); +int pysqlite_connection_setup_types(void); #endif diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 7cddc6d5e28222b..5cfb4b97d61dfe3 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -946,7 +946,8 @@ PyTypeObject pysqlite_CursorType = { 0 /* tp_free */ }; -extern void pysqlite_cursor_setup_types(void) +extern int pysqlite_cursor_setup_types(void) { pysqlite_CursorType.tp_new = PyType_GenericNew; + return PyType_Ready(&pysqlite_CursorType); } diff --git a/Modules/_sqlite/cursor.h b/Modules/_sqlite/cursor.h index 33ddbc40a81af3f..4a20e756f7829d9 100644 --- a/Modules/_sqlite/cursor.h +++ b/Modules/_sqlite/cursor.h @@ -64,7 +64,7 @@ PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args); PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args); PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args); -void pysqlite_cursor_setup_types(void); +int pysqlite_cursor_setup_types(void); #define UNKNOWN (-1) #endif diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 350ee1295d5b77e..025f724a8338e3f 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -348,6 +348,7 @@ static struct PyModuleDef _sqlite3module = { #define ADD_TYPE(module, type) \ do { \ + Py_INCREF(&type); \ if (PyModule_AddType(module, &type) < 0) { \ Py_DECREF(module); \ return NULL; \ @@ -360,17 +361,20 @@ PyMODINIT_FUNC PyInit__sqlite3(void) PyObject *tmp_obj; int i; - if (!(module = PyModule_Create(&_sqlite3module))) { + module = PyModule_Create(&_sqlite3module); + + if (!module || + (pysqlite_row_setup_types() < 0) || + (pysqlite_cursor_setup_types() < 0) || + (pysqlite_connection_setup_types() < 0) || + (pysqlite_cache_setup_types() < 0) || + (pysqlite_statement_setup_types() < 0) || + (pysqlite_prepare_protocol_setup_types() < 0) + ) { + Py_XDECREF(module); return NULL; } - pysqlite_cache_setup_types(); - pysqlite_connection_setup_types(); - pysqlite_cursor_setup_types(); - pysqlite_prepare_protocol_setup_types(); - pysqlite_row_setup_types(); - pysqlite_statement_setup_types(); - ADD_TYPE(module, pysqlite_CacheType); ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, pysqlite_CursorType); diff --git a/Modules/_sqlite/prepare_protocol.c b/Modules/_sqlite/prepare_protocol.c index 135f5ae882bf892..05a2ca5a652f5e3 100644 --- a/Modules/_sqlite/prepare_protocol.c +++ b/Modules/_sqlite/prepare_protocol.c @@ -75,8 +75,9 @@ PyTypeObject pysqlite_PrepareProtocolType= { 0 /* tp_free */ }; -extern void pysqlite_prepare_protocol_setup_types(void) +extern int pysqlite_prepare_protocol_setup_types(void) { pysqlite_PrepareProtocolType.tp_new = PyType_GenericNew; Py_SET_TYPE(&pysqlite_PrepareProtocolType, &PyType_Type); + return PyType_Ready(&pysqlite_PrepareProtocolType); } diff --git a/Modules/_sqlite/prepare_protocol.h b/Modules/_sqlite/prepare_protocol.h index 586e37d3599e87d..3998a55e51cafee 100644 --- a/Modules/_sqlite/prepare_protocol.h +++ b/Modules/_sqlite/prepare_protocol.h @@ -36,7 +36,7 @@ extern PyTypeObject pysqlite_PrepareProtocolType; int pysqlite_prepare_protocol_init(pysqlite_PrepareProtocol* self, PyObject* args, PyObject* kwargs); void pysqlite_prepare_protocol_dealloc(pysqlite_PrepareProtocol* self); -void pysqlite_prepare_protocol_setup_types(void); +int pysqlite_prepare_protocol_setup_types(void); #define UNKNOWN (-1) #endif diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index f83e2608c925ba7..4b47108278a0ab4 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -269,9 +269,10 @@ PyTypeObject pysqlite_RowType = { 0 /* tp_free */ }; -extern void pysqlite_row_setup_types(void) +extern int pysqlite_row_setup_types(void) { pysqlite_RowType.tp_new = pysqlite_row_new; pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping; pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence; + return PyType_Ready(&pysqlite_RowType); } diff --git a/Modules/_sqlite/row.h b/Modules/_sqlite/row.h index 81af050e5fabeda..4ad506f8dd96873 100644 --- a/Modules/_sqlite/row.h +++ b/Modules/_sqlite/row.h @@ -35,6 +35,6 @@ typedef struct _Row extern PyTypeObject pysqlite_RowType; -void pysqlite_row_setup_types(void); +int pysqlite_row_setup_types(void); #endif diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 09ddd793ed942d5..9de8f9b67228f5c 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -497,7 +497,8 @@ PyTypeObject pysqlite_StatementType = { 0 /* tp_free */ }; -extern void pysqlite_statement_setup_types(void) +extern int pysqlite_statement_setup_types(void) { pysqlite_StatementType.tp_new = PyType_GenericNew; + return PyType_Ready(&pysqlite_StatementType); } diff --git a/Modules/_sqlite/statement.h b/Modules/_sqlite/statement.h index 7b88fca946ddfae..5002f02dc5b3926 100644 --- a/Modules/_sqlite/statement.h +++ b/Modules/_sqlite/statement.h @@ -55,6 +55,6 @@ int pysqlite_statement_finalize(pysqlite_Statement* self); int pysqlite_statement_reset(pysqlite_Statement* self); void pysqlite_statement_mark_dirty(pysqlite_Statement* self); -void pysqlite_statement_setup_types(void); +int pysqlite_statement_setup_types(void); #endif From 408682591ee5be2f1ff22328c1e93e441e74e766 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 26 May 2020 09:13:22 +0200 Subject: [PATCH 13/15] Only add relevant types --- Modules/_sqlite/module.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 025f724a8338e3f..a9d4650883c2092 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -375,13 +375,10 @@ PyMODINIT_FUNC PyInit__sqlite3(void) return NULL; } - ADD_TYPE(module, pysqlite_CacheType); ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, pysqlite_CursorType); - ADD_TYPE(module, pysqlite_NodeType); ADD_TYPE(module, pysqlite_PrepareProtocolType); ADD_TYPE(module, pysqlite_RowType); - ADD_TYPE(module, pysqlite_StatementType); if (!(dict = PyModule_GetDict(module))) { goto error; From 21e678871e28d11c3a84a6380b540756f7ff7a7f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 26 May 2020 10:20:21 +0200 Subject: [PATCH 14/15] Revert "Fix dot in Node name" This reverts commit 01e89c4845afc82d4ca9576cb85ec27210f46953. --- Modules/_sqlite/cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 4695c2ab2b7c24c..758fc022f781087 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -263,7 +263,7 @@ static PyMethodDef cache_methods[] = { PyTypeObject pysqlite_NodeType = { PyVarObject_HEAD_INIT(NULL, 0) - MODULE_NAME ".Node", /* tp_name */ + MODULE_NAME "Node", /* tp_name */ sizeof(pysqlite_Node), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)pysqlite_node_dealloc, /* tp_dealloc */ From 841eab664a1c1131b3ecb1a4ee2653962154ed6b Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Tue, 26 May 2020 10:29:06 +0200 Subject: [PATCH 15/15] Update Modules/_sqlite/module.c Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index a9d4650883c2092..71d951ee887e477 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -348,7 +348,6 @@ static struct PyModuleDef _sqlite3module = { #define ADD_TYPE(module, type) \ do { \ - Py_INCREF(&type); \ if (PyModule_AddType(module, &type) < 0) { \ Py_DECREF(module); \ return NULL; \