Skip to content

dbm.dumb: bytearray keys raise TypeError instead of being converted to bytes #158217

Description

@Tony123-tech

In Lib/dbm/dumb.py, _Database.setitem and related methods accept
bytearray keys (the isinstance check includes bytearray), but the
in-memory index is a dict, and bytearray is not hashable:

import dbm.dumb, tempfile, os
d = tempfile.mkdtemp()
db = dbm.dumb.open(os.path.join(d, 'testdb'), 'c')
db[bytearray(b'key')] = b'value'
# TypeError: unhashable type: 'bytearray'

The same problem affects getitem, delitem, and contains.

The isinstance check should either reject bytearray, or convert it to
bytes before use.

Discovered while reviewing the module with an AI assistant.

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions