Skip to content

Commit 1b0f583

Browse files
author
Erlend E. Aasland
committed
Allocate and track statement objects in pysqlite_statement_create
By allocating and tracking creation of statement object in pysqlite_statement_create(), the caller does not need to worry about GC syncronization, and eliminates the possibility of getting a badly created object. All related fault handling is moved to pysqlite_statement_create().
1 parent 36ae3eb commit 1b0f583

File tree

4 files changed

+35
-43
lines changed

4 files changed

+35
-43
lines changed

Modules/_sqlite/connection.c

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,6 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
13371337
PyObject* sql;
13381338
pysqlite_Statement* statement;
13391339
PyObject* weakref;
1340-
int rc;
13411340

13421341
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
13431342
return NULL;
@@ -1351,32 +1350,11 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
13511350

13521351
_pysqlite_drop_unused_statement_references(self);
13531352

1354-
statement = PyObject_GC_New(pysqlite_Statement, pysqlite_StatementType);
1355-
if (!statement) {
1353+
statement = pysqlite_statement_create(self, sql);
1354+
if (statement == NULL) {
13561355
return NULL;
13571356
}
13581357

1359-
statement->db = NULL;
1360-
statement->st = NULL;
1361-
statement->sql = NULL;
1362-
statement->in_use = 0;
1363-
statement->in_weakreflist = NULL;
1364-
1365-
rc = pysqlite_statement_create(statement, self, sql);
1366-
PyObject_GC_Track(statement);
1367-
if (rc != SQLITE_OK) {
1368-
if (rc == PYSQLITE_TOO_MUCH_SQL) {
1369-
PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
1370-
} else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
1371-
if (PyErr_ExceptionMatches(PyExc_TypeError))
1372-
PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
1373-
} else {
1374-
(void)pysqlite_statement_reset(statement);
1375-
_pysqlite_seterror(self->db);
1376-
}
1377-
goto error;
1378-
}
1379-
13801358
weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
13811359
if (weakref == NULL)
13821360
goto error;

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
507507

508508
if (self->statement->in_use) {
509509
Py_SETREF(self->statement,
510-
PyObject_GC_New(pysqlite_Statement, pysqlite_StatementType));
511-
if (!self->statement) {
512-
goto error;
513-
}
514-
rc = pysqlite_statement_create(self->statement, self->connection, operation);
515-
PyObject_GC_Track(self->statement);
516-
if (rc != SQLITE_OK) {
517-
Py_CLEAR(self->statement);
510+
pysqlite_statement_create(self->connection, operation));
511+
if (self->statement == NULL) {
518512
goto error;
519513
}
520514
}

Modules/_sqlite/statement.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,37 @@ typedef enum {
4848
TYPE_UNKNOWN
4949
} parameter_type;
5050

51-
int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
51+
pysqlite_Statement *
52+
pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
5253
{
5354
const char* tail;
5455
int rc;
5556
const char* sql_cstr;
5657
Py_ssize_t sql_cstr_len;
5758
const char* p;
5859

59-
self->st = NULL;
60-
self->in_use = 0;
61-
6260
assert(PyUnicode_Check(sql));
6361

6462
sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len);
6563
if (sql_cstr == NULL) {
66-
rc = PYSQLITE_SQL_WRONG_TYPE;
67-
return rc;
64+
PyErr_SetString(pysqlite_Warning,
65+
"SQL is of wrong type. Must be string.");
66+
return NULL;
6867
}
6968
if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
70-
PyErr_SetString(PyExc_ValueError, "the query contains a null character");
71-
return PYSQLITE_SQL_WRONG_TYPE;
69+
PyErr_SetString(PyExc_ValueError,
70+
"the query contains a null character");
71+
return NULL;
7272
}
7373

74+
pysqlite_Statement *self = PyObject_GC_New(pysqlite_Statement,
75+
pysqlite_StatementType);
76+
if (self == NULL) {
77+
return NULL;
78+
}
79+
80+
self->st = NULL;
81+
self->in_use = 0;
7482
self->in_weakreflist = NULL;
7583
self->sql = Py_NewRef(sql);
7684

@@ -102,14 +110,26 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
102110
Py_END_ALLOW_THREADS
103111

104112
self->db = connection->db;
113+
PyObject_GC_Track(self);
114+
115+
if (rc != SQLITE_OK) {
116+
_pysqlite_seterror(self->db);
117+
goto error;
118+
}
105119

106120
if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
107121
(void)sqlite3_finalize(self->st);
108122
self->st = NULL;
109-
rc = PYSQLITE_TOO_MUCH_SQL;
123+
PyErr_SetString(pysqlite_Warning,
124+
"You can only execute one statement at a time.");
125+
goto error;
110126
}
111127

112-
return rc;
128+
return self;
129+
130+
error:
131+
Py_DECREF(self);
132+
return NULL;
113133
}
114134

115135
int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)

Modules/_sqlite/statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct
4545

4646
extern PyTypeObject *pysqlite_StatementType;
4747

48-
int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql);
48+
pysqlite_Statement *pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql);
4949

5050
int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter);
5151
void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters);

0 commit comments

Comments
 (0)