Skip to content

Commit 311740f

Browse files
author
Erlend E. Aasland
committed
Convert Cache
1 parent 6e1a726 commit 311740f

File tree

4 files changed

+109
-18
lines changed

4 files changed

+109
-18
lines changed

Modules/_sqlite/cache.c

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
#include "cache.h"
2525
#include <limits.h>
26+
#include "clinic/cache.c.h"
27+
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=94e9b64faa911d27]*/
2633

2734
/* only used internally */
2835
pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
@@ -54,16 +61,19 @@ void pysqlite_node_dealloc(pysqlite_Node* self)
5461
Py_TYPE(self)->tp_free((PyObject*)self);
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+
size: int = 10
69+
/
70+
[clinic start generated code]*/
6371

64-
if (!PyArg_ParseTuple(args, "O|i", &factory, &size)) {
65-
return -1;
66-
}
72+
static int
73+
pysqlite_cache_init_impl(pysqlite_Cache *self, PyObject *factory, int size)
74+
/*[clinic end generated code: output=3a3b3e0486364359 input=5c1df5f8291291b0]*/
75+
{
76+
self->factory = NULL;
6777

6878
/* minimum cache size is 5 entries */
6979
if (size < 5) {
@@ -112,7 +122,18 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self)
112122
Py_TYPE(self)->tp_free((PyObject*)self);
113123
}
114124

115-
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
125+
/*[clinic input]
126+
_sqlite3.Cache.get as pysqlite_cache_get
127+
128+
key: object
129+
/
130+
131+
Gets an entry from the cache or calls the factory function to produce one.
132+
[clinic start generated code]*/
133+
134+
static PyObject *
135+
pysqlite_cache_get(pysqlite_Cache *self, PyObject *key)
136+
/*[clinic end generated code: output=149ad799afafcdc8 input=07aef9c27e458441]*/
116137
{
117138
pysqlite_Node* node;
118139
pysqlite_Node* ptr;
@@ -217,7 +238,15 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
217238
return 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;
@@ -254,10 +283,8 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
254283
}
255284

256285
static PyMethodDef cache_methods[] = {
257-
{"get", (PyCFunction)pysqlite_cache_get, METH_O,
258-
PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")},
259-
{"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS,
260-
PyDoc_STR("For debugging only.")},
286+
PYSQLITE_CACHE_GET_METHODDEF
287+
PYSQLITE_CACHE_DISPLAY_METHODDEF
261288
{NULL, NULL}
262289
};
263290

Modules/_sqlite/cache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ extern PyTypeObject pysqlite_CacheType;
6565
int pysqlite_node_init(pysqlite_Node* self, PyObject* args, PyObject* kwargs);
6666
void pysqlite_node_dealloc(pysqlite_Node* self);
6767

68-
int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs);
6968
void pysqlite_cache_dealloc(pysqlite_Cache* self);
70-
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args);
7169

7270
int pysqlite_cache_setup_types(void);
7371

Modules/_sqlite/clinic/cache.c.h

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

Modules/_sqlite/cursor.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ static int check_cursor(pysqlite_Cursor* cur)
368368
static PyObject *
369369
_pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
370370
{
371+
_Py_IDENTIFIER(get);
372+
pysqlite_Statement *stmt;
371373
PyObject* operation;
372374
PyObject* parameters_list = NULL;
373375
PyObject* parameters_iter = NULL;
@@ -462,8 +464,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
462464
(void)pysqlite_statement_reset(self->statement);
463465
}
464466

465-
Py_XSETREF(self->statement,
466-
(pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
467+
stmt = (pysqlite_Statement *)_PyObject_CallMethodIdOneArg((PyObject *)self->connection->statement_cache,
468+
&PyId_get, func_args);
469+
Py_XSETREF(self->statement, stmt);
467470
Py_DECREF(func_args);
468471

469472
if (!self->statement) {

0 commit comments

Comments
 (0)