Skip to content

Commit 85b3d3d

Browse files
author
Erlend E. Aasland
committed
Convert _sqlite3.Cache to Argument Clinic
1 parent 203b249 commit 85b3d3d

File tree

4 files changed

+117
-18
lines changed

4 files changed

+117
-18
lines changed

Modules/_sqlite/cache.c

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
#include "cache.h"
2525
#include <limits.h>
2626

27+
#include "clinic/cache.c.h"
28+
/*[clinic input]
29+
module _sqlite3
30+
class _sqlite3.Cache "pysqlite_Cache *" "pysqlite_CacheType"
31+
[clinic start generated code]*/
32+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1cb910d4c2696228]*/
33+
2734
/* only used internally */
2835
pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
2936
{
@@ -54,17 +61,20 @@ void pysqlite_node_dealloc(pysqlite_Node* self)
5461
Py_DECREF(tp);
5562
}
5663

57-
int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
58-
{
59-
PyObject* factory;
60-
int size = 10;
64+
/*[clinic input]
65+
_sqlite3.Cache.__init__ as pysqlite_cache_init
6166
62-
self->factory = NULL;
67+
factory: object
68+
/
69+
size: int = 10
6370
64-
if (!PyArg_ParseTuple(args, "O|i", &factory, &size)) {
65-
return -1;
66-
}
71+
Gets an entry from the cache or calls the factory function to produce one.
72+
[clinic start generated code]*/
6773

74+
static int
75+
pysqlite_cache_init_impl(pysqlite_Cache *self, PyObject *factory, int size)
76+
/*[clinic end generated code: output=3a3b3e0486364359 input=e289088a46c2f1cc]*/
77+
{
6878
/* minimum cache size is 5 entries */
6979
if (size < 5) {
7080
size = 5;
@@ -113,7 +123,18 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self)
113123
Py_DECREF(tp);
114124
}
115125

116-
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
126+
/*[clinic input]
127+
_sqlite3.Cache.get as pysqlite_cache_get
128+
129+
key: object
130+
/
131+
132+
Gets an entry from the cache or calls the factory function to produce one.
133+
[clinic start generated code]*/
134+
135+
static PyObject *
136+
pysqlite_cache_get(pysqlite_Cache *self, PyObject *key)
137+
/*[clinic end generated code: output=149ad799afafcdc8 input=07aef9c27e458441]*/
117138
{
118139
pysqlite_Node* node;
119140
pysqlite_Node* ptr;
@@ -217,7 +238,15 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
217238
return Py_NewRef(node->data);
218239
}
219240

220-
PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
241+
/*[clinic input]
242+
_sqlite3.Cache.display as pysqlite_cache_display
243+
244+
For debugging only.
245+
[clinic start generated code]*/
246+
247+
static PyObject *
248+
pysqlite_cache_display_impl(pysqlite_Cache *self)
249+
/*[clinic end generated code: output=4010f6d5a649271c input=916727d67499366c]*/
221250
{
222251
pysqlite_Node* ptr;
223252
PyObject* prevkey;
@@ -268,10 +297,8 @@ static PyType_Spec node_spec = {
268297
PyTypeObject *pysqlite_NodeType = NULL;
269298

270299
static PyMethodDef cache_methods[] = {
271-
{"get", (PyCFunction)pysqlite_cache_get, METH_O,
272-
PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")},
273-
{"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS,
274-
PyDoc_STR("For debugging only.")},
300+
PYSQLITE_CACHE_GET_METHODDEF
301+
PYSQLITE_CACHE_DISPLAY_METHODDEF
275302
{NULL, NULL}
276303
};
277304

Modules/_sqlite/cache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ typedef struct
6262
extern PyTypeObject *pysqlite_NodeType;
6363
extern PyTypeObject *pysqlite_CacheType;
6464

65-
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args);
66-
6765
int pysqlite_cache_setup_types(PyObject *module);
6866

6967
#endif

Modules/_sqlite/clinic/cache.c.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(pysqlite_cache_init__doc__,
6+
"Cache(factory, /, size=10)\n"
7+
"--\n"
8+
"\n"
9+
"Gets an entry from the cache or calls the factory function to produce one.");
10+
11+
static int
12+
pysqlite_cache_init_impl(pysqlite_Cache *self, PyObject *factory, int size);
13+
14+
static int
15+
pysqlite_cache_init(PyObject *self, PyObject *args, PyObject *kwargs)
16+
{
17+
int return_value = -1;
18+
static const char * const _keywords[] = {"", "size", NULL};
19+
static _PyArg_Parser _parser = {NULL, _keywords, "Cache", 0};
20+
PyObject *argsbuf[2];
21+
PyObject * const *fastargs;
22+
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
23+
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
24+
PyObject *factory;
25+
int size = 10;
26+
27+
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
28+
if (!fastargs) {
29+
goto exit;
30+
}
31+
factory = fastargs[0];
32+
if (!noptargs) {
33+
goto skip_optional_pos;
34+
}
35+
size = _PyLong_AsInt(fastargs[1]);
36+
if (size == -1 && PyErr_Occurred()) {
37+
goto exit;
38+
}
39+
skip_optional_pos:
40+
return_value = pysqlite_cache_init_impl((pysqlite_Cache *)self, factory, size);
41+
42+
exit:
43+
return return_value;
44+
}
45+
46+
PyDoc_STRVAR(pysqlite_cache_get__doc__,
47+
"get($self, key, /)\n"
48+
"--\n"
49+
"\n"
50+
"Gets an entry from the cache or calls the factory function to produce one.");
51+
52+
#define PYSQLITE_CACHE_GET_METHODDEF \
53+
{"get", (PyCFunction)pysqlite_cache_get, METH_O, pysqlite_cache_get__doc__},
54+
55+
PyDoc_STRVAR(pysqlite_cache_display__doc__,
56+
"display($self, /)\n"
57+
"--\n"
58+
"\n"
59+
"For debugging only.");
60+
61+
#define PYSQLITE_CACHE_DISPLAY_METHODDEF \
62+
{"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS, pysqlite_cache_display__doc__},
63+
64+
static PyObject *
65+
pysqlite_cache_display_impl(pysqlite_Cache *self);
66+
67+
static PyObject *
68+
pysqlite_cache_display(pysqlite_Cache *self, PyObject *Py_UNUSED(ignored))
69+
{
70+
return pysqlite_cache_display_impl(self);
71+
}
72+
/*[clinic end generated code: output=324d7a1f2235e360 input=a9049054013a1b77]*/

Modules/_sqlite/cursor.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,10 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
460460
(void)pysqlite_statement_reset(self->statement);
461461
}
462462

463-
Py_XSETREF(self->statement,
464-
(pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
463+
_Py_IDENTIFIER(get);
464+
PyObject *stmt_cache = (PyObject *)self->connection->statement_cache;
465+
PyObject *stmt = _PyObject_CallMethodIdOneArg(stmt_cache, &PyId_get, func_args);
466+
Py_XSETREF(self->statement, (pysqlite_Statement *)stmt);
465467
Py_DECREF(func_args);
466468

467469
if (!self->statement) {

0 commit comments

Comments
 (0)