Skip to content

Commit 511b37a

Browse files
[3.13] Update the dbm documentation (GH-137919) (GH-137963)
Unify documentation for all backends, enumerate all not implemented mapping methods, document particularities of implemented mapping methods. (cherry picked from commit 8700404)
1 parent 8aabea7 commit 511b37a

File tree

2 files changed

+67
-41
lines changed

2 files changed

+67
-41
lines changed

Doc/library/dbm.rst

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ the Oracle Berkeley DB.
8484
.. versionchanged:: 3.11
8585
*file* accepts a :term:`path-like object`.
8686

87-
The object returned by :func:`~dbm.open` supports the same basic functionality as a
88-
:class:`dict`; keys and their corresponding values can be stored, retrieved, and
89-
deleted, and the :keyword:`in` operator and the :meth:`!keys` method are
90-
available, as well as :meth:`!get` and :meth:`!setdefault` methods.
87+
The object returned by :func:`~dbm.open` supports the basic
88+
functionality of mutable :term:`mappings <mapping>`;
89+
keys and their corresponding values can be stored, retrieved, and
90+
deleted, and iteration, the :keyword:`in` operator and methods :meth:`!keys`,
91+
:meth:`!get`, :meth:`!setdefault` and :meth:`!clear` are available.
92+
The :meth:`!keys` method returns a list instead of a view object.
93+
The :meth:`!setdefault` method requires two arguments.
9194

9295
Key and values are always stored as :class:`bytes`. This means that when
9396
strings are used they are implicitly converted to the default encoding before
@@ -108,6 +111,10 @@ will automatically close them when done.
108111
Deleting a key from a read-only database raises a database module specific exception
109112
instead of :exc:`KeyError`.
110113

114+
.. versionchanged:: 3.13
115+
:meth:`!clear` methods are now available for all :mod:`dbm` backends.
116+
117+
111118
The following example records some hostnames and a corresponding title, and
112119
then prints out the contents of the database::
113120

@@ -167,9 +174,6 @@ or any other SQLite browser, including the SQLite CLI.
167174
.. function:: open(filename, /, flag="r", mode=0o666)
168175

169176
Open an SQLite database.
170-
The returned object behaves like a :term:`mapping`,
171-
implements a :meth:`!close` method,
172-
and supports a "closing" context manager via the :keyword:`with` keyword.
173177

174178
:param filename:
175179
The path to the database to be opened.
@@ -186,6 +190,17 @@ or any other SQLite browser, including the SQLite CLI.
186190
The Unix file access mode of the file (default: octal ``0o666``),
187191
used only when the database has to be created.
188192

193+
The returned database object behaves similar to a mutable :term:`mapping`,
194+
but the :meth:`!keys` method returns a list, and
195+
the :meth:`!setdefault` method requires two arguments.
196+
It also supports a "closing" context manager via the :keyword:`with` keyword.
197+
198+
The following method is also provided:
199+
200+
.. method:: sqlite3.close()
201+
202+
Close the SQLite database.
203+
189204

190205
:mod:`dbm.gnu` --- GNU database manager
191206
---------------------------------------
@@ -215,6 +230,11 @@ functionality like crash tolerance.
215230
raised for general mapping errors like specifying an incorrect key.
216231

217232

233+
.. data:: open_flags
234+
235+
A string of characters the *flag* parameter of :meth:`~dbm.gnu.open` supports.
236+
237+
218238
.. function:: open(filename, flag="r", mode=0o666, /)
219239

220240
Open a GDBM database and return a :class:`!gdbm` object.
@@ -250,14 +270,25 @@ functionality like crash tolerance.
250270
.. versionchanged:: 3.11
251271
*filename* accepts a :term:`path-like object`.
252272

253-
.. data:: open_flags
273+
:class:`!gdbm` objects behave similar to mutable :term:`mappings <mapping>`,
274+
but methods :meth:`!items`, :meth:`!values`, :meth:`!pop`, :meth:`!popitem`,
275+
and :meth:`!update` are not supported,
276+
the :meth:`!keys` method returns a list, and
277+
the :meth:`!setdefault` method requires two arguments.
278+
It also supports a "closing" context manager via the :keyword:`with` keyword.
279+
280+
.. versionchanged:: 3.2
281+
Added the :meth:`!get` and :meth:`!setdefault` methods.
254282

255-
A string of characters the *flag* parameter of :meth:`~dbm.gnu.open` supports.
283+
.. versionchanged:: 3.13
284+
Added the :meth:`!clear` method.
256285

