Skip to content

Commit 84d79cf

Browse files
author
Erlend Egeberg Aasland
authored
bpo-40956: Convert _sqlite3.Row to Argument Clinic (GH-23964)
1 parent 0159e5e commit 84d79cf

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

Modules/_sqlite/clinic/row.c.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
static PyObject *
6+
pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor,
7+
PyObject *data);
8+
9+
static PyObject *
10+
pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
11+
{
12+
PyObject *return_value = NULL;
13+
pysqlite_Cursor *cursor;
14+
PyObject *data;
15+
16+
if ((type == pysqlite_RowType) &&
17+
!_PyArg_NoKeywords("Row", kwargs)) {
18+
goto exit;
19+
}
20+
if (!_PyArg_CheckPositional("Row", PyTuple_GET_SIZE(args), 2, 2)) {
21+
goto exit;
22+
}
23+
if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), pysqlite_CursorType)) {
24+
_PyArg_BadArgument("Row", "argument 1", (pysqlite_CursorType)->tp_name, PyTuple_GET_ITEM(args, 0));
25+
goto exit;
26+
}
27+
cursor = (pysqlite_Cursor *)PyTuple_GET_ITEM(args, 0);
28+
if (!PyTuple_Check(PyTuple_GET_ITEM(args, 1))) {
29+
_PyArg_BadArgument("Row", "argument 2", "tuple", PyTuple_GET_ITEM(args, 1));
30+
goto exit;
31+
}
32+
data = PyTuple_GET_ITEM(args, 1);
33+
return_value = pysqlite_row_new_impl(type, cursor, data);
34+
35+
exit:
36+
return return_value;
37+
}
38+
39+
PyDoc_STRVAR(pysqlite_row_keys__doc__,
40+
"keys($self, /)\n"
41+
"--\n"
42+
"\n"
43+
"Returns the keys of the row.");
44+
45+
#define PYSQLITE_ROW_KEYS_METHODDEF \
46+
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, pysqlite_row_keys__doc__},
47+
48+
static PyObject *
49+
pysqlite_row_keys_impl(pysqlite_Row *self);
50+
51+
static PyObject *
52+
pysqlite_row_keys(pysqlite_Row *self, PyObject *Py_UNUSED(ignored))
53+
{
54+
return pysqlite_row_keys_impl(self);
55+
}
56+
/*[clinic end generated code: output=8d29220b9cde035d input=a9049054013a1b77]*/

Modules/_sqlite/row.c

+30-21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
#include "row.h"
2525
#include "cursor.h"
26+
#include "clinic/row.c.h"
27+
28+
/*[clinic input]
29+
module _sqlite3
30+
class _sqlite3.Row "pysqlite_Row *" "pysqlite_RowType"
31+
[clinic start generated code]*/
32+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=384227da65f250fd]*/
2633

2734
void pysqlite_row_dealloc(pysqlite_Row* self)
2835
{
@@ -35,30 +42,25 @@ void pysqlite_row_dealloc(pysqlite_Row* self)
3542
Py_DECREF(tp);
3643
}
3744

45+
/*[clinic input]
46+
@classmethod
47+
_sqlite3.Row.__new__ as pysqlite_row_new
48+
49+
cursor: object(type='pysqlite_Cursor *', subclass_of='pysqlite_CursorType')
50+
data: object(subclass_of='&PyTuple_Type')
51+
/
52+
53+
[clinic start generated code]*/
54+
3855
static PyObject *
39-
pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
56+
pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor,
57+
PyObject *data)
58+
/*[clinic end generated code: output=10d58b09a819a4c1 input=f6cd7e6e0935828d]*/
4059
{
4160
pysqlite_Row *self;
42-
PyObject* data;
43-
pysqlite_Cursor* cursor;
4461

4562
assert(type != NULL && type->tp_alloc != NULL);
4663

47-
if (!_PyArg_NoKeywords("Row", kwargs))
48-
return NULL;
49-
if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
50-
return NULL;
51-
52-
if (!PyObject_TypeCheck((PyObject*)cursor, pysqlite_CursorType)) {
53-
PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
54-
return NULL;
55-
}
56-
57-
if (!PyTuple_Check(data)) {
58-
PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
59-
return NULL;
60-
}
61-
6264
self = (pysqlite_Row *) type->tp_alloc(type, 0);
6365
if (self == NULL)
6466
return NULL;
@@ -153,7 +155,15 @@ pysqlite_row_length(pysqlite_Row* self)
153155
return PyTuple_GET_SIZE(self->data);
154156
}
155157

156-
PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject *Py_UNUSED(ignored))
158+
/*[clinic input]
159+
_sqlite3.Row.keys as pysqlite_row_keys
160+
161+
Returns the keys of the row.
162+
[clinic start generated code]*/
163+
164+
static PyObject *
165+
pysqlite_row_keys_impl(pysqlite_Row *self)
166+
/*[clinic end generated code: output=efe3dfb3af6edc07 input=7549a122827c5563]*/
157167
{
158168
PyObject* list;
159169
Py_ssize_t nitems, i;
@@ -204,8 +214,7 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
204214
}
205215

206216
static PyMethodDef row_methods[] = {
207-
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
208-
PyDoc_STR("Returns the keys of the row.")},
217+
PYSQLITE_ROW_KEYS_METHODDEF
209218
{NULL, NULL}
210219
};
211220

0 commit comments

Comments
 (0)