From 5782fde9ee6a1a2363900a64a3369d7845c8d6cf Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 12 Oct 2020 20:57:48 +0200 Subject: [PATCH 01/11] Handle PyDict_SetItemString() errors in _sqlite3 --- Modules/_sqlite/microprotocols.c | 8 ++- Modules/_sqlite/module.c | 100 +++++++++++++------------------ 2 files changed, 48 insertions(+), 60 deletions(-) diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index 64095adb4db2b2c..c6d987bf5d97756 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -39,12 +39,18 @@ static PyObject *psyco_adapters = NULL; int pysqlite_microprotocols_init(PyObject *dict) { + int ret; + /* create adapters dictionary and put it in module namespace */ if ((psyco_adapters = PyDict_New()) == NULL) { return -1; } - return PyDict_SetItemString(dict, "adapters", psyco_adapters); + ret = PyDict_SetItemString(dict, "adapters", psyco_adapters); + if (ret < 0) { + Py_DECREF(psyco_adapters); + } + return ret; } diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 102026663abd839..209f3234dd59893 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -238,12 +238,18 @@ Enable or disable callback functions throwing errors to stderr."); static void converters_init(PyObject* dict) { + int ret; + _pysqlite_converters = PyDict_New(); if (!_pysqlite_converters) { return; } - PyDict_SetItemString(dict, "converters", _pysqlite_converters); + ret = PyDict_SetItemString(dict, "converters", _pysqlite_converters); + if (ret < 0) { + Py_DECREF(_pysqlite_converters); + } + return; } static PyMethodDef module_methods[] = { @@ -338,6 +344,23 @@ do { \ } \ } while (0) +#define ADD_STRING(dict, name, obj) \ +do { \ + if (PyDict_SetItemString(dict, name, obj) < 0) { \ + Py_DECREF(obj); \ + goto error; \ + } \ +} while (0) + +#define ADD_EXCEPTION(dict, name, exc, base) \ +do { \ + exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \ + if (!exc) { \ + goto error; \ + } \ + ADD_STRING(dict, name, exc); \ +} while (0) + PyMODINIT_FUNC PyInit__sqlite3(void) { PyObject *module, *dict; @@ -373,60 +396,20 @@ PyMODINIT_FUNC PyInit__sqlite3(void) } /*** Create DB-API Exception hierarchy */ - - if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "Error", pysqlite_Error); - - if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "Warning", pysqlite_Warning); + ADD_EXCEPTION(dict, "Error", pysqlite_Error, PyExc_Exception); + ADD_EXCEPTION(dict, "Warning", pysqlite_Warning, PyExc_Exception); /* Error subclasses */ - - if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError); - - if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError); + ADD_EXCEPTION(dict, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error); + ADD_EXCEPTION(dict, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error); /* pysqlite_DatabaseError subclasses */ - - if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError); - - if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError); - - if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError); - - if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) { - goto error; - } - PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError); - - if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "DataError", pysqlite_DataError); - - if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) { - goto error; - } - PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError); + ADD_EXCEPTION(dict, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError); + ADD_EXCEPTION(dict, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError); + ADD_EXCEPTION(dict, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError); + ADD_EXCEPTION(dict, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError); + ADD_EXCEPTION(dict, "DataError", pysqlite_DataError, pysqlite_DatabaseError); + ADD_EXCEPTION(dict, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError); /* In Python 2.x, setting Connection.text_factory to OptimizedUnicode caused Unicode objects to be returned for @@ -434,7 +417,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) Now OptimizedUnicode is an alias for str, so it has no effect. */ Py_INCREF((PyObject*)&PyUnicode_Type); - PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); + ADD_STRING(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); /* Set integer constants */ for (i = 0; _int_constants[i].constant_name != NULL; i++) { @@ -442,24 +425,23 @@ PyMODINIT_FUNC PyInit__sqlite3(void) if (!tmp_obj) { goto error; } - PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj); - Py_DECREF(tmp_obj); + ADD_STRING(dict, _int_constants[i].constant_name, tmp_obj); } if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) { goto error; } - PyDict_SetItemString(dict, "version", tmp_obj); - Py_DECREF(tmp_obj); + ADD_STRING(dict, "version", tmp_obj); if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) { goto error; } - PyDict_SetItemString(dict, "sqlite_version", tmp_obj); - Py_DECREF(tmp_obj); + ADD_STRING(dict, "sqlite_version", tmp_obj); /* initialize microprotocols layer */ - pysqlite_microprotocols_init(dict); + if (pysqlite_microprotocols_init(dict) < 0) { + goto error; + } /* initialize the default converters */ converters_init(dict); From 76c71bfaa7a7404741b3f0679d34488f1b7a3e5b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 12 Oct 2020 21:21:29 +0200 Subject: [PATCH 02/11] Add NEWS --- .../NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst diff --git a/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst b/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst new file mode 100644 index 000000000000000..583d4b07dcf5d25 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst @@ -0,0 +1 @@ +Fix possible ref. leaks in :mod:`sqlite3` module init. From 3bf16c84098a11cbe5a6fc6758f73bfb60450b3c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 12 Oct 2020 21:32:55 +0200 Subject: [PATCH 03/11] Improve naming --- Modules/_sqlite/module.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 209f3234dd59893..8ab850da96056a4 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -344,10 +344,10 @@ do { \ } \ } while (0) -#define ADD_STRING(dict, name, obj) \ +#define ADD_STRING(dict, key, val) \ do { \ - if (PyDict_SetItemString(dict, name, obj) < 0) { \ - Py_DECREF(obj); \ + if (PyDict_SetItemString(dict, key, val) < 0) { \ + Py_DECREF(val); \ goto error; \ } \ } while (0) From 9a0acfd36f6592773b30cd1ed04a7db44d1a38f3 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Tue, 13 Oct 2020 20:27:42 +0200 Subject: [PATCH 04/11] Fix NEWS entry Co-authored-by: Dong-hee Na --- .../next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst b/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst index 583d4b07dcf5d25..7d71e9a70079b95 100644 --- a/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst +++ b/Misc/NEWS.d/next/Library/2020-10-12-21-21-24.bpo-42021.8yv_8-.rst @@ -1 +1 @@ -Fix possible ref. leaks in :mod:`sqlite3` module init. +Fix possible ref leaks in :mod:`sqlite3` module init. From 0a660386392a164a55bb7cd7529c1c11c07f5905 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 13 Oct 2020 20:50:28 +0200 Subject: [PATCH 05/11] Address Dong-hee Na's review comments - Use PyModule_Add*() iso. directly manipulating the module's __dict__ - Fix return type mismatch - Ditch ADD_STRING (after cleaning up, there was no use for it) Co-authored-by: Dong-hee Na --- Modules/_sqlite/microprotocols.c | 11 +++-- Modules/_sqlite/module.c | 70 +++++++++++++------------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index c6d987bf5d97756..ddc30e8a89b460e 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -37,20 +37,19 @@ static PyObject *psyco_adapters = NULL; /* pysqlite_microprotocols_init - initialize the adapters dictionary */ int -pysqlite_microprotocols_init(PyObject *dict) +pysqlite_microprotocols_init(PyObject *module) { - int ret; - /* create adapters dictionary and put it in module namespace */ if ((psyco_adapters = PyDict_New()) == NULL) { return -1; } - ret = PyDict_SetItemString(dict, "adapters", psyco_adapters); - if (ret < 0) { + if (PyModule_AddObject(module, "adapters", psyco_adapters) < 0) { Py_DECREF(psyco_adapters); + return -1; } - return ret; + + return 0; } diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 8ab850da96056a4..32551eafabef9c8 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -236,17 +236,14 @@ PyDoc_STRVAR(enable_callback_tracebacks_doc, \n\ Enable or disable callback functions throwing errors to stderr."); -static void converters_init(PyObject* dict) +static void converters_init(PyObject* module) { - int ret; - _pysqlite_converters = PyDict_New(); if (!_pysqlite_converters) { return; } - ret = PyDict_SetItemString(dict, "converters", _pysqlite_converters); - if (ret < 0) { + if (PyModule_AddObject(module, "converters", _pysqlite_converters) < 0) { Py_DECREF(_pysqlite_converters); } return; @@ -344,27 +341,21 @@ do { \ } \ } while (0) -#define ADD_STRING(dict, key, val) \ -do { \ - if (PyDict_SetItemString(dict, key, val) < 0) { \ - Py_DECREF(val); \ - goto error; \ - } \ -} while (0) - -#define ADD_EXCEPTION(dict, name, exc, base) \ +#define ADD_EXCEPTION(module, name, exc, base) \ do { \ exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \ if (!exc) { \ goto error; \ } \ - ADD_STRING(dict, name, exc); \ + if (PyModule_AddObject(module, name, exc) < 0) { \ + Py_DECREF(exc); \ + goto error; \ + } \ } while (0) PyMODINIT_FUNC PyInit__sqlite3(void) { - PyObject *module, *dict; - PyObject *tmp_obj; + PyObject *module; int i; if (sqlite3_libversion_number() < 3007003) { @@ -391,25 +382,21 @@ PyMODINIT_FUNC PyInit__sqlite3(void) ADD_TYPE(module, *pysqlite_PrepareProtocolType); ADD_TYPE(module, *pysqlite_RowType); - if (!(dict = PyModule_GetDict(module))) { - goto error; - } - /*** Create DB-API Exception hierarchy */ - ADD_EXCEPTION(dict, "Error", pysqlite_Error, PyExc_Exception); - ADD_EXCEPTION(dict, "Warning", pysqlite_Warning, PyExc_Exception); + ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception); + ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception); /* Error subclasses */ - ADD_EXCEPTION(dict, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error); - ADD_EXCEPTION(dict, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error); + ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error); + ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error); /* pysqlite_DatabaseError subclasses */ - ADD_EXCEPTION(dict, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError); - ADD_EXCEPTION(dict, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError); - ADD_EXCEPTION(dict, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError); - ADD_EXCEPTION(dict, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError); - ADD_EXCEPTION(dict, "DataError", pysqlite_DataError, pysqlite_DatabaseError); - ADD_EXCEPTION(dict, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError); + ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError); + ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError); + ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError); + ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError); + ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError); + ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError); /* In Python 2.x, setting Connection.text_factory to OptimizedUnicode caused Unicode objects to be returned for @@ -417,34 +404,35 @@ PyMODINIT_FUNC PyInit__sqlite3(void) Now OptimizedUnicode is an alias for str, so it has no effect. */ Py_INCREF((PyObject*)&PyUnicode_Type); - ADD_STRING(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); + if (PyModule_AddObject(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) { + Py_DECREF((PyObject*)&PyUnicode_Type); + goto error; + } /* Set integer constants */ for (i = 0; _int_constants[i].constant_name != NULL; i++) { - tmp_obj = PyLong_FromLong(_int_constants[i].constant_value); - if (!tmp_obj) { + if (PyModule_AddIntConstant(module, + _int_constants[i].constant_name, + _int_constants[i].constant_value) < 0) { goto error; } - ADD_STRING(dict, _int_constants[i].constant_name, tmp_obj); } - if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) { + if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) { goto error; } - ADD_STRING(dict, "version", tmp_obj); - if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) { + if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) { goto error; } - ADD_STRING(dict, "sqlite_version", tmp_obj); /* initialize microprotocols layer */ - if (pysqlite_microprotocols_init(dict) < 0) { + if (pysqlite_microprotocols_init(module) < 0) { goto error; } /* initialize the default converters */ - converters_init(dict); + converters_init(module); error: if (PyErr_Occurred()) From 2346055063b92f6a6aa899eaa66ffd157aeea5e5 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 13 Oct 2020 22:21:05 +0200 Subject: [PATCH 06/11] Nitpick, declaration parameter naming --- Modules/_sqlite/microprotocols.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/microprotocols.h b/Modules/_sqlite/microprotocols.h index 5418c2b98fd7519..87df6bac55797ae 100644 --- a/Modules/_sqlite/microprotocols.h +++ b/Modules/_sqlite/microprotocols.h @@ -38,7 +38,7 @@ /** exported functions **/ /* used by module.c to init the microprotocols system */ -extern int pysqlite_microprotocols_init(PyObject *dict); +extern int pysqlite_microprotocols_init(PyObject *module); extern int pysqlite_microprotocols_add( PyTypeObject *type, PyObject *proto, PyObject *cast); extern PyObject *pysqlite_microprotocols_adapt( From 70a42ab87d90d57f056effcc41c8597d3dc9b290 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 13 Oct 2020 21:26:15 +0200 Subject: [PATCH 07/11] Use PyModule_AddIntMacro() to add SQLite constants --- Modules/_sqlite/module.c | 104 +++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 58 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 32551eafabef9c8..0297e2fab292e57 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -267,59 +267,52 @@ static PyMethodDef module_methods[] = { {NULL, NULL} }; -struct _IntConstantPair { - const char *constant_name; - int constant_value; -}; - -typedef struct _IntConstantPair IntConstantPair; - -static const IntConstantPair _int_constants[] = { - {"PARSE_DECLTYPES", PARSE_DECLTYPES}, - {"PARSE_COLNAMES", PARSE_COLNAMES}, - - {"SQLITE_OK", SQLITE_OK}, - {"SQLITE_DENY", SQLITE_DENY}, - {"SQLITE_IGNORE", SQLITE_IGNORE}, - {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX}, - {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE}, - {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX}, - {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE}, - {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER}, - {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW}, - {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER}, - {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW}, - {"SQLITE_DELETE", SQLITE_DELETE}, - {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX}, - {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE}, - {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX}, - {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE}, - {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER}, - {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW}, - {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER}, - {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW}, - {"SQLITE_INSERT", SQLITE_INSERT}, - {"SQLITE_PRAGMA", SQLITE_PRAGMA}, - {"SQLITE_READ", SQLITE_READ}, - {"SQLITE_SELECT", SQLITE_SELECT}, - {"SQLITE_TRANSACTION", SQLITE_TRANSACTION}, - {"SQLITE_UPDATE", SQLITE_UPDATE}, - {"SQLITE_ATTACH", SQLITE_ATTACH}, - {"SQLITE_DETACH", SQLITE_DETACH}, - {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE}, - {"SQLITE_REINDEX", SQLITE_REINDEX}, - {"SQLITE_ANALYZE", SQLITE_ANALYZE}, - {"SQLITE_CREATE_VTABLE", SQLITE_CREATE_VTABLE}, - {"SQLITE_DROP_VTABLE", SQLITE_DROP_VTABLE}, - {"SQLITE_FUNCTION", SQLITE_FUNCTION}, - {"SQLITE_SAVEPOINT", SQLITE_SAVEPOINT}, +static int add_integer_constants(PyObject *module) { + int ret = 0; + + ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES); + ret += PyModule_AddIntMacro(module, PARSE_COLNAMES); + ret += PyModule_AddIntMacro(module, SQLITE_OK); + ret += PyModule_AddIntMacro(module, SQLITE_DENY); + ret += PyModule_AddIntMacro(module, SQLITE_IGNORE); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW); + ret += PyModule_AddIntMacro(module, SQLITE_DELETE); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW); + ret += PyModule_AddIntMacro(module, SQLITE_INSERT); + ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA); + ret += PyModule_AddIntMacro(module, SQLITE_READ); + ret += PyModule_AddIntMacro(module, SQLITE_SELECT); + ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION); + ret += PyModule_AddIntMacro(module, SQLITE_UPDATE); + ret += PyModule_AddIntMacro(module, SQLITE_ATTACH); + ret += PyModule_AddIntMacro(module, SQLITE_DETACH); + ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE); + ret += PyModule_AddIntMacro(module, SQLITE_REINDEX); + ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE); + ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE); + ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE); + ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION); + ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT); #if SQLITE_VERSION_NUMBER >= 3008003 - {"SQLITE_RECURSIVE", SQLITE_RECURSIVE}, + ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE); #endif - {"SQLITE_DONE", SQLITE_DONE}, - {(char*)NULL, 0} -}; - + ret += PyModule_AddIntMacro(module, SQLITE_DONE); + return ret; +} static struct PyModuleDef _sqlite3module = { PyModuleDef_HEAD_INIT, @@ -356,7 +349,6 @@ do { \ PyMODINIT_FUNC PyInit__sqlite3(void) { PyObject *module; - int i; if (sqlite3_libversion_number() < 3007003) { PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.3 or higher required"); @@ -410,12 +402,8 @@ PyMODINIT_FUNC PyInit__sqlite3(void) } /* Set integer constants */ - for (i = 0; _int_constants[i].constant_name != NULL; i++) { - if (PyModule_AddIntConstant(module, - _int_constants[i].constant_name, - _int_constants[i].constant_value) < 0) { - goto error; - } + if (add_integer_constants(module) < 0) { + goto error; } if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) { From bc4d076b058d523065b10d1f3e1f6346de15f38c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 14 Oct 2020 15:37:02 +0200 Subject: [PATCH 08/11] INCREF before PyModule_AddObject Co-authored-by: Dong-hee Na --- Modules/_sqlite/module.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 0297e2fab292e57..4a0a00f1cb6c7b9 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -340,6 +340,7 @@ do { \ if (!exc) { \ goto error; \ } \ + Py_INCREF(exc); \ if (PyModule_AddObject(module, name, exc) < 0) { \ Py_DECREF(exc); \ goto error; \ From 6a5a7cbb6c653cba9b41e96ef3f999707ba0fcf4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 15 Oct 2020 09:35:04 +0200 Subject: [PATCH 09/11] Use Py_INCREF after PyDict_New Co-authored-by: Dong-hee Na --- Modules/_sqlite/microprotocols.c | 1 + Modules/_sqlite/module.c | 1 + 2 files changed, 2 insertions(+) diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index ddc30e8a89b460e..500d83791157f81 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -44,6 +44,7 @@ pysqlite_microprotocols_init(PyObject *module) return -1; } + Py_INCREF(psyco_adapters); if (PyModule_AddObject(module, "adapters", psyco_adapters) < 0) { Py_DECREF(psyco_adapters); return -1; diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 4a0a00f1cb6c7b9..453db05fba57cfd 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -243,6 +243,7 @@ static void converters_init(PyObject* module) return; } + Py_INCREF(_pysqlite_converters); if (PyModule_AddObject(module, "converters", _pysqlite_converters) < 0) { Py_DECREF(_pysqlite_converters); } From 5ac72989deabb47c7cc620a0ae77cc737443b358 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 15 Oct 2020 13:53:07 +0200 Subject: [PATCH 10/11] Revert "Use Py_INCREF after PyDict_New" This reverts commit 6a5a7cbb6c653cba9b41e96ef3f999707ba0fcf4. --- Modules/_sqlite/microprotocols.c | 1 - Modules/_sqlite/module.c | 1 - 2 files changed, 2 deletions(-) diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index 500d83791157f81..ddc30e8a89b460e 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -44,7 +44,6 @@ pysqlite_microprotocols_init(PyObject *module) return -1; } - Py_INCREF(psyco_adapters); if (PyModule_AddObject(module, "adapters", psyco_adapters) < 0) { Py_DECREF(psyco_adapters); return -1; diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 453db05fba57cfd..4a0a00f1cb6c7b9 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -243,7 +243,6 @@ static void converters_init(PyObject* module) return; } - Py_INCREF(_pysqlite_converters); if (PyModule_AddObject(module, "converters", _pysqlite_converters) < 0) { Py_DECREF(_pysqlite_converters); } From 94e66e17a57619490840f06362b31b50cf141614 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 15 Oct 2020 13:53:17 +0200 Subject: [PATCH 11/11] Revert "INCREF before PyModule_AddObject" This reverts commit bc4d076b058d523065b10d1f3e1f6346de15f38c. --- Modules/_sqlite/module.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 4a0a00f1cb6c7b9..0297e2fab292e57 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -340,7 +340,6 @@ do { \ if (!exc) { \ goto error; \ } \ - Py_INCREF(exc); \ if (PyModule_AddObject(module, name, exc) < 0) { \ Py_DECREF(exc); \ goto error; \