Skip to content

Commit c5bc2a6

Browse files
Add tests and a news entry.
1 parent 369164a commit c5bc2a6

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Lib/sqlite3/test/types.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,33 @@ def __conform__(self, protocol):
102102
def __str__(self):
103103
return "<%s>" % self.val
104104

105+
class BadConform:
106+
def __init__(self, exc):
107+
self.exc = exc
108+
def __conform__(self, protocol):
109+
raise self.exc
110+
105111
def setUp(self):
106112
self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
107113
self.cur = self.con.cursor()
108-
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))")
114+
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)")
109115

110116
# override float, make them always return the same number
111117
sqlite.converters["FLOAT"] = lambda x: 47.2
112118

113119
# and implement two custom ones
114120
sqlite.converters["BOOL"] = lambda x: bool(int(x))
115121
sqlite.converters["FOO"] = DeclTypesTests.Foo
122+
sqlite.converters["BAD"] = DeclTypesTests.BadConform
116123
sqlite.converters["WRONG"] = lambda x: "WRONG"
117124
sqlite.converters["NUMBER"] = float
118125

119126
def tearDown(self):
120127
del sqlite.converters["FLOAT"]
121128
del sqlite.converters["BOOL"]
122129
del sqlite.converters["FOO"]
130+
del sqlite.converters["BAD"]
131+
del sqlite.converters["WRONG"]
123132
del sqlite.converters["NUMBER"]
124133
self.cur.close()
125134
self.con.close()
@@ -159,13 +168,13 @@ def CheckBool(self):
159168
self.cur.execute("insert into test(b) values (?)", (False,))
160169
self.cur.execute("select b from test")
161170
row = self.cur.fetchone()
162-
self.assertEqual(row[0], False)
171+
self.assertIs(row[0], False)
163172

164173
self.cur.execute("delete from test")
165174
self.cur.execute("insert into test(b) values (?)", (True,))
166175
self.cur.execute("select b from test")
167176
row = self.cur.fetchone()
168-
self.assertEqual(row[0], True)
177+
self.assertIs(row[0], True)
169178

170179
def CheckUnicode(self):
171180
# default
@@ -182,6 +191,19 @@ def CheckFoo(self):
182191
row = self.cur.fetchone()
183192
self.assertEqual(row[0], val)
184193

194+
def CheckErrorInConform(self):
195+
val = DeclTypesTests.BadConform(TypeError)
196+
with self.assertRaises(sqlite.InterfaceError):
197+
self.cur.execute("insert into test(bad) values (?)", (val,))
198+
with self.assertRaises(sqlite.InterfaceError):
199+
self.cur.execute("insert into test(bad) values (:val)", {"val": val})
200+
201+
val = DeclTypesTests.BadConform(KeyboardInterrupt)
202+
with self.assertRaises(KeyboardInterrupt):
203+
self.cur.execute("insert into test(bad) values (?)", (val,))
204+
with self.assertRaises(KeyboardInterrupt):
205+
self.cur.execute("insert into test(bad) values (:val)", {"val": val})
206+
185207
def CheckUnsupportedSeq(self):
186208
class Bar: pass
187209
val = Bar()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Errors other than :exc:`TypeError` raised in methods ``__adapt__()`` and
2+
``__conform__()`` in the :mod:`sqlite3` module are now propagated to the
3+
user.

0 commit comments

Comments
 (0)