[New-bugs-announce] [issue33385] setdefault() with a single argument doesn't work for dbm.gdbm and dmb.ndbm objects
Serhiy Storchaka
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):
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.
----------
components: Library (Lib)
messages: 315898
nosy: serhiy.storchaka
priority: normal
severity: normal
status: open
title: setdefault() with a single argument doesn't work for dbm.gdbm and dmb.ndbm objects
type: enhancement
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33385>
_______________________________________
More information about the New-bugs-announce
mailing list