[Python-3000] Immutable bytes type and dbm modules

"Martin v. Löwis" martin at v.loewis.de
Tue Aug 7 05:41:58 CEST 2007


> Or perhaps a special value of the encoding argument passed to
> *dbm.open() (maybe None, maybe the default, maybe "raw" or "bytes"?)
> to specify that the key values are to be bytes?

This is essentially the state of the bsddb module in the struni branch
right now. The default is bytes keys and values; if you want string
keys, you write

   db = bsddb.open(...)
   db = bsddb.StringKeys(db)

which arranges for transparent UTF-8 encoding; it would be possible to
extend this to

   db = bsddb.open(...)
   db = bsddb.StringKeys(db, encoding="latin-1")

However, this has the view that there is a single "proper" key
representation, which is bytes, and then reinterpretations.

Now if you say that the dbm files are dicts conceptually, and
bytes are not allowed as dict keys, then any API that allows
for bytes as dbm keys (whether by default or as an option) is
conceptually inconsistent - as you now do have dict-like objects
which use bytes keys. This causes confusion if you pass one of
them to, say, .update of a "real" dict, which then fails. IOW,
I couldn't do

   d = {}
   d.update(db)

if db is in the "keys are bytes" mode.

Regards,
Martin


More information about the Python-3000 mailing list