8000 bpo-42064: Convert `sqlite3` global state to module state by erlend-aasland · Pull Request #29073 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-42064: Convert sqlite3 global state to module state #29073

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 4 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-42064: Convert global state to module state
  • Loading branch information
Erlend E. Aasland committed Oct 19, 2021
commit 99cdaff3a9b3142b22ad428287595bf7c06fbf3c
21 changes: 16 additions & 5 deletions Modules/_sqlite/clinic/connection.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,26 @@ PyDoc_STRVAR(pysqlite_connection_close__doc__,
"Closes the connection.");

#define PYSQLITE_CONNECTION_CLOSE_METHODDEF \
{"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS, pysqlite_connection_close__doc__},
{"close", (PyCFunction)(void(*)(void))pysqlite_connection_close, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_close__doc__},

static PyObject *
pysqlite_connection_close_impl(pysqlite_Connection *self);
pysqlite_connection_close_impl(pysqlite_Connection *self, PyTypeObject *cls);

static PyObject *
pysqlite_connection_close(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored))
pysqlite_connection_close(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
return pysqlite_connection_close_impl(self);
PyObject *return_value = NULL;
static const char * const _keywords[] = { NULL};
static _PyArg_Parser _parser = {":close", _keywords, 0};

if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser
)) {
goto exit;
}
return_value = pysqlite_connection_close_impl(self, cls);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_connection_commit__doc__,
Expand Down Expand Up @@ -757,4 +768,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
#ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
#define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
#endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */
/*[clinic end generated code: output=7567e5d716309258 input=a9049054013a1b77]*/
/*[clinic end generated code: output=2cd6449f4a8f9a2a input=a9049054013a1b77]*/
10 changes: 6 additions & 4 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ clinic_fsconverter(PyObject *pathlike, const char **result)
return 0;
}

#define clinic_state() (pysqlite_get_state(NULL))
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
#include "clinic/connection.c.h"
#undef clinic_state

Expand Down Expand Up @@ -404,19 +404,21 @@ pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
/*[clinic input]
_sqlite3.Connection.close as pysqlite_connection_close

cls: defining_class

Closes the connection.
[clinic start generated code]*/

static PyObject *
pysqlite_connection_close_impl(pysqlite_Connection *self)
/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
pysqlite_connection_close_impl(pysqlite_Connection *self, PyTypeObject *cls)
/*[clinic end generated code: output=981f0a726752b78a input=16141a7506e49f33]*/
{
if (!pysqlite_check_thread(self)) {
return NULL;
}

if (!self->initialized) {
pysqlite_state *state = pysqlite_get_state(NULL);
pysqlite_state *state = pysqlite_get_state_by_cls(cls);
PyErr_SetString(state->ProgrammingError,
"Base Connection.__init__ not called.");
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "module.h"
#include "util.h"

#define clinic_state() (pysqlite_get_state(NULL))
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
#include "clinic/cursor.c.h"
#undef clinic_state

Expand Down
4 changes: 2 additions & 2 deletions Modules/_sqlite/microprotocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pysqlite_microprotocols_init(PyObject *module)
/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */

int
pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
pysqlite_microprotocols_add(pysqlite_state *state, PyTypeObject *type,
PyObject *proto, PyObject *cast)
{
PyObject* key;
int rc;
Expand All @@ -61,7 +62,6 @@ pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
return -1;
}

pysqlite_state *state = pysqlite_get_state(NULL);
rc = PyDict_SetItem(state->psyco_adapters, key, cast);
Py_DECREF(key);

Expand Down
5 changes: 3 additions & 2 deletions Modules/_sqlite/microprotocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

/* used by module.c to init the microprotocols system */
extern int pysqlite_microprotocols_init(PyObject *module);
extern int pysqlite_microprotocols_add(
PyTypeObject *type, PyObject *proto, PyObject *cast);
extern int pysqlite_microprotocols_add(pysqlite_state *state,
PyTypeObject *type, PyObject *proto,
PyObject *cast);
extern PyObject *pysqlite_microprotocols_adapt(pysqlite_state *state,
PyObject *obj, PyObject *proto,
PyObject *alt);
Expand Down
21 changes: 7 additions & 14 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#error "SQLite 3.7.15 or higher required"
#endif

#define clinic_state() (pysqlite_get_state(NULL))
#define clinic_state() (pysqlite_get_state(module))
#include "clinic/module.c.h"
#undef clinic_state

Expand All @@ -41,8 +41,6 @@ module _sqlite3
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/

pysqlite_state pysqlite_global_state;

// NOTE: This must equal sqlite3.Connection.__init__ argument spec!
/*[clinic input]
_sqlite3.connect as pysqlite_connect
Expand Down Expand Up @@ -160,7 +158,7 @@ pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,

pysqlite_state *state = pysqlite_get_state(module);
PyObject *protocol = (PyObject *)state->PrepareProtocolType;
rc = pysqlite_microprotocols_add(type, protocol, caster);
rc = pysqlite_microprotocols_add(state, type, protocol, caster);
if (rc == -1) {
return NULL;
}
Expand Down Expand Up @@ -395,16 +393,11 @@ static int add_integer_constants(PyObject *module) {
return ret;
}

static struct PyModuleDe 1E0A f _sqlite3module = {
PyModuleDef_HEAD_INIT,
"_sqlite3",
NULL,
-1,
module_methods,
NULL,
NULL,
NULL,
NULL
struct PyModuleDef _sqlite3module = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_sqlite3",
.m_size = sizeof(pysqlite_state),
.m_methods = module_ F438 methods,
};

#define ADD_TYPE(module, type) \
Expand Down
20 changes: 13 additions & 7 deletions Modules/_sqlite/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,28 @@ typedef struct {
extern pysqlite_state pysqlite_global_state;

static inline pysqlite_state *
pysqlite_get_state(PyObject *Py_UNUSED(module))
pysqlite_get_state(PyObject *module)
{
return &pysqlite_global_state; // Replace with PyModule_GetState
pysqlite_state *state = (pysqlite_state *)PyModule_GetState(module);
assert(state != NULL);
return state;
}

static inline pysqlite_state *
pysqlite_get_state_by_cls(PyTypeObject *Py_UNUSED(cls))
pysqlite_get_state_by_cls(PyTypeObject *cls)
{
return &pysqlite_global_state; // Replace with PyType_GetModuleState
pysqlite_state *state = (pysqlite_state *)PyType_GetModuleState(cls);
assert(state != NULL);
return state;
}

struct PyModuleDef _sqlite3module;
static inline pysqlite_state *
pysqlite_get_state_by_type(PyTypeObject *Py_UNUSED(tp))
pysqlite_get_state_by_type(PyTypeObject *tp)
{
// Replace with _PyType_GetModuleByDef & PyModule_GetState
return &pysqlite_global_state;
PyObject *module = _PyType_GetModuleByDef(tp, &_sqlite3module);
assert(module != NULL);
return pysqlite_get_state(module);
}

extern const char *pysqlite_error_name(int rc);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "row.h"
#include "cursor.h"

#define clinic_state() (pysqlite_get_state(NULL))
#define clinic_state() (pysqlite_get_state_by_type(type))
#include "clinic/row.c.h"
#undef clinic_state

Expand Down
0