New submission from Serhiy Storchaka <storchaka+cpyt...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue33385>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to