[Python-checkins] bpo-42064: Convert `sqlite3` global state to module state (GH-29073)

encukou webhook-mailer at python.org
Wed Oct 27 07:12:26 EDT 2021


https://github.com/python/cpython/commit/8f24b7dbcbd83311dad510863d8cb41f0e91b464
commit: 8f24b7dbcbd83311dad510863d8cb41f0e91b464
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: encukou <encukou at gmail.com>
date: 2021-10-27T13:12:21+02:00
summary:

bpo-42064: Convert `sqlite3` global state to module state (GH-29073)

files:
M Modules/_sqlite/clinic/cursor.c.h
M Modules/_sqlite/connection.c
M Modules/_sqlite/cursor.c
M Modules/_sqlite/microprotocols.c
M Modules/_sqlite/microprotocols.h
M Modules/_sqlite/module.c
M Modules/_sqlite/module.h
M Modules/_sqlite/row.c

diff --git a/Modules/_sqlite/clinic/cursor.c.h b/Modules/_sqlite/clinic/cursor.c.h
index eb3e7ce31ada3..d8a36ac38aaf0 100644
--- a/Modules/_sqlite/clinic/cursor.c.h
+++ b/Modules/_sqlite/clinic/cursor.c.h
@@ -279,25 +279,14 @@ PyDoc_STRVAR(pysqlite_cursor_close__doc__,
 "Closes the cursor.");
 
 #define PYSQLITE_CURSOR_CLOSE_METHODDEF    \
-    {"close", (PyCFunction)(void(*)(void))pysqlite_cursor_close, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_cursor_close__doc__},
+    {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, pysqlite_cursor_close__doc__},
 
 static PyObject *
-pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls);
+pysqlite_cursor_close_impl(pysqlite_Cursor *self);
 
 static PyObject *
-pysqlite_cursor_close(pysqlite_Cursor *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
 {
-    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_cursor_close_impl(self, cls);
-
-exit:
-    return return_value;
+    return pysqlite_cursor_close_impl(self);
 }
-/*[clinic end generated code: output=3b5328c1619b7626 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=514f6eb4e4974671 input=a9049054013a1b77]*/
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index e94c4cbb4e8c3..da2f12e8f99c2 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -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
 
@@ -416,7 +416,8 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
     }
 
     if (!self->initialized) {
-        pysqlite_state *state = pysqlite_get_state(NULL);
+        PyTypeObject *tp = Py_TYPE(self);
+        pysqlite_state *state = pysqlite_get_state_by_type(tp);
         PyErr_SetString(state->ProgrammingError,
                         "Base Connection.__init__ not called.");
         return NULL;
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index ca74a68de4dba..1d7c0b46a616d 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -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
 
@@ -966,17 +966,16 @@ pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
 /*[clinic input]
 _sqlite3.Cursor.close as pysqlite_cursor_close
 
-    cls: defining_class
-
 Closes the cursor.
 [clinic start generated code]*/
 
 static PyObject *
-pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
-/*[clinic end generated code: output=a08ab3d772f45438 input=28ba9b532ab46ba0]*/
+pysqlite_cursor_close_impl(pysqlite_Cursor *self)
+/*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
 {
     if (!self->connection) {
-        pysqlite_state *state = pysqlite_get_state_by_cls(cls);
+        PyTypeObject *tp = Py_TYPE(self);
+        pysqlite_state *state = pysqlite_get_state_by_type(tp);
         PyErr_SetString(state->ProgrammingError,
                         "Base Cursor.__init__ not called.");
         return NULL;
diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c
index 68e4f7fb166db..95c799d306f30 100644
--- a/Modules/_sqlite/microprotocols.c
+++ b/Modules/_sqlite/microprotocols.c
@@ -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;
@@ -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);
 
diff --git a/Modules/_sqlite/microprotocols.h b/Modules/_sqlite/microprotocols.h
index d12bc448596c4..6bde9d01f4529 100644
--- a/Modules/_sqlite/microprotocols.h
+++ b/Modules/_sqlite/microprotocols.h
@@ -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);
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 47b1f7a9d0720..e41ac0fc1ab73 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -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
 
@@ -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
@@ -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;
     }
@@ -395,16 +393,11 @@ static int add_integer_constants(PyObject *module) {
     return ret;
 }
 
-static struct PyModuleDef _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_methods,
 };
 
 #define ADD_TYPE(module, type)                 \
diff --git a/Modules/_sqlite/module.h b/Modules/_sqlite/module.h
index c273c1f9ed9f2..1d319f1ed5541 100644
--- a/Modules/_sqlite/module.h
+++ b/Modules/_sqlite/module.h
@@ -63,22 +63,20 @@ 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;
 }
 
+extern struct PyModuleDef _sqlite3module;
 static inline pysqlite_state *
-pysqlite_get_state_by_cls(PyTypeObject *Py_UNUSED(cls))
+pysqlite_get_state_by_type(PyTypeObject *tp)
 {
-    return &pysqlite_global_state;  // Replace with PyType_GetModuleState
-}
-
-static inline pysqlite_state *
-pysqlite_get_state_by_type(PyTypeObject *Py_UNUSED(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);
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
index b146c9dc5e3bd..1a1943285ce00 100644
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -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
 
@@ -219,7 +219,7 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
     if (opid != Py_EQ && opid != Py_NE)
         Py_RETURN_NOTIMPLEMENTED;
 
-    pysqlite_state *state = pysqlite_get_state_by_cls(Py_TYPE(self));
+    pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
     if (PyObject_TypeCheck(_other, state->RowType)) {
         pysqlite_Row *other = (pysqlite_Row *)_other;
         int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);



More information about the Python-checkins mailing list