bpo-42064: Move sqlite3 types to global state (GH-26537)

* Move connection type to global state
* Move cursor type to global state
* Move prepare protocol type to global state
* Move row type to global state
* Move statement type to global state
* ADD_TYPE takes a pointer
* pysqlite_get_state is now static inline
This commit is contained in:
Erlend Egeberg Aasland 2021-06-15 14:47:34 +02:00 committed by GitHub
parent 8ebd9447e9
commit 10a5c806d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 103 additions and 70 deletions

View file

@ -32,7 +32,10 @@
#error "SQLite 3.7.15 or higher required"
#endif
#define clinic_state() (pysqlite_get_state(NULL))
#include "clinic/module.c.h"
#undef clinic_state
/*[clinic input]
module _sqlite3
[clinic start generated code]*/
@ -57,12 +60,6 @@ int pysqlite_BaseTypeAdapted = 0;
pysqlite_state pysqlite_global_state;
pysqlite_state *
pysqlite_get_state(PyObject *Py_UNUSED(module))
{
return &pysqlite_global_state;
}
static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
kwargs)
{
@ -93,7 +90,8 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
}
if (factory == NULL) {
factory = (PyObject*)pysqlite_ConnectionType;
pysqlite_state *state = pysqlite_get_state(self);
factory = (PyObject *)state->ConnectionType;
}
return PyObject_Call(factory, args, kwargs);
@ -176,9 +174,12 @@ pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
pysqlite_BaseTypeAdapted = 1;
}
rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
if (rc == -1)
pysqlite_state *state = pysqlite_get_state(NULL);
PyObject *protocol = (PyObject *)state->PrepareProtocolType;
rc = pysqlite_microprotocols_add(type, protocol, caster);
if (rc == -1) {
return NULL;
}
Py_RETURN_NONE;
}
@ -240,7 +241,7 @@ pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
_sqlite3.adapt as pysqlite_adapt
obj: object
proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType
proto: object(c_default='(PyObject *)clinic_state()->PrepareProtocolType') = PrepareProtocolType
alt: object = NULL
/
@ -250,7 +251,7 @@ Adapt given object to given protocol. Non-standard.
static PyObject *
pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
PyObject *alt)
/*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/
/*[clinic end generated code: output=0c3927c5fcd23dd9 input=c8995aeb25d0e542]*/
{
return pysqlite_microprotocols_adapt(obj, proto, alt);
}
@ -358,7 +359,7 @@ static struct PyModuleDef _sqlite3module = {
#define ADD_TYPE(module, type) \
do { \
if (PyModule_AddType(module, &type) < 0) { \
if (PyModule_AddType(module, type) < 0) { \
goto error; \
} \
} while (0)
@ -392,6 +393,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
}
module = PyModule_Create(&_sqlite3module);
pysqlite_state *state = pysqlite_get_state(module);
if (!module ||
(pysqlite_row_setup_types(module) < 0) ||
@ -403,10 +405,10 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
goto error;
}
ADD_TYPE(module, *pysqlite_ConnectionType);
ADD_TYPE(module, *pysqlite_CursorType);
ADD_TYPE(module, *pysqlite_PrepareProtocolType);
ADD_TYPE(module, *pysqlite_RowType);
ADD_TYPE(module, state->ConnectionType);
ADD_TYPE(module, state->CursorType);
ADD_TYPE(module, state->PrepareProtocolType);
ADD_TYPE(module, state->RowType);
/*** Create DB-API Exception hierarchy */
ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);