Skip to content

Commit e38b43c

Browse files
erlend-aaslandfelixxmserhiy-storchaka
authored
gh-118221: Always use the default row factory in sqlite3.iterdump() (#118223)
sqlite3.iterdump() depends on the row factory returning resulting rows as tuples; it will fail with custom row factories like for example a dict factory. With this commit, we explicitly reset the row factory of the cursor used by iterdump(), so we always get predictable results. This does not affect the row factory of the parent connection. Co-authored-by: Mariusz Felisiak <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 796b3fb commit e38b43c

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Lib/sqlite3/dump.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def _iterdump(connection, *, filter=None):
2626

2727
writeable_schema = False
2828
cu = connection.cursor()
29+
cu.row_factory = None # Make sure we get predictable results.
2930
# Disable foreign key constraints, if there is any foreign key violation.
3031
violations = cu.execute("PRAGMA foreign_key_check").fetchall()
3132
if violations:

Lib/test/test_sqlite3/test_dump.py

+15
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ def __getitem__(self, index):
190190
got = list(self.cx.iterdump())
191191
self.assertEqual(expected, got)
192192

193+
def test_dump_custom_row_factory(self):
194+
# gh-118221: iterdump should be able to cope with custom row factories.
195+
def dict_factory(cu, row):
196+
fields = [col[0] for col in cu.description]
197+
return dict(zip(fields, row))
198+
199+
self.cx.row_factory = dict_factory
200+
CREATE_TABLE = "CREATE TABLE test(t);"
201+
expected = ["BEGIN TRANSACTION;", CREATE_TABLE, "COMMIT;"]
202+
203+
self.cu.execute(CREATE_TABLE)
204+
actual = list(self.cx.iterdump())
205+
self.assertEqual(expected, actual)
206+
self.assertEqual(self.cx.row_factory, dict_factory)
207+
193208
def test_dump_virtual_tables(self):
194209
# gh-64662
195210
expected = [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug where :func:`sqlite3.iterdump` could fail if a custom :attr:`row
2+
factory <sqlite3.Connection.row_factory>` was used. Patch by Erlend Aasland.

0 commit comments

Comments
 (0)