Skip to content

Commit b523129

Browse files
committed
python#15545: fix sqlite3.iterdump regression on unsortable row_factory objects.
The fix for issue 9750 introduced a regression by sorting the row objects returned by fetchall. But if a row_factory such as sqlite3.Row is used, the rows may not be sortable (in Python3), which leads to an exception. The sorting is still a nice idea, so the patch moves the sort into the sql. Fix and test by Peter Otten.
1 parent 78470b4 commit b523129

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Lib/sqlite3/dump.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ def _iterdump(connection):
2525
FROM "sqlite_master"
2626
WHERE "sql" NOT NULL AND
2727
"type" == 'table'
28+
ORDER BY "name"
2829
"""
2930
schema_res = cu.execute(q)
30-
for table_name, type, sql in sorted(schema_res.fetchall()):
31+
for table_name, type, sql in schema_res.fetchall():
3132
if table_name == 'sqlite_sequence':
3233
yield('DELETE FROM "sqlite_sequence";')
3334
elif table_name == 'sqlite_stat1':

Lib/sqlite3/test/dump.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ def CheckTableDump(self):
4949
[self.assertEqual(expected_sqls[i], actual_sqls[i])
5050
for i in range(len(expected_sqls))]
5151

52+
def CheckUnorderableRow(self):
53+
# iterdump() should be able to cope with unorderable row types (issue #15545)
54+
class UnorderableRow:
55+
def __init__(self, cursor, row):
56+
self.row = row
57+
def __getitem__(self, index):
58+
return self.row[index]
59+
self.cx.row_factory = UnorderableRow
60+
CREATE_ALPHA = """CREATE TABLE "alpha" ("one");"""
61+
CREATE_BETA = """CREATE TABLE "beta" ("two");"""
62+
expected = [
63+
"BEGIN TRANSACTION;",
64+
CREATE_ALPHA,
65+
CREATE_BETA,
66+
"COMMIT;"
67+
]
68+
self.cu.execute(CREATE_BETA)
69+
self.cu.execute(CREATE_ALPHA)
70+
got = list(self.cx.iterdump())
71+
self.assertEqual(expected, got)
72+
5273
def suite():
5374
return unittest.TestSuite(unittest.makeSuite(DumpTests, "Check"))
5475

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ Core and Builtins
199199
Library
200200
-------
201201

202+
- Issue #15545: Fix regression in sqlite3's iterdump method where it was
203+
failing if the connection used a row factory (such as sqlite3.Row) that
204+
produced unsortable objects. (Regression was introduced by fix for 9750).
205+
202206
- Issue #16491: IDLE now prints chained exception tracebacks.
203207

204208
- Issue #16828: Fix error incorrectly raised by bz2.compress(''). Patch by

0 commit comments

Comments
 (0)