bpo-41861: Convert _sqlite3 CursorType and ConnectionType to heap types (GH-22478)

This commit is contained in:
Erlend Egeberg Aasland 2020-10-01 16:03:21 +02:00 committed by GitHub
parent 9031bd4fa4
commit 256e54acdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 102 deletions

View File

@ -220,6 +220,8 @@ void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset
void pysqlite_connection_dealloc(pysqlite_Connection* self) void pysqlite_connection_dealloc(pysqlite_Connection* self)
{ {
PyTypeObject *tp = Py_TYPE(self);
Py_XDECREF(self->statement_cache); Py_XDECREF(self->statement_cache);
/* Clean up if user has not called .close() explicitly. */ /* Clean up if user has not called .close() explicitly. */
@ -236,7 +238,9 @@ void pysqlite_connection_dealloc(pysqlite_Connection* self)
Py_XDECREF(self->collations); Py_XDECREF(self->collations);
Py_XDECREF(self->statements); Py_XDECREF(self->statements);
Py_XDECREF(self->cursors); Py_XDECREF(self->cursors);
Py_TYPE(self)->tp_free((PyObject*)self);
tp->tp_free(self);
Py_DECREF(tp);
} }
/* /*
@ -281,13 +285,13 @@ PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args,
} }
if (factory == NULL) { if (factory == NULL) {
factory = (PyObject*)&pysqlite_CursorType; factory = (PyObject*)pysqlite_CursorType;
} }
cursor = PyObject_CallOneArg(factory, (PyObject *)self); cursor = PyObject_CallOneArg(factory, (PyObject *)self);
if (cursor == NULL) if (cursor == NULL)
return NULL; return NULL;
if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) { if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"factory must return a cursor, not %.100s", "factory must return a cursor, not %.100s",
Py_TYPE(cursor)->tp_name); Py_TYPE(cursor)->tp_name);
@ -1494,7 +1498,7 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL}; static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords, if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
&pysqlite_ConnectionType, &target, pysqlite_ConnectionType, &target,
&pages, &progress, &name, &sleep_obj)) { &pages, &progress, &name, &sleep_obj)) {
return NULL; return NULL;
} }
@ -1831,50 +1835,32 @@ static struct PyMemberDef connection_members[] =
{NULL} {NULL}
}; };
PyTypeObject pysqlite_ConnectionType = { static PyType_Slot connection_slots[] = {
PyVarObject_HEAD_INIT(NULL, 0) {Py_tp_dealloc, pysqlite_connection_dealloc},
MODULE_NAME ".Connection", /* tp_name */ {Py_tp_doc, (void *)connection_doc},
sizeof(pysqlite_Connection), /* tp_basicsize */ {Py_tp_methods, connection_methods},
0, /* tp_itemsize */ {Py_tp_members, connection_members},
(destructor)pysqlite_connection_dealloc, /* tp_dealloc */ {Py_tp_getset, connection_getset},
0, /* tp_vectorcall_offset */ {Py_tp_new, PyType_GenericNew},
0, /* tp_getattr */ {Py_tp_init, pysqlite_connection_init},
0, /* tp_setattr */ {Py_tp_call, pysqlite_connection_call},
0, /* tp_as_async */ {0, NULL},
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc)pysqlite_connection_call, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
connection_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
connection_methods, /* tp_methods */
connection_members, /* tp_members */
connection_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)pysqlite_connection_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0 /* tp_free */
}; };
extern int pysqlite_connection_setup_types(void) static PyType_Spec connection_spec = {
.name = MODULE_NAME ".Connection",
.basicsize = sizeof(pysqlite_Connection),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = connection_slots,
};
PyTypeObject *pysqlite_ConnectionType = NULL;
extern int pysqlite_connection_setup_types(PyObject *module)
{ {
pysqlite_ConnectionType.tp_new = PyType_GenericNew; pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
return PyType_Ready(&pysqlite_ConnectionType); if (pysqlite_ConnectionType == NULL) {
return -1;
}
return 0;
} }

View File

@ -106,7 +106,7 @@ typedef struct
PyObject* NotSupportedError; PyObject* NotSupportedError;
} pysqlite_Connection; } pysqlite_Connection;
extern PyTypeObject pysqlite_ConnectionType; extern PyTypeObject *pysqlite_ConnectionType;
PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware); PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
void pysqlite_connection_dealloc(pysqlite_Connection* self); void pysqlite_connection_dealloc(pysqlite_Connection* self);
@ -122,6 +122,6 @@ int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObjec
int pysqlite_check_thread(pysqlite_Connection* self); int pysqlite_check_thread(pysqlite_Connection* self);
int pysqlite_check_connection(pysqlite_Connection* con); int pysqlite_check_connection(pysqlite_Connection* con);
int pysqlite_connection_setup_types(void); int pysqlite_connection_setup_types(PyObject *module);
#endif #endif

View File

