Skip to content

Commit ee10a9d

Browse files
author
Erlend E. Aasland
committed
Harden tuple creation
1 parent 23acadc commit ee10a9d

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
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: 14 additions & 18 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

@@ -470,9 +469,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
470469
if (!func_args) {
471470
goto error;
472471
}
473-
if (PyTuple_SetItem(func_args, 0, Py_NewRef(operation)) != 0) {
474-
goto error;
475-
}
472+
PyTuple_SET_ITEM(func_args, 0, Py_NewRef(operation));
476473

477474
if (self->statement) {
478475
(void)pysqlite_statement_reset(self->statement);
@@ -557,30 +554,29 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
557554
goto error;
558555
}
559556
for (i = 0; i < numcols; i++) {
560-
descriptor = PyTuple_New(7);
561-
if (!descriptor) {
562-
goto error;
563-
}
564557
const char *colname;
565558
colname = sqlite3_column_name(self->statement->st, i);
566559
if (colname == NULL) {
567560
PyErr_NoMemory();
568-
Py_DECREF(descriptor);
561+
goto error;
562+
}
563+
PyObject *descriptor = PyTuple_New(7);
564+
if (descriptor == NULL) {
569565
goto error;
570566
}
571567
column_name = _pysqlite_build_column_name(self, colname);
572568
if (column_name == NULL) {
573569
Py_DECREF(descriptor);
574570
goto error;
575571
}
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);
572+
PyTuple_SET_ITEM(descriptor, 0, column_name);
573+
PyTuple_SET_ITEM(descriptor, 1, Py_NewRef(Py_None));
574+
PyTuple_SET_ITEM(descriptor, 2, Py_NewRef(Py_None));
575+
PyTuple_SET_ITEM(descriptor, 3, Py_NewRef(Py_None));
576+
PyTuple_SET_ITEM(descriptor, 4, Py_NewRef(Py_None));
577+
PyTuple_SET_ITEM(descriptor, 5, Py_NewRef(Py_None));
578+
PyTuple_SET_ITEM(descriptor, 6, Py_NewRef(Py_None));
579+
PyTuple_SET_ITEM(self->description, i, descriptor);
584580
}
585581
}
586582

0 commit comments

Comments
 (0)