Skip to content

Commit f961c3f

Browse files
author
Erlend E. Aasland
committed
bpo-44041: Only call sqlite3_column_count() if needed
1 parent 834498e commit f961c3f

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

Modules/_sqlite/cursor.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self)
147147
return -1;
148148
}
149149

150-
for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
150+
int column_count = self->statement->column_count;
151+
for (i = 0; i < column_count; i++) {
151152
converter = NULL;
152153

153154
if (self->connection->detect_types & PARSE_COLNAMES) {
@@ -547,9 +548,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
547548
}
548549

549550
assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
550-
Py_BEGIN_ALLOW_THREADS
551-
numcols = sqlite3_column_count(self->statement->st);
552-
Py_END_ALLOW_THREADS
551+
numcols = self->statement->column_count;
553552
if (self->description == Py_None && numcols > 0) {
554553
Py_SETREF(self->description, PyTuple_New(numcols));
555554
if (!self->description) {

Modules/_sqlite/statement.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
5858

5959
self->st = NULL;
6060
self->in_use = 0;
61+
self->column_count = 0;
6162

6263
assert(PyUnicode_Check(sql));
6364

@@ -103,10 +104,20 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
103104

104105
self->db = connection->db;
105106

106-
if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
107+
if (rc != SQLITE_OK) {
108+
return rc;
109+
}
110+
111+
if (pysqlite_check_remaining_sql(tail)) {
107112
(void)sqlite3_finalize(self->st);
108113
self->st = NULL;
109-
rc = PYSQLITE_TOO_MUCH_SQL;
114+
return PYSQLITE_TOO_MUCH_SQL;
115+
}
116+
117+
if (!self->is_dml) {
118+
Py_BEGIN_ALLOW_THREADS
119+
self->column_count = sqlite3_column_count(self->st);
120+
Py_END_ALLOW_THREADS
110121
}
111122

112123
return rc;

Modules/_sqlite/statement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct
4040
PyObject* sql;
4141
int in_use;
4242
int is_dml;
43+
int column_count;
4344
PyObject* in_weakreflist; /* List of weak references */
4445
} pysqlite_Statement;
4546

0 commit comments

Comments
 (0)