@ -33,7 +33,7 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
{ {
pysqlite_Connection* connection; pysqlite_Connection* connection;
if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection)) if (!PyArg_ParseTuple(args, "O!", pysqlite_ConnectionType, &connection))
{ {
return -1; return -1;
} }
@ -74,6 +74,8 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
static void pysqlite_cursor_dealloc(pysqlite_Cursor* self) static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
{ {
PyTypeObject *tp = Py_TYPE(self);
/* Reset the statement if the user has not closed the cursor */ /* Reset the statement if the user has not closed the cursor */
if (self->statement) { if (self->statement) {
pysqlite_statement_reset(self->statement); pysqlite_statement_reset(self->statement);
@ -91,7 +93,8 @@ static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
PyObject_ClearWeakRefs((PyObject*)self); PyObject_ClearWeakRefs((PyObject*)self);
} }
Py_TYPE(self)->tp_free((PyObject*)self); tp->tp_free(self);
Py_DECREF(tp);
} }
static PyObject * static PyObject *
@ -898,56 +901,39 @@ static struct PyMemberDef cursor_members[] =
{"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY}, {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
{"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY}, {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
{"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
{"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Cursor, in_weakreflist), READONLY},
{NULL} {NULL}
}; };
static const char cursor_doc[] = static const char cursor_doc[] =
PyDoc_STR("SQLite database cursor class."); PyDoc_STR("SQLite database cursor class.");
PyTypeObject pysqlite_CursorType = { static PyType_Slot cursor_slots[] = {
PyVarObject_HEAD_INIT(NULL, 0) {Py_tp_dealloc, pysqlite_cursor_dealloc},
MODULE_NAME ".Cursor", /* tp_name */ {Py_tp_doc, (void *)cursor_doc},
sizeof(pysqlite_Cursor), /* tp_basicsize */ {Py_tp_iter, PyObject_SelfIter},
0, /* tp_itemsize */ {Py_tp_iternext, pysqlite_cursor_iternext},
(destructor)pysqlite_cursor_dealloc, /* tp_dealloc */ {Py_tp_methods, cursor_methods},
0, /* tp_vectorcall_offset */ {Py_tp_members, cursor_members},
0, /* tp_getattr */ {Py_tp_new, PyType_GenericNew},
0, /* tp_setattr */ {Py_tp_init, pysqlite_cursor_init},
0, /* tp_as_async */ {0, NULL},
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
cursor_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
cursor_methods, /* tp_methods */
cursor_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)pysqlite_cursor_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0 /* tp_free */
}; };
extern int pysqlite_cursor_setup_types(void) static PyType_Spec cursor_spec = {
.name = MODULE_NAME ".Cursor",
.basicsize = sizeof(pysqlite_Cursor),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = cursor_slots,
};
PyTypeObject *pysqlite_CursorType = NULL;
extern int pysqlite_cursor_setup_types(PyObject *module)
{ {
pysqlite_CursorType.tp_new = PyType_GenericNew; pysqlite_CursorType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &cursor_spec, NULL);
return PyType_Ready(&pysqlite_CursorType); if (pysqlite_CursorType == NULL) {
return -1;
}
return 0;
} }

View File

@ -52,7 +52,7 @@ typedef struct
PyObject* in_weakreflist; /* List of weak references */ PyObject* in_weakreflist; /* List of weak references */
} pysqlite_Cursor; } pysqlite_Cursor;
extern PyTypeObject pysqlite_CursorType; extern PyTypeObject *pysqlite_CursorType;
PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args); PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args);
PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args); PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args);
@ -64,7 +64,7 @@ PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args);
PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args); PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args);
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args); PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args);
int pysqlite_cursor_setup_types(void); int pysqlite_cursor_setup_types(PyObject *module);
#define UNKNOWN (-1) #define UNKNOWN (-1)
#endif #endif

View File

@ -82,7 +82,7 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
} }
if (factory == NULL) { if (factory == NULL) {
factory = (PyObject*)&pysqlite_ConnectionType; factory = (PyObject*)pysqlite_ConnectionType;
} }
if (PySys_Audit("sqlite3.connect", "O", database) < 0) { if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
@ -353,8 +353,8 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
if (!module || if (!module ||
(pysqlite_row_setup_types(module) < 0) || (pysqlite_row_setup_types(module) < 0) ||
(pysqlite_cursor_setup_types() < 0) || (pysqlite_cursor_setup_types(module) < 0) ||
(pysqlite_connection_setup_types() < 0) || (pysqlite_connection_setup_types(module) < 0) ||
(pysqlite_cache_setup_types(module) < 0) || (pysqlite_cache_setup_types(module) < 0) ||
(pysqlite_statement_setup_types(module) < 0) || (pysqlite_statement_setup_types(module) < 0) ||
(pysqlite_prepare_protocol_setup_types(module) < 0) (pysqlite_prepare_protocol_setup_types(module) < 0)
@ -363,8 +363,8 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
return NULL; return NULL;
} }
ADD_TYPE(module, pysqlite_ConnectionType); ADD_TYPE(module, *pysqlite_ConnectionType);
ADD_TYPE(module, pysqlite_CursorType); ADD_TYPE(module, *pysqlite_CursorType);
ADD_TYPE(module, *pysqlite_PrepareProtocolType); ADD_TYPE(module, *pysqlite_PrepareProtocolType);
ADD_TYPE(module, *pysqlite_RowType); ADD_TYPE(module, *pysqlite_RowType);

View File

@ -49,7 +49,7 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
return NULL; return NULL;
if (!PyObject_TypeCheck((PyObject*)cursor, &pysqlite_CursorType)) { if (!PyObject_TypeCheck((PyObject*)cursor, pysqlite_CursorType)) {
PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument"); PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
return NULL; return NULL;
} }