Skip to content

Commit e9194ea

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43852: Improve tuple creation in sqlite3 (pythonGH-25421)
1 parent 927b841 commit e9194ea

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

Modules/_sqlite/connection.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,7 @@ _pysqlite_build_py_params(sqlite3_context *context, int argc,
592592
goto error;
593593
}
594594

595-
PyTuple_SetItem(args, i, cur_py_value);
596-
595+
PyTuple_SET_ITEM(args, i, cur_py_value);
597596
}
598597

599598
return args;

Modules/_sqlite/cursor.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
357357
if (!converted) {
358358
goto error;
359359
}
360-
PyTuple_SetItem(row, i, converted);
360+
PyTuple_SET_ITEM(row, i, converted);
361361
}
362362

363363
if (PyErr_Occurred())
@@ -406,7 +406,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
406406
PyObject* func_args;
407407
PyObject* result;
408408
int numcols;
409-
PyObject* descriptor;
410409
PyObject* column_name;
411410
sqlite_int64 lastrowid;
412411

@@ -557,30 +556,24 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
557556
goto error;
558557
}
559558
for (i = 0; i < numcols; i++) {
560-
descriptor = PyTuple_New(7);
561-
if (!descriptor) {
562-
goto error;
563-
}
564559
const char *colname;
565560
colname = sqlite3_column_name(self->statement->st, i);
566561
if (colname == NULL) {
567562
PyErr_NoMemory();
568-
Py_DECREF(descriptor);
569563
goto error;
570564
}
571565
column_name = _pysqlite_build_column_name(self, colname);
572566
if (column_name == NULL) {
573-
Py_DECREF(descriptor);
574567
goto error;
575568
}
576-
PyTuple_SetItem(descriptor, 0, column_name);
577-
PyTuple_SetItem(descriptor, 1, Py_NewRef(Py_None));
578-
PyTuple_SetItem(descriptor, 2, Py_NewRef(Py_None));
579-
PyTuple_SetItem(descriptor, 3, Py_NewRef(Py_None));
580-
PyTuple_SetItem(descriptor, 4, Py_NewRef(Py_None));
581-
PyTuple_SetItem(descriptor, 5, Py_NewRef(Py_None));
582-
PyTuple_SetItem(descriptor, 6, Py_NewRef(Py_None));
583-
PyTuple_SetItem(self->description, i, descriptor);
569+
PyObject *descriptor = PyTuple_Pack(7, column_name,
570+
Py_None, Py_None, Py_None,
571+
Py_None, Py_None, Py_None);
572+
Py_DECREF(column_name);
573+
if (descriptor == NULL) {
574+
goto error;
575+
}
576+
PyTuple_SET_ITEM(self->description, i, descriptor);
584577
}
585578
}
586579

0 commit comments

Comments
 (0)