[New-bugs-announce] [issue33385] setdefault() with a single argument doesn't work for dbm.gdbm and dmb.ndbm objects
report at bugs.python.org
Sun Apr 29 10:00:31 EDT 2018
New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:
setdefault() is not implemented directly in dbm.gdbm and dmb.ndbm database classes. It is inherited from MutableMapping:
def setdefault(self, key, default=None):
self[key] = 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.
components: Library (Lib)
title: setdefault() with a single argument doesn't work for dbm.gdbm and dmb.ndbm objects
versions: Python 3.8
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce