Issue #27861: Fixed a crash in sqlite3.Connection.cursor() when a factory

creates not a cursor.  Patch by Xiang Zhang.
This commit is contained in:
Serhiy Storchaka 2016-08-29 14:29:55 +03:00
parent 5de141f157
commit ef113cd4cc
4 changed files with 35 additions and 9 deletions

View file

@ -58,8 +58,20 @@ class CursorFactoryTests(unittest.TestCase):
self.con.close()
def CheckIsInstance(self):
cur = self.con.cursor(factory=MyCursor)
cur = self.con.cursor()
self.assertIsInstance(cur, sqlite.Cursor)
cur = self.con.cursor(MyCursor)
self.assertIsInstance(cur, MyCursor)
cur = self.con.cursor(factory=lambda con: MyCursor(con))
self.assertIsInstance(cur, MyCursor)
def CheckInvalidFactory(self):
# not a callable at all
self.assertRaises(TypeError, self.con.cursor, None)
# invalid callable with not exact one argument
self.assertRaises(TypeError, self.con.cursor, lambda: None)
# invalid callable returning non-cursor
self.assertRaises(TypeError, self.con.cursor, lambda con: None)
class RowFactoryTestsBackwardsCompat(unittest.TestCase):
def setUp(self):
@ -183,10 +195,12 @@ class RowFactoryTests(unittest.TestCase):
def CheckFakeCursorClass(self):
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
# segmentation fault.
# Issue #27861: Also applies for cursor factory.
class FakeCursor(str):
__class__ = sqlite.Cursor
cur = self.con.cursor(factory=FakeCursor)
self.assertRaises(TypeError, sqlite.Row, cur, ())
self.con.row_factory = sqlite.Row
self.assertRaises(TypeError, self.con.cursor, FakeCursor)
self.assertRaises(TypeError, sqlite.Row, FakeCursor(), ())
def tearDown(self):
self.con.close()