bpo-32788: Better error handling in sqlite3. by serhiy-storchaka · Pull Request #3723 · python/cpython
Expand Up
@@ -102,24 +102,33 @@ def __conform__(self, protocol):
def __str__(self):
return "<%s>" % self.val
class BadConform: def __init__(self, exc): self.exc = exc def __conform__(self, protocol): raise self.exc
def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) self.cur = self.con.cursor() self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5))") self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5), bad bad)")
# override float, make them always return the same number sqlite.converters["FLOAT"] = lambda x: 47.2
# and implement two custom ones sqlite.converters["BOOL"] = lambda x: bool(int(x)) sqlite.converters["FOO"] = DeclTypesTests.Foo sqlite.converters["BAD"] = DeclTypesTests.BadConform sqlite.converters["WRONG"] = lambda x: "WRONG" sqlite.converters["NUMBER"] = float
def tearDown(self): del sqlite.converters["FLOAT"] del sqlite.converters["BOOL"] del sqlite.converters["FOO"] del sqlite.converters["BAD"] del sqlite.converters["WRONG"] del sqlite.converters["NUMBER"] self.cur.close() self.con.close() Expand Down Expand Up @@ -159,13 +168,13 @@ def CheckBool(self): self.cur.execute("insert into test(b) values (?)", (False,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], False) self.assertIs(row[0], False)
self.cur.execute("delete from test") self.cur.execute("insert into test(b) values (?)", (True,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], True) self.assertIs(row[0], True)
def CheckUnicode(self): # default Expand All @@ -182,6 +191,19 @@ def CheckFoo(self): row = self.cur.fetchone() self.assertEqual(row[0], val)
def CheckErrorInConform(self): val = DeclTypesTests.BadConform(TypeError) with self.assertRaises(sqlite.InterfaceError): self.cur.execute("insert into test(bad) values (?)", (val,)) with self.assertRaises(sqlite.InterfaceError): self.cur.execute("insert into test(bad) values (:val)", {"val": val})
val = DeclTypesTests.BadConform(KeyboardInterrupt) with self.assertRaises(KeyboardInterrupt): self.cur.execute("insert into test(bad) values (?)", (val,)) with self.assertRaises(KeyboardInterrupt): self.cur.execute("insert into test(bad) values (:val)", {"val": val})
def CheckUnsupportedSeq(self): class Bar: pass val = Bar() Expand Down
class BadConform: def __init__(self, exc): self.exc = exc def __conform__(self, protocol): raise self.exc
def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) self.cur = self.con.cursor() self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5))") self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob, n1 number, n2 number(5), bad bad)")
# override float, make them always return the same number sqlite.converters["FLOAT"] = lambda x: 47.2
# and implement two custom ones sqlite.converters["BOOL"] = lambda x: bool(int(x)) sqlite.converters["FOO"] = DeclTypesTests.Foo sqlite.converters["BAD"] = DeclTypesTests.BadConform sqlite.converters["WRONG"] = lambda x: "WRONG" sqlite.converters["NUMBER"] = float
def tearDown(self): del sqlite.converters["FLOAT"] del sqlite.converters["BOOL"] del sqlite.converters["FOO"] del sqlite.converters["BAD"] del sqlite.converters["WRONG"] del sqlite.converters["NUMBER"] self.cur.close() self.con.close() Expand Down Expand Up @@ -159,13 +168,13 @@ def CheckBool(self): self.cur.execute("insert into test(b) values (?)", (False,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], False) self.assertIs(row[0], False)
self.cur.execute("delete from test") self.cur.execute("insert into test(b) values (?)", (True,)) self.cur.execute("select b from test") row = self.cur.fetchone() self.assertEqual(row[0], True) self.assertIs(row[0], True)
def CheckUnicode(self): # default Expand All @@ -182,6 +191,19 @@ def CheckFoo(self): row = self.cur.fetchone() self.assertEqual(row[0], val)
def CheckErrorInConform(self): val = DeclTypesTests.BadConform(TypeError) with self.assertRaises(sqlite.InterfaceError): self.cur.execute("insert into test(bad) values (?)", (val,)) with self.assertRaises(sqlite.InterfaceError): self.cur.execute("insert into test(bad) values (:val)", {"val": val})
val = DeclTypesTests.BadConform(KeyboardInterrupt) with self.assertRaises(KeyboardInterrupt): self.cur.execute("insert into test(bad) values (?)", (val,)) with self.assertRaises(KeyboardInterrupt): self.cur.execute("insert into test(bad) values (:val)", {"val": val})
def CheckUnsupportedSeq(self): class Bar: pass val = Bar() Expand Down