diff --git a/Lib/sqlite3/dump.py b/Lib/sqlite3/dump.py --- a/Lib/sqlite3/dump.py +++ b/Lib/sqlite3/dump.py @@ -25,9 +25,10 @@ FROM "sqlite_master" WHERE "sql" NOT NULL AND "type" == 'table' + ORDER BY "name"; """ - schema_res = cu.execute(q) - for table_name, type, sql in sorted(schema_res.fetchall()): + schema_res = cu.execute(q).fetchall() + for table_name, type, sql in schema_res: if table_name == 'sqlite_sequence': yield('DELETE FROM "sqlite_sequence";') elif table_name == 'sqlite_stat1': diff --git a/Lib/sqlite3/test/dump.py b/Lib/sqlite3/test/dump.py --- a/Lib/sqlite3/test/dump.py +++ b/Lib/sqlite3/test/dump.py @@ -49,6 +49,27 @@ [self.assertEqual(expected_sqls[i], actual_sqls[i]) for i in range(len(expected_sqls))] + def CheckUnorderableRow(self): + # iterdump() should be able to cope with unorderable row types (issue #15545) + class UnorderableRow: + def __init__(self, cursor, row): + self.row = row + def __getitem__(self, index): + return self.row[index] + self.cx.row_factory = UnorderableRow + CREATE_ALPHA = """CREATE TABLE "alpha" ("one");""" + CREATE_BETA = """CREATE TABLE "beta" ("two");""" + expected = [ + "BEGIN TRANSACTION;", + CREATE_ALPHA, + CREATE_BETA, + "COMMIT;" + ] + self.cu.execute(CREATE_BETA) + self.cu.execute(CREATE_ALPHA) + got = list(self.cx.iterdump()) + self.assertEqual(expected, got) + def suite(): return unittest.TestSuite(unittest.makeSuite(DumpTests, "Check"))