Skip to content

Commit 8f24b7d

Browse files
author
Erlend Egeberg Aasland
authored
bpo-42064: Convert sqlite3 global state to module state (GH-29073)
1 parent 82a662e commit 8f24b7d

File tree

8 files changed

+36
-55
lines changed

8 files changed

+36
-55
lines changed

Modules/_sqlite/clinic/cursor.c.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -279,25 +279,14 @@ PyDoc_STRVAR(pysqlite_cursor_close__doc__,
279279
"Closes the cursor.");
280280

281281
#define PYSQLITE_CURSOR_CLOSE_METHODDEF \
282-
{"close", (PyCFunction)(void(*)(void))pysqlite_cursor_close, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_cursor_close__doc__},
282+
{"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, pysqlite_cursor_close__doc__},
283283

284284
static PyObject *
285-
pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls);
285+
pysqlite_cursor_close_impl(pysqlite_Cursor *self);
286286

287287
static PyObject *
288-
pysqlite_cursor_close(pysqlite_Cursor *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
288+
pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
289289
{
290-
PyObject *return_value = NULL;
291-
static const char * const _keywords[] = { NULL};
292-
static _PyArg_Parser _parser = {":close", _keywords, 0};
293-
294-
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser
295-
)) {
296-
goto exit;
297-
}
298-
return_value = pysqlite_cursor_close_impl(self, cls);
299-
300-
exit:
301-
return return_value;
290+
return pysqlite_cursor_close_impl(self);
302291
}
303-
/*[clinic end generated code: output=3b5328c1619b7626 input=a9049054013a1b77]*/
292+
/*[clinic end generated code: output=514f6eb4e4974671 input=a9049054013a1b77]*/

Modules/_sqlite/connection.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ clinic_fsconverter(PyObject *pathlike, const char **result)
5959
return 0;
6060
}
6161

62-
#define clinic_state() (pysqlite_get_state(NULL))
62+
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
6363
#include "clinic/connection.c.h"
6464
#undef clinic_state
6565

@@ -416,7 +416,8 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
416416
}
417417

