diff --git a/Lib/sqlite3/dump.py b/Lib/sqlite3/dump.py index 1cf8759f897..cf417f2193a 100644 --- a/Lib/sqlite3/dump.py +++ b/Lib/sqlite3/dump.py @@ -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. diff --git a/Lib/test/test_sqlite3/test_dump.py b/Lib/test/test_sqlite3/test_dump.py index c3ed3aefef0..1dcce87c5e7 100644 --- a/Lib/test/test_sqlite3/test_dump.py +++ b/Lib/test/test_sqlite3/test_dump.py @@ -117,6 +117,21 @@ class DumpTests(unittest.TestCase): 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 = [ diff --git a/Misc/NEWS.d/next/Library/2024-04-24-12-29-33.gh-issue-118221.2k_bac.rst b/Misc/NEWS.d/next/Library/2024-04-24-12-29-33.gh-issue-118221.2k_bac.rst new file mode 100644 index 00000000000..9b0ea9978a1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-24-12-29-33.gh-issue-118221.2k_bac.rst @@ -0,0 +1,2 @@ +Fix a bug where :func:`sqlite3.iterdump` could fail if a custom :attr:`row +factory ` was used. Patch by Erlend Aasland.