8000 bpo-40737: Fix possible reference leak for sqlite3 initialization by erlend-aasland · Pull Request #20323 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40737: Fix possible reference leak for sqlite3 initialization #20323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 26, 2020
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix possible reference leak for :mod:`sqlite3` initialization.
14 changes: 2 additions & 12 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 23 additions & 19 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,34 +346,38 @@ 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;
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;
}

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);
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);
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;
Expand Down
3 changes: 1 addition & 2 deletions Modules/_sqlite/prepare_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/prepare_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ typedef struct _Row

extern PyTypeObject pysqlite_RowType;

int pysqlite_row_setup_types(void);
void pysqlite_row_setup_types(void);

#endif
3 changes: 1 addition & 2 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
0