Skip to content

Commit b8ef03d

Browse files
[alt 2] gh-95132: use vectorcall
1 parent 95296cb commit b8ef03d

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

Modules/_sqlite/module.c

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module _sqlite3
4242
[clinic start generated code]*/
4343
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/
4444

45-
PyDoc_STRVAR(pysqlite_connect__doc__,
45+
PyDoc_STRVAR(module_connect_doc,
4646
"connect($module, /, database, timeout=5.0, detect_types=0,\n"
4747
" isolation_level=<unrepresentable>, check_same_thread=True,\n"
4848
" factory=ConnectionType, cached_statements=128, uri=False)\n"
@@ -54,40 +54,31 @@ PyDoc_STRVAR(pysqlite_connect__doc__,
5454
"in RAM instead of on disk.");
5555

5656
#define PYSQLITE_CONNECT_METHODDEF \
57-
{"connect", _PyCFunction_CAST(module_connect), METH_VARARGS|METH_KEYWORDS, pysqlite_connect__doc__},
57+
{"connect", _PyCFunction_CAST(module_connect), METH_FASTCALL|METH_KEYWORDS, module_connect_doc},
5858

5959
static PyObject *
60-
module_connect(PyObject *self, PyObject *args, PyObject *kwargs)
60+
module_connect(PyObject *module, PyObject *const *args, Py_ssize_t nargsf,
61+
PyObject *kwnames)
6162
{
62-
/* Python seems to have no way of extracting a single keyword-arg at
63-
* C-level, so this code is redundant with the one in connection_init in
64-
* connection.c and must always be copied from there ... */
65-
static char *kwlist[] = {
66-
"database", "timeout", "detect_types", "isolation_level",
67-
"check_same_thread", "factory", "cached_statements", "uri",
68-
NULL
69-
};
70-
PyObject *database;
71-
int detect_types = 0;
72-
PyObject *isolation_level;
73-
PyObject *factory = NULL;
74-
int check_same_thread = 1;
75-
int cached_statements;
76-
int uri = 0;
77-
double timeout = 5.0;
78-
79-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist,
80-
&database, &timeout, &detect_types,
81-
&isolation_level, &check_same_thread,
82-
&factory, &cached_statements, &uri))
83-
{
84-
return NULL;
63+
pysqlite_state *state = pysqlite_get_state(module);
64+
PyObject *factory = (PyObject *)state->ConnectionType;
65+
66+
static const int FACTORY_POS = 5;
67+
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
68+
if (nargs > FACTORY_POS) {
69+
factory = args[FACTORY_POS];
8570
}
86-
if (factory == NULL) {
87-
pysqlite_state *state = pysqlite_get_state(self);
88-
factory = (PyObject *)state->ConnectionType;
71+
else if (kwnames != NULL) {
72+
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
73+
PyObject *item = PyTuple_GET_ITEM(kwnames, i); // borrowed ref.
74+
if (PyUnicode_CompareWithASCIIString(item, "factory") == 0) {
75+
factory = args[nargs + i];
76+
break;
77+
}
78+
}
8979
}
90-
return PyObject_Call(factory, args, kwargs);
80+
81+
return PyObject_Vectorcall(factory, args, nargsf, kwnames);
9182
}
9283

9384
/*[clinic input]

0 commit comments

Comments
 (0)