Skip to content

bpo-40956: Use Argument Clinic in sqlite3 module #20826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use Argument Clinic in :mod:`sqlite3`
55 changes: 41 additions & 14 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

#include "cache.h"
#include <limits.h>
#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=94e9b64faa911d27]*/

/* only used internally */
pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
Expand Down Expand Up @@ -54,16 +61,19 @@ void pysqlite_node_dealloc(pysqlite_Node* self)
Py_TYPE(self)->tp_free((PyObject*)self);
}

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
/
[clinic start generated code]*/

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

/* minimum cache size is 5 entries */
if (size < 5) {
Expand Down Expand Up @@ -112,7 +122,18 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self)
Py_TYPE(self)->tp_free((PyObject*)self);
}

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;
Expand Down Expand Up @@ -217,7 +238,15 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
return 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;
Expand Down Expand Up @@ -254,10 +283,8 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
}

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}
};

Expand Down
2 changes: 0 additions & 2 deletions Modules/_sqlite/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ extern PyTypeObject pysqlite_CacheType;
int pysqlite_node_init(pysqlite_Node* self, PyObject* args, PyObject* kwargs);
void pysqlite_node_dealloc(pysqlite_Node* self);

int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs);
void pysqlite_cache_dealloc(pysqlite_Cache* self);
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args);

int pysqlite_cache_setup_types(void);

Expand Down
63 changes: 63 additions & 0 deletions Modules/_sqlite/clinic/cache.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*[clinic input]
preserve
[clinic start generated code]*/

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;
PyObject *factory;
int size = 10;

if (Py_IS_TYPE(self, &pysqlite_CacheType) &&
!_PyArg_NoKeywords("Cache", kwargs)) {
goto exit;
}
if (!_PyArg_CheckPositional("Cache", PyTuple_GET_SIZE(args), 1, 2)) {
goto exit;
}
factory = PyTuple_GET_ITEM(args, 0);
if (PyTuple_GET_SIZE(args) < 2) {
goto skip_optional;
}
size = _PyLong_AsInt(PyTuple_GET_ITEM(args, 1));
if (size == -1 && PyErr_Occurred()) {
goto exit;
}
skip_optional:
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=60204e1295b95c6a input=a9049054013a1b77]*/
Loading