Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix possible reference leak for :mod:`sqlite3` initialization.
20 changes: 12 additions & 8 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ static struct PyModuleDef _sqlite3module = {
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)
{
PyObject *module, *dict;
Expand All @@ -366,14 +374,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_TYPE(module, pysqlite_ConnectionType);
ADD_TYPE(module, pysqlite_CursorType);
ADD_TYPE(module, pysqlite_PrepareProtocolType);
ADD_TYPE(module, pysqlite_RowType);

if (!(dict = PyModule_GetDict(module))) {
goto error;
Expand Down