Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Convert sqlite3.connect() and Connection.__init__()
  • Loading branch information
Erlend E. Aasland
Erlend E. Aasland committed Sep 8, 2020
commit 144d84ad63b286bb56396beadc9fe528f20eec88
103 changes: 102 additions & 1 deletion Modules/_sqlite/clinic/connection.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,107 @@
preserve
[clinic start generated code]*/

static int
pysqlite_connection_init_impl(pysqlite_Connection *self,
PyObject *database_obj, double timeout,
int detect_types, PyObject *isolation_level,
int check_same_thread, PyObject *factory,
int cached_statements, int uri);

static int
pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", NULL};
static _PyArg_Parser _parser = {NULL, _keywords, "Connection", 0};
PyObject *argsbuf[8];
PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
PyObject *database_obj;
double timeout = 5.0;
int detect_types = 0;
PyObject *isolation_level = NULL;
int check_same_thread = 1;
PyObject *factory = (PyObject*)&pysqlite_ConnectionType;
int cached_statements = 100;
int uri = 0;

fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 8, 0, argsbuf);
if (!fastargs) {
goto exit;
}
if (!PyUnicode_FSConverter(fastargs[0], &database_obj)) {
goto exit;
}
if (!noptargs) {
goto skip_optional_pos;
}
if (fastargs[1]) {
if (PyFloat_CheckExact(fastargs[1])) {
timeout = PyFloat_AS_DOUBLE(fastargs[1]);
}
else
{
timeout = PyFloat_AsDouble(fastargs[1]);
if (timeout == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[2]) {
detect_types = _PyLong_AsInt(fastargs[2]);
if (detect_types == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[3]) {
isolation_level = fastargs[3];
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[4]) {
check_same_thread = _PyLong_AsInt(fastargs[4]);
if (check_same_thread == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[5]) {
factory = fastargs[5];
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (fastargs[6]) {
cached_statements = _PyLong_AsInt(fastargs[6]);
if (cached_statements == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
uri = PyObject_IsTrue(fastargs[7]);
if (uri < 0) {
goto exit;
}
skip_optional_pos:
return_value = pysqlite_connection_init_impl((pysqlite_Connection *)self, database_obj, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_connection_cursor__doc__,
"cursor($self, /, factory=<unrepresentable>)\n"
"--\n"
Expand Down Expand Up @@ -682,4 +783,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
#ifndef PYSQLITE_CONNECTION_BACKUP_METHODDEF
#define PYSQLITE_CONNECTION_BACKUP_METHODDEF
#endif /* !defined(PYSQLITE_CONNECTION_BACKUP_METHODDEF) */
/*[clinic end generated code: output=2f69688f7e0cf5b3 input=a9049054013a1b77]*/
/*[clinic end generated code: output=d1fb1f48710a3cd4 input=a9049054013a1b77]*/
114 changes: 113 additions & 1 deletion Modules/_sqlite/clinic/module.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,118 @@
preserve
[clinic start generated code]*/

PyDoc_STRVAR(pysqlite_connect__doc__,
"connect($module, /, database, timeout=5.0, detect_types=0,\n"
" isolation_level=<unrepresentable>, check_same_thread=1,\n"
" factory=ConnectionType, cached_statements=100, uri=False)\n"
"--\n"
"\n"
"Opens a connection to the SQLite database file database.\n"
"\n"
"You can use \":memory:\" to open a database connection to a database that resides\n"
"in RAM instead of on disk.");

#define PYSQLITE_CONNECT_METHODDEF \
{"connect", (PyCFunction)(void(*)(void))pysqlite_connect, METH_FASTCALL|METH_KEYWORDS, pysqlite_connect__doc__},

static PyObject *
pysqlite_connect_impl(PyObject *module, PyObject *database, double timeout,
int detect_types, PyObject *isolation_level,
int check_same_thread, PyObject *factory,
int cached_statements, int uri);

static PyObject *
pysqlite_connect(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", NULL};
static _PyArg_Parser _parser = {NULL, _keywords, "connect", 0};
PyObject *argsbuf[8];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *database;
double timeout = 5.0;
int detect_types = 0;
PyObject *isolation_level = NULL;
int check_same_thread = 1;
PyObject *factory = (PyObject*)&pysqlite_ConnectionType;
int cached_statements = 100;
int uri = 0;

args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 8, 0, argsbuf);
if (!args) {
goto exit;
}
if (!PyUnicode_FSConverter(args[0], &database)) {
goto exit;
}
if (!noptargs) {
goto skip_optional_pos;
}
if (args[1]) {
if (PyFloat_CheckExact(args[1])) {
timeout = PyFloat_AS_DOUBLE(args[1]);
}
else
{
timeout = PyFloat_AsDouble(args[1]);
if (timeout == -1.0 && PyErr_Occurred()) {
goto exit;
}
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (args[2]) {
detect_types = _PyLong_AsInt(args[2]);
if (detect_types == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (args[3]) {
isolation_level = args[3];
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (args[4]) {
check_same_thread = _PyLong_AsInt(args[4]);
if (check_same_thread == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (args[5]) {
factory = args[5];
if (!--noptargs) {
goto skip_optional_pos;
}
}
if (args[6]) {
cached_statements = _PyLong_AsInt(args[6]);
if (cached_statements == -1 && PyErr_Occurred()) {
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
}
uri = PyObject_IsTrue(args[7]);
if (uri < 0) {
goto exit;
}
skip_optional_pos:
return_value = pysqlite_connect_impl(module, database, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_complete_statement__doc__,
"complete_statement($module, statement, /)\n"
"--\n"
Expand Down Expand Up @@ -213,4 +325,4 @@ pysqlite_adapt(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
#ifndef PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
#define PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
#endif /* !defined(PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF) */
/*[clinic end generated code: output=2f740fe39ac0ec71 input=a9049054013a1b77]*/
/*[clinic end generated code: output=5e0bb0b26046264e input=a9049054013a1b77]*/
43 changes: 20 additions & 23 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,30 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);


int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
{
static char *kwlist[] = {
"database", "timeout", "detect_types", "isolation_level",
"check_same_thread", "factory", "cached_statements", "uri",
NULL
};
/*[clinic input]
_sqlite3.Connection.__init__ as pysqlite_connection_init

database as database_obj: object(converter='PyUnicode_FSConverter')
timeout: double = 5.0
detect_types: int = 0
isolation_level: object = NULL
check_same_thread: int = 1
factory: object(c_default='(PyObject*)&pysqlite_ConnectionType') = ConnectionType
cached_statements: int = 100
uri: bool = False
[clinic start generated code]*/

static int
pysqlite_connection_init_impl(pysqlite_Connection *self,
PyObject *database_obj, double timeout,
int detect_types, PyObject *isolation_level,
int check_same_thread, PyObject *factory,
int cached_statements, int uri)
/*[clinic end generated code: output=dc19df1c0e2b7b77 input=f7f8a772a2df3427]*/
{
const char* database;
PyObject* database_obj;
int detect_types = 0;
PyObject* isolation_level = NULL;
PyObject* factory = NULL;
int check_same_thread = 1;
int cached_statements = 100;
int uri = 0;
double timeout = 5.0;
int rc;

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
&isolation_level, &check_same_thread,
&factory, &cached_statements, &uri))
{
return -1;
}

database = PyBytes_AsString(database_obj);

self->initialized = 1;
Expand Down
1 change: 0 additions & 1 deletion Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
void pysqlite_connection_dealloc(pysqlite_Connection* self);
PyObject* _pysqlite_connection_begin(pysqlite_Connection* self);
PyObject* pysqlite_connection_new(PyTypeObject* type, PyObject* args, PyObject* kw);
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);

int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
int pysqlite_check_thread(pysqlite_Connection* self);
Expand Down
Loading