418418
if (!self->initialized) {
419-
pysqlite_state *state = pysqlite_get_state(NULL);
419+
PyTypeObject *tp = Py_TYPE(self);
420+
pysqlite_state *state = pysqlite_get_state_by_type(tp);
420421
PyErr_SetString(state->ProgrammingError,
421422
"Base Connection.__init__ not called.");
422423
return NULL;

Modules/_sqlite/cursor.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "module.h"
2626
#include "util.h"
2727

28-
#define clinic_state() (pysqlite_get_state(NULL))
28+
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
2929
#include "clinic/cursor.c.h"
3030
#undef clinic_state
3131

@@ -966,17 +966,16 @@ pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
966966
/*[clinic input]
967967
_sqlite3.Cursor.close as pysqlite_cursor_close
968968
969-
cls: defining_class
970-
971969
Closes the cursor.
972970
[clinic start generated code]*/
973971

974972
static PyObject *
975-
pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
976-
/*[clinic end generated code: output=a08ab3d772f45438 input=28ba9b532ab46ba0]*/
973+
pysqlite_cursor_close_impl(pysqlite_Cursor *self)
974+
/*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
977975
{
978976
if (!self->connection) {
979-
pysqlite_state *state = pysqlite_get_state_by_cls(cls);
977+
PyTypeObject *tp = Py_TYPE(self);
978+
pysqlite_state *state = pysqlite_get_state_by_type(tp);
980979
PyErr_SetString(state->ProgrammingError,
981980
"Base Cursor.__init__ not called.");
982981
return NULL;

Modules/_sqlite/microprotocols.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pysqlite_microprotocols_init(PyObject *module)
4949
/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
5050

5151
int
52-
pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
52+
pysqlite_microprotocols_add(pysqlite_state *state, PyTypeObject *type,
53+
PyObject *proto, PyObject *cast)
5354
{
5455
PyObject* key;
5556
int rc;
@@ -61,7 +62,6 @@ pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
6162
return -1;
6263
}
6364

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

Modules/_sqlite/microprotocols.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333

3434
/* used by module.c to init the microprotocols system */
3535
extern int pysqlite_microprotocols_init(PyObject *module);
36-
extern int pysqlite_microprotocols_add(
37-
PyTypeObject *type, PyObject *proto, PyObject *cast);
36+
extern int pysqlite_microprotocols_add(pysqlite_state *state,
37+
PyTypeObject *type, PyObject *proto,
38+
PyObject *cast);
3839
extern PyObject *pysqlite_microprotocols_adapt(pysqlite_state *state,
3940
PyObject *obj, PyObject *proto,
4041
PyObject *alt);

Modules/_sqlite/module.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#error "SQLite 3.7.15 or higher required"
3333
#endif
3434

35-
#define clinic_state() (pysqlite_get_state(NULL))
35+
#define clinic_state() (pysqlite_get_state(module))
3636
#include "clinic/module.c.h"
3737
#undef clinic_state
3838

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

44-
pysqlite_state pysqlite_global_state;
45-
4644
// NOTE: This must equal sqlite3.Connection.__init__ argument spec!
4745
/*[clinic input]
4846
_sqlite3.connect as pysqlite_connect
@@ -160,7 +158,7 @@ pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
160158

161159
pysqlite_state *state = pysqlite_get_state(module);
162160
PyObject *protocol = (PyObject *)state->PrepareProtocolType;
163-
rc = pysqlite_microprotocols_add(type, protocol, caster);
161+
rc = pysqlite_microprotocols_add(state, type, protocol, caster);
164162
if (rc == -1) {
165163
return NULL;
166164
}
@@ -395,16 +393,11 @@ static int add_integer_constants(PyObject *module) {
395393
return ret;
396394
}
397395

398-
static struct PyModuleDef _sqlite3module = {
399-
PyModuleDef_HEAD_INIT,
400-
"_sqlite3",
401-
NULL,
402-
-1,
403-
module_methods,
404-
NULL,
405-
NULL,
406-
NULL,
407-
NULL
396+
struct PyModuleDef _sqlite3module = {
397+
.m_base = PyModuleDef_HEAD_INIT,
398+
.m_name = "_sqlite3",
399+
.m_size = sizeof(pysqlite_state),
400+
.m_methods = module_methods,
408401
};
409402

410403
#define ADD_TYPE(module, type) \

Modules/_sqlite/module.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,20 @@ typedef struct {
6363
extern pysqlite_state pysqlite_global_state;
6464

6565
static inline pysqlite_state *
66-
pysqlite_get_state(PyObject *Py_UNUSED(module))
66+
pysqlite_get_state(PyObject *module)
6767
{
68-
return &pysqlite_global_state; // Replace with PyModule_GetState
68+
pysqlite_state *state = (pysqlite_state *)PyModule_GetState(module);
69+
assert(state != NULL);
70+
return state;
6971
}
7072

73+
extern struct PyModuleDef _sqlite3module;
7174
static inline pysqlite_state *
72-
pysqlite_get_state_by_cls(PyTypeObject *Py_UNUSED(cls))
75+
pysqlite_get_state_by_type(PyTypeObject *tp)
7376
{
74-
return &pysqlite_global_state; // Replace with PyType_GetModuleState
75-
}
76-
77-
static inline pysqlite_state *
78-
pysqlite_get_state_by_type(PyTypeObject *Py_UNUSED(tp))
79-
{
80-
// Replace with _PyType_GetModuleByDef & PyModule_GetState
81-
return &pysqlite_global_state;
77+
PyObject *module = _PyType_GetModuleByDef(tp, &_sqlite3module);
78+
assert(module != NULL);
79+
return pysqlite_get_state(module);
8280
}
8381

8482
extern const char *pysqlite_error_name(int rc);

Modules/_sqlite/row.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "row.h"
2525
#include "cursor.h"
2626

27-
#define clinic_state() (pysqlite_get_state(NULL))
27+
#define clinic_state() (pysqlite_get_state_by_type(type))
2828
#include "clinic/row.c.h"
2929
#undef clinic_state
3030

@@ -219,7 +219,7 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
219219
if (opid != Py_EQ && opid != Py_NE)
220220
Py_RETURN_NOTIMPLEMENTED;
221221

222-
pysqlite_state *state = pysqlite_get_state_by_cls(Py_TYPE(self));
222+
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
223223
if (PyObject_TypeCheck(_other, state->RowType)) {
224224
pysqlite_Row *other = (pysqlite_Row *)_other;
225225
int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);

0 commit comments

Comments
 (0)