bpo-44976: Lazy creation of sqlite3 result rows (GH-27884) · python/cpython@3df0fc8 (original) (raw)
`@@ -53,7 +53,6 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
`
53
53
`Py_INCREF(connection);
`
54
54
`Py_XSETREF(self->connection, connection);
`
55
55
`Py_CLEAR(self->statement);
`
56
``
`-
Py_CLEAR(self->next_row);
`
57
56
`Py_CLEAR(self->row_cast_map);
`
58
57
``
59
58
`Py_INCREF(Py_None);
`
`@@ -94,7 +93,6 @@ cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
`
94
93
`Py_VISIT(self->lastrowid);
`
95
94
`Py_VISIT(self->row_factory);
`
96
95
`Py_VISIT(self->statement);
`
97
``
`-
Py_VISIT(self->next_row);
`
98
96
`return 0;
`
99
97
`}
`
100
98
``
`@@ -111,7 +109,6 @@ cursor_clear(pysqlite_Cursor *self)
`
111
109
`pysqlite_statement_reset(self->statement);
`
112
110
`Py_CLEAR(self->statement);
`
113
111
` }
`
114
``
`-
Py_CLEAR(self->next_row);
`
115
112
``
116
113
`return 0;
`
117
114
`}
`
`@@ -489,8 +486,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
`
489
486
`self->locked = 1;
`
490
487
`self->reset = 0;
`
491
488
``
492
``
`-
Py_CLEAR(self->next_row);
`
493
``
-
494
489
`if (multiple) {
`
495
490
`if (PyIter_Check(second_argument)) {
`
496
491
`/* iterator */
`
`@@ -658,11 +653,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
`
658
653
` }
`
659
654
` }
`
660
655
``
661
``
`-
if (rc == SQLITE_ROW) {
`
662
``
`-
self->next_row = _pysqlite_fetch_one_row(self);
`
663
``
`-
if (self->next_row == NULL)
`
664
``
`-
goto error;
`
665
``
`-
} else if (rc == SQLITE_DONE && !multiple) {
`
``
656
`+
if (rc == SQLITE_DONE && !multiple) {
`
666
657
`pysqlite_statement_reset(self->statement);
`
667
658
`Py_CLEAR(self->statement);
`
668
659
` }
`
`@@ -821,10 +812,6 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
`
821
812
`static PyObject *
`
822
813
`pysqlite_cursor_iternext(pysqlite_Cursor *self)
`
823
814
`{
`
824
``
`-
PyObject* next_row_tuple;
`
825
``
`-
PyObject* next_row;
`
826
``
`-
int rc;
`
827
``
-
828
815
`if (!check_cursor(self)) {
`
829
816
`return NULL;
`
830
817
` }
`
`@@ -835,53 +822,40 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
`
835
822
`return NULL;
`
836
823
` }
`
837
824
``
838
``
`-
if (!self->next_row) {
`
839
``
`-
if (self->statement) {
`
840
``
`-
(void)pysqlite_statement_reset(self->statement);
`
841
``
`-
Py_CLEAR(self->statement);
`
842
``
`-
}
`
``
825
`+
if (self->statement == NULL) {
`
843
826
`return NULL;
`
844
827
` }
`
845
828
``
846
``
`-
next_row_tuple = self->next_row;
`
847
``
`-
assert(next_row_tuple != NULL);
`
848
``
`-
self->next_row = NULL;
`
849
``
-
850
``
`-
if (self->row_factory != Py_None) {
`
851
``
`-
next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
`
852
``
`-
if (next_row == NULL) {
`
853
``
`-
self->next_row = next_row_tuple;
`
854
``
`-
return NULL;
`
855
``
`-
}
`
856
``
`-
Py_DECREF(next_row_tuple);
`
857
``
`-
} else {
`
858
``
`-
next_row = next_row_tuple;
`
``
829
`+
sqlite3_stmt *stmt = self->statement->st;
`
``
830
`+
assert(stmt != NULL);
`
``
831
`+
if (sqlite3_data_count(stmt) == 0) {
`
``
832
`+
(void)pysqlite_statement_reset(self->statement);
`
``
833
`+
Py_CLEAR(self->statement);
`
``
834
`+
return NULL;
`
859
835
` }
`
860
836
``
861
``
`-
if (self->statement) {
`
862
``
`-
rc = pysqlite_step(self->statement->st);
`
863
``
`-
if (PyErr_Occurred()) {
`
864
``
`-
(void)pysqlite_statement_reset(self->statement);
`
865
``
`-
Py_DECREF(next_row);
`
866
``
`-
return NULL;
`
867
``
`-
}
`
868
``
`-
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
`
869
``
`-
(void)pysqlite_statement_reset(self->statement);
`
870
``
`-
Py_DECREF(next_row);
`
871
``
`-
_pysqlite_seterror(self->connection->state, self->connection->db);
`
872
``
`-
return NULL;
`
873
``
`-
}
`
874
``
-
875
``
`-
if (rc == SQLITE_ROW) {
`
876
``
`-
self->next_row = _pysqlite_fetch_one_row(self);
`
877
``
`-
if (self->next_row == NULL) {
`
878
``
`-
(void)pysqlite_statement_reset(self->statement);
`
879
``
`-
return NULL;
`
880
``
`-
}
`
881
``
`-
}
`
``
837
`+
PyObject *row = _pysqlite_fetch_one_row(self);
`
``
838
`+
if (row == NULL) {
`
``
839
`+
return NULL;
`
882
840
` }
`
883
``
-
884
``
`-
return next_row;
`
``
841
`+
int rc = pysqlite_step(stmt);
`
``
842
`+
if (rc == SQLITE_DONE) {
`
``
843
`+
(void)pysqlite_statement_reset(self->statement);
`
``
844
`+
}
`
``
845
`+
else if (rc != SQLITE_ROW) {
`
``
846
`+
(void)_pysqlite_seterror(self->connection->state,
`
``
847
`+
self->connection->db);
`
``
848
`+
Py_DECREF(row);
`
``
849
`+
return NULL;
`
``
850
`+
}
`
``
851
`+
if (!Py_IsNone(self->row_factory)) {
`
``
852
`+
PyObject *factory = self->row_factory;
`
``
853
`+
PyObject *args[] = { (PyObject *)self, row, };
`
``
854
`+
PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL);
`
``
855
`+
Py_DECREF(row);
`
``
856
`+
row = new_row;
`
``
857
`+
}
`
``
858
`+
return row;
`
885
859
`}
`
886
860
``
887
861
`/*[clinic input]
`