From c0625927fe3a90459259054274c21960ead7ffeb Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 4 Nov 2020 15:40:43 +0100 Subject: [PATCH 1/6] Use PyModule_AddObjectRef iso. PyModule_AddObject in _sqlite3 --- Modules/_sqlite/microprotocols.c | 10 +++++----- Modules/_sqlite/module.c | 30 +++++++++++++++++++----------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index cf1fefd671851d..3291d696a38415 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -38,17 +38,17 @@ static PyObject *psyco_adapters = NULL; int pysqlite_microprotocols_init(PyObject *module) { + int res; + /* create adapters dictionary and put it in module namespace */ if ((psyco_adapters = PyDict_New()) == NULL) { return -1; } - if (PyModule_AddObject(module, "adapters", psyco_adapters) < 0) { - Py_DECREF(psyco_adapters); - return -1; - } + res = PyModule_AddObjectRef(module, "adapters", psyco_adapters); + Py_DECREF(psyco_adapters); - return 0; + return res; } diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 33324402385f44..4a8a8c02ab4403 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -263,17 +263,19 @@ pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto, return pysqlite_microprotocols_adapt(obj, proto, alt); } -static void converters_init(PyObject* module) +static int converters_init(PyObject* module) { + int res; + _pysqlite_converters = PyDict_New(); if (!_pysqlite_converters) { - return; + return -1; } - if (PyModule_AddObject(module, "converters", _pysqlite_converters) < 0) { - Py_DECREF(_pysqlite_converters); - } - return; + res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters); + Py_DECREF(_pysqlite_converters); + + return res; } static PyMethodDef module_methods[] = { @@ -357,12 +359,14 @@ do { \ #define ADD_EXCEPTION(module, name, exc, base) \ do { \ + int res; \ exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \ if (!exc) { \ goto error; \ } \ - if (PyModule_AddObject(module, name, exc) < 0) { \ - Py_DECREF(exc); \ + res = PyModule_AddObjectRef(module, name, exc); \ + Py_DECREF(exc); \ + if (res == -1) { \ goto error; \ } \ } while (0) @@ -370,6 +374,7 @@ do { \ PyMODINIT_FUNC PyInit__sqlite3(void) { PyObject *module; + int res; if (sqlite3_libversion_number() < 3007003) { PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.3 or higher required"); @@ -417,8 +422,9 @@ PyMODINIT_FUNC PyInit__sqlite3(void) Now OptimizedUnicode is an alias for str, so it has no effect. */ Py_INCREF((PyObject*)&PyUnicode_Type); - if (PyModule_AddObject(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) { - Py_DECREF((PyObject*)&PyUnicode_Type); + res = PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); + Py_DECREF((PyObject*)&PyUnicode_Type); + if (res == -1) { goto error; } @@ -441,7 +447,9 @@ PyMODINIT_FUNC PyInit__sqlite3(void) } /* initialize the default converters */ - converters_init(module); + if (converters_init(module) < 0) { + goto error; + } error: if (PyErr_Occurred()) From 590d11df2094c3c29b8267a32b280ccc3e16a8e4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 4 Nov 2020 18:36:59 +0100 Subject: [PATCH 2/6] Address review comment: Use intermingled variable declarations --- Modules/_sqlite/module.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 4a8a8c02ab4403..835fba07752851 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -265,14 +265,12 @@ pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto, static int converters_init(PyObject* module) { - int res; - _pysqlite_converters = PyDict_New(); if (!_pysqlite_converters) { return -1; } - res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters); + int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters); Py_DECREF(_pysqlite_converters); return res; @@ -374,7 +372,6 @@ do { \ PyMODINIT_FUNC PyInit__sqlite3(void) { PyObject *module; - int res; if (sqlite3_libversion_number() < 3007003) { PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.3 or higher required"); @@ -422,7 +419,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) Now OptimizedUnicode is an alias for str, so it has no effect. */ Py_INCREF((PyObject*)&PyUnicode_Type); - res = PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); + int res = PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); Py_DECREF((PyObject*)&PyUnicode_Type); if (res == -1) { goto error; From 23ba6fa2a26e18f1599e7b9fd363803e7b4fa731 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 4 Nov 2020 18:56:29 +0100 Subject: [PATCH 3/6] Update Modules/_sqlite/module.c Co-authored-by: Victor Stinner --- 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 835fba07752851..eb551083bb5f38 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -364,7 +364,7 @@ do { \ } \ res = PyModule_AddObjectRef(module, name, exc); \ Py_DECREF(exc); \ - if (res == -1) { \ + if (res < 0) { \ goto error; \ } \ } while (0) From cdae05e1d39df9eee3bcc9516043e24bde173a52 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 4 Nov 2020 18:58:33 +0100 Subject: [PATCH 4/6] Address review comment: Remove useless incref/decref --- Modules/_sqlite/module.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index eb551083bb5f38..982286afead20b 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -418,10 +418,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void) non-ASCII data and bytestrings to be returned for ASCII data. Now OptimizedUnicode is an alias for str, so it has no effect. */ - Py_INCREF((PyObject*)&PyUnicode_Type); - int res = PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); - Py_DECREF((PyObject*)&PyUnicode_Type); - if (res == -1) { + if (PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) { goto error; } From 911ee5a3d8d354d41cac21b4593bca1257cb5b39 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 4 Nov 2020 19:03:08 +0100 Subject: [PATCH 5/6] Address review comment: Use intermingled variable declarations --- Modules/_sqlite/microprotocols.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index 3291d696a38415..41f086791ea4b7 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -38,14 +38,12 @@ static PyObject *psyco_adapters = NULL; int pysqlite_microprotocols_init(PyObject *module) { - int res; - /* create adapters dictionary and put it in module namespace */ if ((psyco_adapters = PyDict_New()) == NULL) { return -1; } - res = PyModule_AddObjectRef(module, "adapters", psyco_adapters); + int res = PyModule_AddObjectRef(module, "adapters", psyco_adapters); Py_DECREF(psyco_adapters); return res; From a4bb4bff76c1db364b1042c6191c6ac2a0004bd1 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 4 Nov 2020 19:06:32 +0100 Subject: [PATCH 6/6] Address review comment: Use intermingled variable declarations again --- Modules/_sqlite/module.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 982286afead20b..9fdf51417ed883 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -357,12 +357,11 @@ do { \ #define ADD_EXCEPTION(module, name, exc, base) \ do { \ - int res; \ exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \ if (!exc) { \ goto error; \ } \ - res = PyModule_AddObjectRef(module, name, exc); \ + int res = PyModule_AddObjectRef(module, name, exc); \ Py_DECREF(exc); \ if (res < 0) { \ goto error; \