[pypy-commit] pypy default: sqlite3: fix fetching of cursor.description attr

Roman Podoliaka noreply at buildbot.pypy.org
Fri Dec 12 07:58:00 CET 2014


Author: Roman Podoliaka <roman.podoliaka at gmail.com>
Branch: 
Changeset: r74893:fa1e46300cb1
Date: 2014-12-11 15:13 +0200
http://bitbucket.org/pypy/pypy/changeset/fa1e46300cb1/

Log:	sqlite3: fix fetching of cursor.description attr

	According to PEP-249 (DB API) cursor attribute 'description' will be
	None, if the cursor has not had an operation invoked via the
	.execute*() method yet, but this snippet currently fails on PyPy:

	 import sqlite3

	 db = sqlite3.connect(':memory:') cur = db.cursor() try:
	print(cur.description) finally: cur.close()

	 This commit fixes the existing behaviour to comply with the PEP.

diff --git a/lib-python/2.7/sqlite3/test/dbapi.py b/lib-python/2.7/sqlite3/test/dbapi.py
--- a/lib-python/2.7/sqlite3/test/dbapi.py
+++ b/lib-python/2.7/sqlite3/test/dbapi.py
@@ -478,6 +478,29 @@
         except TypeError:
             pass
 
+    def CheckCurDescription(self):
+        self.cu.execute("select * from test")
+
+        actual = self.cu.description
+        expected = [
+            ('id', None, None, None, None, None, None),
+            ('name', None, None, None, None, None, None),
+            ('income', None, None, None, None, None, None),
+        ]
+        self.assertEqual(expected, actual)
+
+    def CheckCurDescriptionVoidStatement(self):
+        self.cu.execute("insert into test(name) values (?)", ("foo",))
+        self.assertIsNone(self.cu.description)
+
+    def CheckCurDescriptionWithoutStatement(self):
+        cu = self.cx.cursor()
+        try:
+            self.assertIsNone(cu.description)
+        finally:
+            cu.close()
+
+
 @unittest.skipUnless(threading, 'This test requires threading.')
 class ThreadTests(unittest.TestCase):
     def setUp(self):
diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -1175,8 +1175,11 @@
         try:
             return self.__description
         except AttributeError:
-            self.__description = self.__statement._get_description()
-            return self.__description
+            try:
+                self.__description = self.__statement._get_description()
+                return self.__description
+            except AttributeError:
+                return None
     description = property(__get_description)
 
     def __get_lastrowid(self):


More information about the pypy-commit mailing list