classification
Title: setdefault() with a single argument doesn't work for dbm.gnu and dmb.dumb objects
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ned.deily, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-04-29 14:00 by serhiy.storchaka, last changed 2018-04-30 09:11 by serhiy.storchaka.

Messages (4)
msg315898 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-29 14:00
setdefault() is not implemented directly in dbm.gdbm and dmb.ndbm database classes. It is inherited from MutableMapping:

    def setdefault(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            self[key] = default
        return default

But since assigning is supported only for bytes and str, setdefault(key) fails if the key was not set before. It works only if the key was set or with the second argument. d.setdefault(key) is equivalent to d[key] except that it raises a weird TypeError instead of KeyError.

There are two ways of solving this problem:

1. Reimplement setdefault() for dbm.gdbm and dmb.ndbm database classes with default=b'' by default.

2. Make the second argument mandatory.

In both cases this violates the MutableMapping interface.
msg315925 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-04-30 06:12
Option 2 seems perfectly reasonable here as a way of preventing bugs (working, correct code wouldn't be using the existing default of None).

Option 1 is problematic because of the variance from Mutable Mapping, because breaking symmetry with get(), and because the change in behavior is implicit.
msg315931 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-30 07:21
Thank you Raymond. I hesitated to make a choose.

Ned, is it good to add a deprecation warning in setdefault() methods of the dbm classes when they called with a single argument in 3.7? Their current behavior (no-op or error) is not useful in any case.
msg315943 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-30 09:09
Actually dbm.ndbm already implements option 1. Only dbm.gnu and dbm.dumb have non-working with a single argument setdefault().
History
Date User Action Args
2018-04-30 09:11:07serhiy.storchakasettitle: setdefault() with a single argument doesn't work for dbm.gdbm and dmb.ndbm objects -> setdefault() with a single argument doesn't work for dbm.gnu and dmb.dumb objects
2018-04-30 09:09:52serhiy.storchakasetmessages: + msg315943
2018-04-30 07:21:47serhiy.storchakasetnosy: + ned.deily
messages: + msg315931
2018-04-30 06:12:24rhettingersetnosy: + rhettinger
messages: + msg315925
2018-04-29 14:00:31serhiy.storchakacreate