]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#15545: sort iterdump via SQL instead of in python code
authorR David Murray <rdmurray@bitdance.com>
Thu, 10 Jan 2013 16:30:51 +0000 (11:30 -0500)
committerR David Murray <rdmurray@bitdance.com>
Thu, 10 Jan 2013 16:30:51 +0000 (11:30 -0500)
Although there is not a regression in Python2, we make the same update here to
keep the code bases in sync.

(The fix for issue 9750 introduced a regression in Python 3 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.

Lib/sqlite3/dump.py
Lib/sqlite3/test/dump.py
Misc/NEWS

index da6be68663e502eecd52fe8d8acf5b322a6b720a..de9c368be3014e7e55f56a69457a003854381629 100644 (file)
@@ -25,9 +25,10 @@ def _iterdump(connection):
         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()):
+    for table_name, type, sql in schema_res.fetchall():
         if table_name == 'sqlite_sequence':
             yield('DELETE FROM "sqlite_sequence";')
         elif table_name == 'sqlite_stat1':
index 2e9b436f30a0f8ca4403a72dbab1d4867296e114..195c13942a018a94e2bc88b08ec32966bc7860e5 100644 (file)
@@ -49,6 +49,27 @@ class DumpTests(unittest.TestCase):
         [self.assertEqual(expected_sqls[i], actual_sqls[i])
             for i in xrange(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"))
 
index 7e29e5450f83a77245402426135ce5e8e8e34f8c..6bb2549a124268d59bced4401ef0d3ddd5aecc31 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -186,6 +186,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #15545: Fix regression in sqlite3's iterdump method where it was
+  failing if the connection used a row factory (such as sqlite3.Row) that
+  produced unsortable objects. (Regression was introduced by fix for 9750).
+
 - Issue #16828: Fix error incorrectly raised by bz2.compress(''). Patch by
   Martin Packman.