Skip to content

Commit 48ea298

Browse files
pythongh-118221: Always use the default row factory in sqlite3.iterdump()
The iterdump() implementation depends on the row factory returning resulting rows as tuples; it will fail with custom row factories like for example a dict factory. Fixed by overriding the row factory of the cursor used by iterdump(). FTR, this does not affect the row factory of the parent connection.
1 parent 692e902 commit 48ea298

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 {k: v for k, v in 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.Cursor.row_factory>` was supplied. Patch by Erlend Aasland.

0 commit comments

Comments
 (0)