Skip to content

Commit

Permalink
[3.12] gh-118221: Always use the default row factory in sqlite3.iterd…
Browse files Browse the repository at this point in the history
…ump() (#118223) (#118270)

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 <felisiak.mariusz@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent fc1732c commit 7a843e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def _iterdump(connection):

writeable_schema = False
cu = connection.cursor()
cu.row_factory = None # Make sure we get predictable results.
yield('BEGIN TRANSACTION;')

# sqlite_master table contains the SQL CREATE statements for the database.
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ def __getitem__(self, index):
got = list(self.cx.iterdump())
self.assertEqual(expected, got)

def test_dump_custom_row_factory(self):
# gh-118221: iterdump should be able to cope with custom row factories.
def dict_factory(cu, row):
fields = [col[0] for col in cu.description]
return dict(zip(fields, row))

self.cx.row_factory = dict_factory
CREATE_TABLE = "CREATE TABLE test(t);"
expected = ["BEGIN TRANSACTION;", CREATE_TABLE, "COMMIT;"]

self.cu.execute(CREATE_TABLE)
actual = list(self.cx.iterdump())
self.assertEqual(expected, actual)
self.assertEqual(self.cx.row_factory, dict_factory)

def test_dump_virtual_tables(self):
# gh-64662
expected = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug where :func:`sqlite3.iterdump` could fail if a custom :attr:`row
factory <sqlite3.Connection.row_factory>` was used. Patch by Erlend Aasland.

0 comments on commit 7a843e8

Please sign in to comment.