From 85b3d3d84bc703836460c49fa0d9ca3760c447ee Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 19 Oct 2020 00:04:56 +0200 Subject: [PATCH] Convert _sqlite3.Cache to Argument Clinic --- Modules/_sqlite/cache.c | 55 +++++++++++++++++------- Modules/_sqlite/cache.h | 2 - Modules/_sqlite/clinic/cache.c.h | 72 ++++++++++++++++++++++++++++++++ Modules/_sqlite/cursor.c | 6 ++- 4 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 Modules/_sqlite/clinic/cache.c.h diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 078a484b86cee6..7a52fd196277bb 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -24,6 +24,13 @@ #include "cache.h" #include +#include "clinic/cache.c.h" +/*[clinic input] +module _sqlite3 +class _sqlite3.Cache "pysqlite_Cache *" "pysqlite_CacheType" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1cb910d4c2696228]*/ + /* only used internally */ pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data) { @@ -54,17 +61,20 @@ void pysqlite_node_dealloc(pysqlite_Node* self) Py_DECREF(tp); } -int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs) -{ - PyObject* factory; - int size = 10; +/*[clinic input] +_sqlite3.Cache.__init__ as pysqlite_cache_init - self->factory = NULL; + factory: object + / + size: int = 10 - if (!PyArg_ParseTuple(args, "O|i", &factory, &size)) { - return -1; - } +Gets an entry from the cache or calls the factory function to produce one. +[clinic start generated code]*/ +static int +pysqlite_cache_init_impl(pysqlite_Cache *self, PyObject *factory, int size) +/*[clinic end generated code: output=3a3b3e0486364359 input=e289088a46c2f1cc]*/ +{ /* minimum cache size is 5 entries */ if (size < 5) { size = 5; @@ -113,7 +123,18 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self) Py_DECREF(tp); } -PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key) +/*[clinic input] +_sqlite3.Cache.get as pysqlite_cache_get + + key: object + / + +Gets an entry from the cache or calls the factory function to produce one. +[clinic start generated code]*/ + +static PyObject * +pysqlite_cache_get(pysqlite_Cache *self, PyObject *key) +/*[clinic end generated code: output=149ad799afafcdc8 input=07aef9c27e458441]*/ { pysqlite_Node* node; pysqlite_Node* ptr; @@ -217,7 +238,15 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key) return Py_NewRef(node->data); } -PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args) +/*[clinic input] +_sqlite3.Cache.display as pysqlite_cache_display + +For debugging only. +[clinic start generated code]*/ + +static PyObject * +pysqlite_cache_display_impl(pysqlite_Cache *self) +/*[clinic end generated code: output=4010f6d5a649271c input=916727d67499366c]*/ { pysqlite_Node* ptr; PyObject* prevkey; @@ -268,10 +297,8 @@ static PyType_Spec node_spec = { PyTypeObject *pysqlite_NodeType = NULL; static PyMethodDef cache_methods[] = { - {"get", (PyCFunction)pysqlite_cache_get, METH_O, - PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")}, - {"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS, - PyDoc_STR("For debugging only.")}, + PYSQLITE_CACHE_GET_METHODDEF + PYSQLITE_CACHE_DISPLAY_METHODDEF {NULL, NULL} }; diff --git a/Modules/_sqlite/cache.h b/Modules/_sqlite/cache.h index 4a1977fcd2cd23..c2bb41c6b10b92 100644 --- a/Modules/_sqlite/cache.h +++ b/Modules/_sqlite/cache.h @@ -62,8 +62,6 @@ typedef struct extern PyTypeObject *pysqlite_NodeType; extern PyTypeObject *pysqlite_CacheType; -PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args); - int pysqlite_cache_setup_types(PyObject *module); #endif diff --git a/Modules/_sqlite/clinic/cache.c.h b/Modules/_sqlite/clinic/cache.c.h new file mode 100644 index 00000000000000..38a13e0b8747ca --- /dev/null +++ b/Modules/_sqlite/clinic/cache.c.h @@ -0,0 +1,72 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(pysqlite_cache_init__doc__, +"Cache(factory, /, size=10)\n" +"--\n" +"\n" +"Gets an entry from the cache or calls the factory function to produce one."); + +static int +pysqlite_cache_init_impl(pysqlite_Cache *self, PyObject *factory, int size); + +static int +pysqlite_cache_init(PyObject *self, PyObject *args, PyObject *kwargs) +{ + int return_value = -1; + static const char * const _keywords[] = {"", "size", NULL}; + static _PyArg_Parser _parser = {NULL, _keywords, "Cache", 0}; + PyObject *argsbuf[2]; + PyObject * const *fastargs; + Py_ssize_t nargs = PyTuple_GET_SIZE(args); + Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + PyObject *factory; + int size = 10; + + fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); + if (!fastargs) { + goto exit; + } + factory = fastargs[0]; + if (!noptargs) { + goto skip_optional_pos; + } + size = _PyLong_AsInt(fastargs[1]); + if (size == -1 && PyErr_Occurred()) { + goto exit; + } +skip_optional_pos: + return_value = pysqlite_cache_init_impl((pysqlite_Cache *)self, factory, size); + +exit: + return return_value; +} + +PyDoc_STRVAR(pysqlite_cache_get__doc__, +"get($self, key, /)\n" +"--\n" +"\n" +"Gets an entry from the cache or calls the factory function to produce one."); + +#define PYSQLITE_CACHE_GET_METHODDEF \ + {"get", (PyCFunction)pysqlite_cache_get, METH_O, pysqlite_cache_get__doc__}, + +PyDoc_STRVAR(pysqlite_cache_display__doc__, +"display($self, /)\n" +"--\n" +"\n" +"For debugging only."); + +#define PYSQLITE_CACHE_DISPLAY_METHODDEF \ + {"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS, pysqlite_cache_display__doc__}, + +static PyObject * +pysqlite_cache_display_impl(pysqlite_Cache *self); + +static PyObject * +pysqlite_cache_display(pysqlite_Cache *self, PyObject *Py_UNUSED(ignored)) +{ + return pysqlite_cache_display_impl(self); +} +/*[clinic end generated code: output=324d7a1f2235e360 input=a9049054013a1b77]*/ diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 296d569148f8ae..fc696487522a57 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -460,8 +460,10 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation (void)pysqlite_statement_reset(self->statement); } - Py_XSETREF(self->statement, - (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args)); + _Py_IDENTIFIER(get); + PyObject *stmt_cache = (PyObject *)self->connection->statement_cache; + PyObject *stmt = _PyObject_CallMethodIdOneArg(stmt_cache, &PyId_get, func_args); + Py_XSETREF(self->statement, (pysqlite_Statement *)stmt); Py_DECREF(func_args); if (!self->statement) {