257-
:class:`!gdbm` objects behave similar to :term:`mappings <mapping>`,
258-
but :meth:`!items` and :meth:`!values` methods are not supported.
259286
The following methods are also provided:
260287

288+
.. method:: gdbm.close()
289+
290+
Close the GDBM database.
291+
261292
.. method:: gdbm.firstkey()
262293

263294
It's possible to loop over every key in the database using this method and the
@@ -289,16 +320,6 @@ functionality like crash tolerance.
289320
When the database has been opened in fast mode, this method forces any
290321
unwritten data to be written to the disk.
291322

292-
.. method:: gdbm.close()
293-
294-
Close the GDBM database.
295-
296-
.. method:: gdbm.clear()
297-
298-
Remove all items from the GDBM database.
299-
300-
.. versionadded:: 3.13
301-
302323

303324
:mod:`dbm.ndbm` --- New Database Manager
304325
----------------------------------------
@@ -359,22 +380,27 @@ This module can be used with the "classic" NDBM interface or the
359380
:param int mode:
360381
|mode_param_doc|
361382

362-
:class:`!ndbm` objects behave similar to :term:`mappings <mapping>`,
363-
but :meth:`!items` and :meth:`!values` methods are not supported.
364-
The following methods are also provided:
365-
366383
.. versionchanged:: 3.11
367384
Accepts :term:`path-like object` for filename.
368385

369-
.. method:: ndbm.close()
386+
:class:`!ndbm` objects behave similar to mutable :term:`mappings <mapping>`,
387+
but methods :meth:`!items`, :meth:`!values`, :meth:`!pop`, :meth:`!popitem`,
388+
and :meth:`!update` are not supported,
389+
the :meth:`!keys` method returns a list, and
390+
the :meth:`!setdefault` method requires two arguments.
391+
It also supports a "closing" context manager via the :keyword:`with` keyword.
370392

371-
Close the NDBM database.
393+
.. versionchanged:: 3.2
394+
Added the :meth:`!get` and :meth:`!setdefault` methods.
372395

373-
.. method:: ndbm.clear()
396+
.. versionchanged:: 3.13
397+
Added the :meth:`!clear` method.
374398

375-
Remove all items from the NDBM database.
399+
The following method is also provided:
376400

377-
.. versionadded:: 3.13
401+
.. method:: ndbm.close()
402+
403+
Close the NDBM database.
378404

379405

380406
:mod:`dbm.dumb` --- Portable DBM implementation
@@ -412,9 +438,6 @@ The :mod:`!dbm.dumb` module defines the following:
412438
.. function:: open(filename, flag="c", mode=0o666)
413439

414440
Open a :mod:`!dbm.dumb` database.
415-
The returned database object behaves similar to a :term:`mapping`,
416-
in addition to providing :meth:`~dumbdbm.sync` and :meth:`~dumbdbm.close`
417-
methods.
418441

419442
:param filename:
420443
The basename of the database file (without extensions).
@@ -448,15 +471,18 @@ The :mod:`!dbm.dumb` module defines the following:
448471
.. versionchanged:: 3.11
449472
*filename* accepts a :term:`path-like object`.
450473

451-
In addition to the methods provided by the
452-
:class:`collections.abc.MutableMapping` class,
453-
the following methods are provided:
474+
The returned database object behaves similar to a mutable :term:`mapping`,
475+
but the :meth:`!keys` and :meth:`!items` methods return lists, and
476+
the :meth:`!setdefault` method requires two arguments.
477+
It also supports a "closing" context manager via the :keyword:`with` keyword.
454478

455-
.. method:: dumbdbm.sync()
456-
457-
Synchronize the on-disk directory and data files. This method is called
458-
by the :meth:`shelve.Shelf.sync` method.
479+
The following methods are also provided:
459480

460481
.. method:: dumbdbm.close()
461482

462483
Close the database.
484+
485+
.. method:: dumbdbm.sync()
486+
487+
Synchronize the on-disk directory and data files. This method is called
488+
by the :meth:`shelve.Shelf.sync` method.

Doc/whatsnew/3.13.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ dbm
843843
(Contributed by Raymond Hettinger and Erlend E. Aasland in :gh:`100414`.)
844844

845845
* Allow removing all items from the database through
846-
the new :meth:`.gdbm.clear` and :meth:`.ndbm.clear` methods.
846+
the new :meth:`!clear` methods of the GDBM and NDBM database objects.
847847
(Contributed by Donghee Na in :gh:`107122`.)
848848

849849

0 commit comments

Comments
 (0)