5252The object returned by :func: `open ` supports most of the same functionality as
5353dictionaries; keys and their corresponding values can be stored, retrieved, and
5454deleted, and the :keyword: `in ` operator and the :meth: `keys ` method are
55- available. Keys and values must always be strings.
55+ available. Key and values are always stored as bytes. This means that when
56+ strings are used they are implicitly converted to the default encoding before
57+ being stored.
5658
5759The following example records some hostnames and a corresponding title, and
5860then prints out the contents of the database::
@@ -63,9 +65,15 @@ then prints out the contents of the database::
6365 db = dbm.open('cache', 'c')
6466
6567 # Record some values
68+ db[b'hello'] = b'there'
6669 db['www.python.org'] = 'Python Website'
6770 db['www.cnn.com'] = 'Cable News Network'
6871
72+ # Note that the keys are considered bytes now.
73+ assert db[b'www.python.org'] == b'Python Website'
74+ # Notice how the value is now in bytes.
75+ assert db['www.cnn.com'] == b'Cable News Network'
76+
6977 # Loop through contents. Other dictionary methods
7078 # such as .keys(), .values() also work.
7179 for k, v in db.iteritems():
@@ -98,17 +106,18 @@ The individual submodules are described in the following sections.
98106
99107This module is quite similar to the :mod: `dbm ` module, but uses the GNU library
100108``gdbm `` instead to provide some additional functionality. Please note that the
101- file formats created by `` gdbm `` and `` dbm ` ` are incompatible.
109+ file formats created by :mod: ` dbm.gnu ` and :mod: ` dbm.ndbm ` are incompatible.
102110
103111The :mod: `dbm.gnu ` module provides an interface to the GNU DBM library.
104- ``gdbm `` objects behave like mappings (dictionaries), except that keys and
105- values are always strings. Printing a :mod: `dbm.gnu ` object doesn't print the
112+ ``dbm.gnu.gdbm `` objects behave like mappings (dictionaries), except that keys and
113+ values are always converted to bytes before storing. Printing a ``gdbm ``
114+ object doesn't print the
106115keys and values, and the :meth: `items ` and :meth: `values ` methods are not
107116supported.
108117
109118.. exception :: error
110119
111- Raised on `` gdbm `` \ -specific errors, such as I/O errors. :exc: `KeyError ` is
120+ Raised on :mod: ` dbm.gnu ` -specific errors, such as I/O errors. :exc: `KeyError ` is
112121 raised for general mapping errors like specifying an incorrect key.
113122
114123
@@ -183,7 +192,7 @@ supported.
183192
184193 If you have carried out a lot of deletions and would like to shrink the space
185194 used by the ``gdbm `` file, this routine will reorganize the database. ``gdbm ``
186- will not shorten the length of a database file except by using this
195+ objects will not shorten the length of a database file except by using this
187196 reorganization; otherwise, deleted file space will be kept and reused as new
188197 (key, value) pairs are added.
189198
@@ -203,8 +212,8 @@ supported.
203212
204213The :mod: `dbm.ndbm ` module provides an interface to the Unix "(n)dbm" library.
205214Dbm objects behave like mappings (dictionaries), except that keys and values are
206- always strings . Printing a dbm object doesn't print the keys and values, and the
207- :meth: `items ` and :meth: `values ` methods are not supported.
215+ always stored as bytes . Printing a `` dbm `` object doesn't print the keys and
216+ values, and the :meth: `items ` and :meth: `values ` methods are not supported.
208217
209218This module can be used with the "classic" ndbm interface, the BSD DB
210219compatibility interface, or the GNU GDBM compatibility interface. On Unix, the
@@ -213,7 +222,7 @@ to simplify building this module.
213222
214223.. exception :: error
215224
216- Raised on dbm-specific errors, such as I/O errors. :exc: `KeyError ` is raised
225+ Raised on :mod: ` dbm.ndbm ` -specific errors, such as I/O errors. :exc: `KeyError ` is raised
217226 for general mapping errors like specifying an incorrect key.
218227
219228
@@ -224,7 +233,7 @@ to simplify building this module.
224233
225234.. function :: open(filename[, flag[, mode]])
226235
227- Open a dbm database and return a dbm object. The *filename * argument is the
236+ Open a dbm database and return a `` dbm `` object. The *filename * argument is the
228237 name of the database file (without the :file: `.dir ` or :file: `.pag ` extensions;
229238 note that the BSD DB implementation of the interface will append the extension
230239 :file: `.db ` and only create one file).
@@ -264,27 +273,27 @@ to simplify building this module.
264273.. note ::
265274
266275 The :mod: `dbm.dumb ` module is intended as a last resort fallback for the
267- :mod: `dbm ` module when no more robust module is available. The :mod: `dbm.dumb `
276+ :mod: `dbm ` module when a more robust module is not available. The :mod: `dbm.dumb `
268277 module is not written for speed and is not nearly as heavily used as the other
269278 database modules.
270279
271280The :mod: `dbm.dumb ` module provides a persistent dictionary-like interface which
272- is written entirely in Python. Unlike other modules such as :mod: `gdbm ` no
281+ is written entirely in Python. Unlike other modules such as :mod: `dbm.gnu ` no
273282external library is required. As with other persistent mappings, the keys and
274- values must always be strings .
283+ values are always stored as bytes .
275284
276285The module defines the following:
277286
278287
279288.. exception :: error
280289
281- Raised on dbm.dumb-specific errors, such as I/O errors. :exc: `KeyError ` is
290+ Raised on :mod: ` dbm.dumb ` -specific errors, such as I/O errors. :exc: `KeyError ` is
282291 raised for general mapping errors like specifying an incorrect key.
283292
284293
285294.. function :: open(filename[, flag[, mode]])
286295
287- Open a dumbdbm database and return a dumbdbm object. The *filename * argument is
296+ Open a `` dumbdbm `` database and return a dumbdbm object. The *filename * argument is
288297 the basename of the database file (without any specific extensions). When a
289298 dumbdbm database is created, files with :file: `.dat ` and :file: `.dir ` extensions
290299 are created.
0 commit comments