Skip to content

Commit 34930cb

Browse files
Compat with other backends: silently coerce keys to bytes
1 parent b1b9a9b commit 34930cb

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Lib/dbm/sqlite3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
)
1313
"""
1414
GET_SIZE = "SELECT COUNT (key) FROM Dict"
15-
LOOKUP_KEY = "SELECT value FROM Dict WHERE key = ?"
15+
LOOKUP_KEY = "SELECT value FROM Dict WHERE key = CAST(? AS BLOB)"
1616
STORE_KV = "REPLACE INTO Dict (key, value) VALUES (CAST(? AS BLOB), CAST(? AS BLOB))"
17-
DELETE_KEY = "DELETE FROM Dict WHERE key = ?"
17+
DELETE_KEY = "DELETE FROM Dict WHERE key = CAST(? AS BLOB)"
1818
ITER_KEYS = "SELECT key FROM Dict"
1919

2020

Lib/test/test_dbm_sqlite3.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class DataTypes(_SQLiteDbmTests):
213213
(42, b"42"),
214214
(3.14, b"3.14"),
215215
("string", b"string"),
216+
(b"bytes", b"bytes"),
216217
)
217218

218219
def setUp(self):
@@ -234,10 +235,15 @@ def test_datatypes_keys(self):
234235
with self.subTest(raw=raw, coerced=coerced):
235236
self.db[raw] = "value"
236237
self.assertEqual(self.db[coerced], b"value")
237-
with self.assertRaises(KeyError):
238-
self.db[raw]
239-
with self.assertRaises(KeyError):
240-
del self.db[raw]
238+
# Raw keys are silently coerced to bytes.
239+
self.assertEqual(self.db[raw], b"value")
240+
del self.db[raw]
241+
242+
def test_datatypes_replace_coerced(self):
243+
self.db["10"] = "value"
244+
self.db[b"10"] = "value"
245+
self.db[10] = "value"
246+
self.assertEqual(self.db.keys(), [b"10"])
241247

242248

243249
class CorruptDatabase(_SQLiteDbmTests):

0 commit comments

Comments
 (0)