Skip to content

Commit 72af303

Browse files
committed
Clarify dbm.dumb bytearray handling and cover with test
1 parent 2ba913c commit 72af303

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Doc/library/dbm.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ Key and Value Types
130130
The accepted types for keys and values vary by backend. Keys and values are
131131
handled identically:
132132

133-
* **Traditional backends**: :mod:`dbm.gnu`, :mod:`dbm.ndbm`, and :mod:`dbm.dumb` accept :class:`str` and :class:`bytes` objects.
133+
* **Traditional backends**:
134+
135+
* :mod:`dbm.gnu` and :mod:`dbm.ndbm`: Accept :class:`str` and :class:`bytes` objects.
136+
* :mod:`dbm.dumb`: Accepts :class:`str` and :class:`bytes` objects; :class:`bytearray` is acceptable as a value, but not as a key.
134137
* **SQLite backend** (:mod:`dbm.sqlite3`): Accepts :class:`str`, :class:`bytes`,
135138
:class:`int`, :class:`float`, :class:`bool`, :class:`bytearray`,
136139
:class:`memoryview`, and :class:`array.array` objects.
@@ -151,8 +154,6 @@ type stored.
151154
* ``db['key'] = False`` stored as ``b'0'`` (:mod:`dbm.sqlite3` only)
152155
* ``db['key'] = memoryview(b'data')`` stored as ``b'data'`` (:mod:`dbm.sqlite3` only)
153156
* ``db['key'] = array.array('i', [1, 2, 3])`` stored as bytes (e.g. on little-endian: ``b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'``) (:mod:`dbm.sqlite3` only)
154-
* ``db['key'] = None`` fails on all backends
155-
* ``db['key'] = [1, 2, 3]`` fails on all backends
156157

157158

158159
The following example records some hostnames and a corresponding title, and

Lib/test/test_dbm_dumb.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class DumbDBMTestCase(unittest.TestCase):
3131
b'd': b'way',
3232
b'f': b'Guido',
3333
b'g': b'intended',
34+
b'h': bytearray(b'bytearray_value'),
3435
'\u00fc'.encode('utf-8') : b'!',
3536
}
3637

@@ -368,6 +369,14 @@ def test_nonascii_filename(self):
368369
self.assertTrue(b'key' in db)
369370
self.assertEqual(db[b'key'], b'value')
370371

372+
def test_bytearray(self):
373+
self.init_db()
374+
with contextlib.closing(dumbdbm.open(_fname, 'r')) as f:
375+
self.assertEqual(f[b'h'], b'bytearray_value')
376+
with contextlib.closing(dumbdbm.open(_fname)) as f:
377+
with self.assertRaises(TypeError):
378+
f[bytearray(b'bytearray_key')] = b'value'
379+
371380
def test_open_with_pathlib_path(self):
372381
dumbdbm.open(os_helper.FakePath(_fname), "c").close()
373382

0 commit comments

Comments
 (0)