Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#490 closed defect (fixed)

[patch] incorrect handling of cursor.rowcount

Reported by: eugene@… Owned by: adrian
Component: Core (Management commands) Version:
Severity: normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

According to http://www.python.org/peps/pep-0249.html cursor.rowcount can be -1:

The attribute is -1 in case no executeXXX() has been
performed on the cursor or the rowcount of the last
operation is not determinable by the interface. [7]

E.g., SQLite3 returns -1 (hardcoded?).

This patch should fix it:

Index: management.py
===================================================================
--- management.py	(revision 635)
+++ management.py	(working copy)
@@ -218,7 +218,12 @@
 
     # Check that the package exists in the database.
     cursor.execute("SELECT 1 FROM packages WHERE label = %s", [app_label])
-    if cursor.rowcount < 1:
+    if cursor.rowcount < 0:
+        # can be -1 (==undetermined) according to http://www.python.org/peps/pep-0249.html
+        flag = cursor.fetchone() is None
+    else:
+        flag = cursor.rowcount < 1
+    if flag:
 #         sys.stderr.write("The '%s' package isn't installed.\n" % app_label)
         print _get_packages_insert(app_label)
 
@@ -232,11 +237,21 @@
         contenttypes_seen[opts.module_name] = 1
         for codename, name in perms:
             cursor.execute("SELECT 1 FROM auth_permissions WHERE package = %s AND codename = %s", (app_label, codename))
-            if cursor.rowcount < 1:
+            if cursor.rowcount < 0:
+                # can be -1 (==undetermined) according to http://www.python.org/peps/pep-0249.html
+                flag = cursor.fetchone() is None
+            else:
+                flag = cursor.rowcount < 1
+            if flag:
 #                 sys.stderr.write("The '%s.%s' permission doesn't exist.\n" % (app_label, codename))
                 print _get_permission_insert(name, codename, opts)
         cursor.execute("SELECT 1 FROM content_types WHERE package = %s AND python_module_name = %s", (app_label, opts.module_name))
-        if cursor.rowcount < 1:
+        if cursor.rowcount < 0:
+            # can be -1 (==undetermined) according to http://www.python.org/peps/pep-0249.html
+            flag = cursor.fetchone() is None
+        else:
+            flag = cursor.rowcount < 1
+        if flag:
 #             sys.stderr.write("The '%s.%s' content type doesn't exist.\n" % (app_label, opts.module_name))
             print _get_contenttype_insert(opts)

Attachments (1)

management.patch (1.9 KB) - added by eugene@… 10 years ago.
patch

Download all attachments as: .zip

Change History (2)

Changed 10 years ago by eugene@…

patch

comment:1 Changed 10 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [1483]) Fixed #490 -- Fixed incorrect handling of cursor.rowcount in yet-unused database_check functionality. Thanks, Eugene

Note: See TracTickets for help on using tickets.
Back to Top