@@ -94,7 +94,8 @@ The :meth:`!setdefault` method requires two arguments.
9494
9595Key and values are always stored as :class: `bytes `. This means that when
9696strings are used they are implicitly converted to the default encoding before
97- being stored.
97+ being stored. For detailed information about accepted types, see
98+ :ref: `dbm-key-value-types `.
9899
99100These objects also support being used in a :keyword: `with ` statement, which
100101will automatically close them when done.
@@ -115,6 +116,47 @@ will automatically close them when done.
115116 :meth: `!clear ` methods are now available for all :mod: `!dbm ` backends.
116117
117118
119+ .. _dbm-key-value-types :
120+
121+ Key and value types
122+ -------------------
123+
124+ The accepted types for keys and values vary by backend. Keys and values are
125+ handled the same way, except where noted below:
126+
127+ * **Traditional backends: **
128+
129+ * :mod: `dbm.gnu ` and :mod: `dbm.ndbm `: Accept :class: `str ` and :class: `bytes `
130+ objects.
131+ * :mod: `dbm.dumb `: Accepts :class: `str ` and :class: `bytes ` objects;
132+ :class: `bytearray ` is acceptable as a value, but not as a key.
133+
134+ * **SQLite backend ** (:mod: `dbm.sqlite3 `): Accepts :class: `str `, :class: `bytes `,
135+ :class: `int `, :class: `float `, :class: `bool `, :class: `bytearray `,
136+ :class: `memoryview `, and :class: `array.array ` objects.
137+
138+ **Storage format: **
139+
140+ All keys and values are stored as :class: `bytes ` objects in the database.
141+ When retrieving, you'll always get bytes back, regardless of the original
142+ type stored.
143+
144+ **Type conversion examples: **
145+
146+ * ``db['key'] = 'string' `` stored as ``b'string' ``
147+ * ``db['key'] = bytearray(b'data') `` stored as ``b'data' ``
148+ (:mod: `dbm.dumb ` and :mod: `dbm.sqlite3 ` only)
149+ * ``db['key'] = 42 `` stored as ``b'42' `` (:mod: `dbm.sqlite3 ` only)
150+ * ``db['key'] = 3.14 `` stored as ``b'3.14' `` (:mod: `dbm.sqlite3 ` only)
151+ * ``db['key'] = True `` stored as ``b'1' `` (:mod: `dbm.sqlite3 ` only)
152+ * ``db['key'] = False `` stored as ``b'0' `` (:mod: `dbm.sqlite3 ` only)
153+ * ``db['key'] = memoryview(b'data') `` stored as ``b'data' ``
154+ (:mod: `dbm.sqlite3 ` only)
155+ * ``db['key'] = array.array('i', [1, 2, 3]) `` stored as bytes (for example, on
156+ little-endian: ``b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00' ``)
157+ (:mod: `dbm.sqlite3 ` only)
158+
159+
118160The following example records some hostnames and a corresponding title, and
119161then prints out the contents of the database::
120162
@@ -136,8 +178,9 @@ then prints out the contents of the database::
136178 # Often-used methods of the dict interface work too.
137179 print(db.get('python.org', b'not present'))
138180
139- # Storing a non-string key or value will raise an exception (most
140- # likely a TypeError).
181+ # Storing a non-string key or value behavior depends on the backend:
182+ # - Traditional backends (ndbm, gnu, dumb) will raise a TypeError
183+ # - The sqlite3 backend accepts it and converts to bytes
141184 db['www.yahoo.com'] = 4
142185
143186 # db is automatically closed when leaving the with statement.
0 commit comments