Skip to content

Commit 144d84a

Browse files
author
Erlend E. Aasland
committed
Convert sqlite3.connect() and Connection.__init__()
1 parent d5baf45 commit 144d84a

File tree

5 files changed

+299
-66
lines changed

5 files changed

+299
-66
lines changed

Modules/_sqlite/clinic/connection.c.h

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,107 @@
22
preserve
33
[clinic start generated code]*/
44

5+
static int
6+
pysqlite_connection_init_impl(pysqlite_Connection *self,
7+
PyObject *database_obj, double timeout,
8+
int detect_types, PyObject *isolation_level,
9+
int check_same_thread, PyObject *factory,
10+
int cached_statements, int uri);
11+
12+
static int
13+
pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
14+
{
15+
int return_value = -1;
16+
static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", NULL};
17+
static _PyArg_Parser _parser = {NULL, _keywords, "Connection", 0};
18+
PyObject *argsbuf[8];
19+
PyObject * const *fastargs;
20+
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
21+
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
22+
PyObject *database_obj;
23+
double timeout = 5.0;
24+
int detect_types = 0;
25+
PyObject *isolation_level = NULL;
26+
int check_same_thread = 1;
27+
PyObject *factory = (PyObject*)&pysqlite_ConnectionType;
28+
int cached_statements = 100;
29+
int uri = 0;
30+
31+
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 8, 0, argsbuf);
32+
if (!fastargs) {
33+
goto exit;
34+
}
35+
if (!PyUnicode_FSConverter(fastargs[0], &database_obj)) {
36+
goto exit;
37+
}
38+
if (!noptargs) {
39+
goto skip_optional_pos;
40+
}
41+
if (fastargs[1]) {
42+
if (PyFloat_CheckExact(fastargs[1])) {
43+
timeout = PyFloat_AS_DOUBLE(fastargs[1]);
44+
}
45+
else
46+
{
47+
timeout = PyFloat_AsDouble(fastargs[1]);
48+
if (timeout == -1.0 && PyErr_Occurred()) {
49+
goto exit;
50+
}
51+
}
52+
if (!--noptargs) {
53+
goto skip_optional_pos;
54+
}
55+
}
56+
if (fastargs[2]) {
57+
detect_types = _PyLong_AsInt(fastargs[2]);
58+
if (detect_types == -1 && PyErr_Occurred()) {
59+
goto exit;
60+
}
61+
if (!--noptargs) {
62+
goto skip_optional_pos;
63+
}
64+
}
65+
if (fastargs[3]) {
66+
isolation_level = fastargs[3];
67+
if (!--noptargs) {
68+
goto skip_optional_pos;
69+
}
70+
}
71+
if (fastargs[4]) {
72+
check_same_thread = _PyLong_AsInt(fastargs[4]);
73+
if (check_same_thread == -1 && PyErr_Occurred()) {
74+
goto exit;
75+
}
76+
if (!--noptargs) {
77+
goto skip_optional_pos;
78+
}
79+
}
80+
if (fastargs[5]) {
81+
factory = fastargs[5];
82+
if (!--noptargs) {
83+
goto skip_optional_pos;
84+
}
85+
}
86+
if (fastargs[6]) {
87+
cached_statements = _PyLong_AsInt(fastargs[6]);
88+
if (cached_statements == -1 && PyErr_Occurred()) {
89+
goto exit;
90+
}
91+
if (!--noptargs) {
92+
goto skip_optional_pos;
93+
}
94+
}
95+
uri = PyObject_IsTrue(fastargs[7]);
96+
if (uri < 0) {
97+
goto exit;
98+
}
99+
skip_optional_pos:
100+
return_value = pysqlite_connection_init_impl((pysqlite_Connection *)self, database_obj, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri);
101+
102+
exit:
103+
return return_value;
104+
}
105+
5106
PyDoc_STRVAR(pysqlite_connection_cursor__doc__,
6107
"cursor($self, /, factory=<unrepresentable>)\n"
7108
"--\n"
@@ -682,4 +783,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
682783
#ifndef PYSQLITE_CONNECTION_BACKUP_METHODDEF
683784
#define PYSQLITE_CONNECTION_BACKUP_METHODDEF
684785
#endif /* !defined(PYSQLITE_CONNECTION_BACKUP_METHODDEF) */
685-
/*[clinic end generated code: output=2f69688f7e0cf5b3 input=a9049054013a1b77]*/
786+
/*[clinic end generated code: output=d1fb1f48710a3cd4 input=a9049054013a1b77]*/

Modules/_sqlite/clinic/module.c.h

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,118 @@
22
preserve
33
[clinic start generated code]*/
44

5+
PyDoc_STRVAR(pysqlite_connect__doc__,
6+
"connect($module, /, database, timeout=5.0, detect_types=0,\n"
7+
" isolation_level=<unrepresentable>, check_same_thread=1,\n"
8+
" factory=ConnectionType, cached_statements=100, uri=False)\n"
9+
"--\n"
10+
"\n"
11+
"Opens a connection to the SQLite database file database.\n"
12+
"\n"
13+
"You can use \":memory:\" to open a database connection to a database that resides\n"
14+
"in RAM instead of on disk.");
15+
16+
#define PYSQLITE_CONNECT_METHODDEF \
17+
{"connect", (PyCFunction)(void(*)(void))pysqlite_connect, METH_FASTCALL|METH_KEYWORDS, pysqlite_connect__doc__},
18+
19+
static PyObject *
20+
pysqlite_connect_impl(PyObject *module, PyObject *database, double timeout,
21+
int detect_types, PyObject *isolation_level,
22+
int check_same_thread, PyObject *factory,
23+
int cached_statements, int uri);
24+
25+
static PyObject *
26+
pysqlite_connect(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
27+
{
28+
PyObject *return_value = NULL;
29+
static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", NULL};
30+
static _PyArg_Parser _parser = {NULL, _keywords, "connect", 0};
31+
PyObject *argsbuf[8];
32+
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
33+
PyObject *database;
34+
double timeout = 5.0;
35+
int detect_types = 0;
36+
PyObject *isolation_level = NULL;
37+
int check_same_thread = 1;
38+
PyObject *factory = (PyObject*)&pysqlite_ConnectionType;
39+
int cached_statements = 100;
40+
int uri = 0;
41+
42+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 8, 0, argsbuf);
43+
if (!args) {
44+
goto exit;
45+
}
46+
if (!PyUnicode_FSConverter(args[0], &database)) {
47+
goto exit;
48+
}
49+
if (!noptargs) {
50+
goto skip_optional_pos;
51+
}
52+
if (args[1]) {
53+
if (PyFloat_CheckExact(args[1])) {
54+
timeout = PyFloat_AS_DOUBLE(args[1]);
55+
}
56+
else
57+
{
58+
timeout = PyFloat_AsDouble(args[1]);
59+
if (timeout == -1.0 && PyErr_Occurred()) {
60+
goto exit;
61+
}
62+
}
63+
if (!--noptargs) {
64+
goto skip_optional_pos;
65+
}
66+
}
67+
if (args[2]) {
68+
detect_types = _PyLong_AsInt(args[2]);
69+
if (detect_types == -1 && PyErr_Occurred()) {
70+
goto exit;
71+
}
72+
if (!--noptargs) {
73+
goto skip_optional_pos;
74+
}
75+
}
76+
if (args[3]) {
77+
isolation_level = args[3];
78+
if (!--noptargs) {
79+
goto skip_optional_pos;
80+
}
81+
}
82+
if (args[4]) {
83+
check_same_thread = _PyLong_AsInt(args[4]);
84+
if (check_same_thread == -1 && PyErr_Occurred()) {
85+
goto exit;
86+
}
87+
if (!--noptargs) {
88+
goto skip_optional_pos;
89+
}
90+
}
91+
if (args[5]) {
92+
factory = args[5];
93+
if (!--noptargs) {
94+
goto skip_optional_pos;
95+
}
96+
}
97+
if (args[6]) {
98+
cached_statements = _PyLong_AsInt(args[6]);
99+
if (cached_statements == -1 && PyErr_Occurred()) {
100+
goto exit;
101+
}
102+
if (!--noptargs) {
103+
goto skip_optional_pos;
104+
}
105+
}
106+
uri = PyObject_IsTrue(args[7]);
107+
if (uri < 0) {
108+
goto exit;
109+
}
110+
skip_optional_pos:
111+
return_value = pysqlite_connect_impl(module, database, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri);
112+
113+
exit:
114+
return return_value;
115+
}
116+
5117
PyDoc_STRVAR(pysqlite_complete_statement__doc__,
6118
"complete_statement($module, statement, /)\n"
7119
"--\n"
@@ -213,4 +325,4 @@ pysqlite_adapt(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
213325
#ifndef PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
214326
#define PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
215327
#endif /* !defined(PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF) */
216-
/*[clinic end generated code: output=2f740fe39ac0ec71 input=a9049054013a1b77]*/
328+
/*[clinic end generated code: output=5e0bb0b26046264e input=a9049054013a1b77]*/

Modules/_sqlite/connection.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,30 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
5858
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
5959

6060

61-
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
62-
{
63-
static char *kwlist[] = {
64-
"database", "timeout", "detect_types", "isolation_level",
65-
"check_same_thread", "factory", "cached_statements", "uri",
66-
NULL
67-
};
61+
/*[clinic input]
62+
_sqlite3.Connection.__init__ as pysqlite_connection_init
63+
64+
database as database_obj: object(converter='PyUnicode_FSConverter')
65+
timeout: double = 5.0
66+
detect_types: int = 0
67+
isolation_level: object = NULL
68+
check_same_thread: int = 1
69+
factory: object(c_default='(PyObject*)&pysqlite_ConnectionType') = ConnectionType
70+
cached_statements: int = 100
71+
uri: bool = False
72+
[clinic start generated code]*/
6873

74+
static int
75+
pysqlite_connection_init_impl(pysqlite_Connection *self,
76+
PyObject *database_obj, double timeout,
77+
int detect_types, PyObject *isolation_level,
78+
int check_same_thread, PyObject *factory,
79+
int cached_statements, int uri)
80+
/*[clinic end generated code: output=dc19df1c0e2b7b77 input=f7f8a772a2df3427]*/
81+
{
6982
const char* database;
70-
PyObject* database_obj;
71-
int detect_types = 0;
72-
PyObject* isolation_level = NULL;
73-
PyObject* factory = NULL;
74-
int check_same_thread = 1;
75-
int cached_statements = 100;
76-
int uri = 0;
77-
double timeout = 5.0;
7883
int rc;
7984

80-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
81-
PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
82-
&isolation_level, &check_same_thread,
83-
&factory, &cached_statements, &uri))
84-
{
85-
return -1;
86-
}
87-
8885
database = PyBytes_AsString(database_obj);
8986

9087
self->initialized = 1;

Modules/_sqlite/connection.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
112112
void pysqlite_connection_dealloc(pysqlite_Connection* self);
113113
PyObject* _pysqlite_connection_begin(pysqlite_Connection* self);
114114
PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw);
115-
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
116115

117116
int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
118117
int pysqlite_check_thread(pysqlite_Connection* self);

0 commit comments

Comments
 (0)