Skip to content

Commit 6c9765f

Browse files
committed
Clarify dbm type support and extend type coverage tests
1 parent 1012be7 commit 6c9765f

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

Doc/library/dbm.rst

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,16 @@ will automatically close them when done.
127127
Key and Value Types
128128
-------------------
129129

130-
The accepted types for keys and values vary by backend:
130+
The accepted types for keys and values vary by backend. Keys and values are
131+
handled identically:
131132

132-
**Keys:**
133+
* **Traditional backends**:
133134

134-
* **All backends**: Accept :class:`str` and :class:`bytes` objects
135-
* **String keys** are automatically converted to bytes using the default
136-
encoding
137-
* **Bytes keys** are stored as-is
138-
139-
**Values:**
140-
141-
* **Traditional backends** (``dbm.gnu``, ``dbm.ndbm``, ``dbm.dumb``): Only
142-
accept :class:`str` and :class:`bytes` objects
143-
* **SQLite backend** (``dbm.sqlite3``): Accepts any object that can be
144-
converted to bytes:
145-
146-
* **Accepted**: :class:`str`, :class:`bytes`, :class:`int`,
147-
:class:`float`, :class:`bool`
148-
* **Rejected**: :class:`None`, :class:`list`, :class:`dict`,
149-
:class:`tuple`, custom objects
135+
* :mod:`dbm.gnu` and :mod:`dbm.ndbm`: Accept :class:`str` and :class:`bytes` objects
136+
* :mod:`dbm.dumb`: Accepts :class:`str`, :class:`bytes`, and :class:`bytearray` objects
137+
* **SQLite backend** (:mod:`dbm.sqlite3`): Accepts :class:`str`, :class:`bytes`,
138+
:class:`int`, :class:`float`, :class:`bool`, :class:`bytearray`,
139+
:class:`memoryview`, and :class:`array.array` objects
150140

151141
**Storage Format:**
152142

@@ -157,9 +147,13 @@ type stored.
157147
**Type Conversion Examples:**
158148

159149
* ``db['key'] = 'string'`` stored as ``b'string'``
160-
* ``db['key'] = 42`` stored as ``b'42'`` (sqlite3 only)
161-
* ``db['key'] = 3.14`` stored as ``b'3.14'`` (sqlite3 only)
162-
* ``db['key'] = True`` stored as ``b'True'`` (sqlite3 only)
150+
* ``db['key'] = bytearray(b'data')`` stored as ``b'data'`` (:mod:`dbm.dumb` and :mod:`dbm.sqlite3` only)
151+
* ``db['key'] = 42`` stored as ``b'42'`` (:mod:`dbm.sqlite3` only)
152+
* ``db['key'] = 3.14`` stored as ``b'3.14'`` (:mod:`dbm.sqlite3` only)
153+
* ``db['key'] = True`` stored as ``b'1'`` (:mod:`dbm.sqlite3` only)
154+
* ``db['key'] = False`` stored as ``b'0'`` (:mod:`dbm.sqlite3` only)
155+
* ``db['key'] = memoryview(b'data')`` stored as ``b'data'`` (:mod:`dbm.sqlite3` only)
156+
* ``db['key'] = array.array('i', [1, 2, 3])`` stored as binary data (:mod:`dbm.sqlite3` only)
163157
* ``db['key'] = None`` fails on all backends
164158
* ``db['key'] = [1, 2, 3]`` fails on all backends
165159

Lib/test/test_dbm_dumb.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ def test_str_write_contains(self):
130130
self._dict['\u00fc'.encode('utf-8')])
131131
self.assertEqual(f[b'1'], b'a')
132132

133+
def test_bytearray(self):
134+
with contextlib.closing(dumbdbm.open(_fname, 'n')) as f:
135+
f['key'] = bytearray(b'bytearray_value')
136+
self.assertEqual(f[b'key'], b'bytearray_value')
137+
f[bytearray(b'bytearray_key')] = b'value'
138+
self.assertEqual(f[b'bytearray_key'], b'value')
139+
133140
def test_line_endings(self):
134141
# test for bug #1172763: dumbdbm would die if the line endings
135142
# weren't what was expected.

Lib/test/test_dbm_sqlite3.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ class DataTypes(_SQLiteDbmTests):
216216
(3.14, b"3.14"),
217217
("string", b"string"),
218218
(b"bytes", b"bytes"),
219+
(True, b"1"),
220+
(False, b"0"),
221+
(bytearray(b"bytearray"), b"bytearray"),
222+
(memoryview(b"memoryview"), b"memoryview"),
219223
)
220224

221225
def setUp(self):

0 commit comments

Comments
 (0)