diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py index a080f4e865508b..77b46c5db9b9e0 100644 --- a/Lib/dbm/dumb.py +++ b/Lib/dbm/dumb.py @@ -144,6 +144,8 @@ def _verify_open(self): def __getitem__(self, key): if isinstance(key, str): key = key.encode('utf-8') + elif isinstance(key, bytearray): + key = bytes(key) self._verify_open() pos, siz = self._index[key] # may raise KeyError with _io.open(self._datfile, 'rb') as f: @@ -189,7 +191,9 @@ def __setitem__(self, key, val): raise error('The database is opened for reading only') if isinstance(key, str): key = key.encode('utf-8') - elif not isinstance(key, (bytes, bytearray)): + elif isinstance(key, bytearray): + key = bytes(key) + elif not isinstance(key, bytes): raise TypeError("keys must be bytes or strings") if isinstance(val, str): val = val.encode('utf-8') @@ -226,6 +230,8 @@ def __delitem__(self, key): raise error('The database is opened for reading only') if isinstance(key, str): key = key.encode('utf-8') + elif isinstance(key, bytearray): + key = bytes(key) self._verify_open() self._modified = True # The blocks used by the associated value are lost. @@ -249,6 +255,8 @@ def items(self): def __contains__(self, key): if isinstance(key, str): key = key.encode('utf-8') + elif isinstance(key, bytearray): + key = bytes(key) try: return key in self._index except TypeError: diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py index d977a81876df65..c0a16d5e203419 100644 --- a/Lib/test/test_dbm_dumb.py +++ b/Lib/test/test_dbm_dumb.py @@ -41,6 +41,15 @@ def test_dumbdbm_creation(self): f[key] = self._dict[key] self.read_helper(f) + def test_dumbdbm_bytearray_keys(self): + # gh-158217: bytearray keys must not raise TypeError: + with contextlib.closing(dumbdbm.open(_fname, 'c')) as f: + f[bytearray(b'key')] = b'value' + self.assertEqual(f[bytearray(b'key')], b'value') + self.assertIn(bytearray(b'key'), f) + del f[bytearray(b'key')] + self.assertNotIn(bytearray(b'key'), f) + @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') @os_helper.skip_unless_working_chmod def test_dumbdbm_creation_mode(self): diff --git a/Misc/NEWS.d/next/Library/2026-09-26-12-53-36.gh-issue-158217.AbCdEf.rst b/Misc/NEWS.d/next/Library/2026-09-26-12-53-36.gh-issue-158217.AbCdEf.rst new file mode 100644 index 00000000000000..5845f23edd24c3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-26-12-53-36.gh-issue-158217.AbCdEf.rst @@ -0,0 +1,2 @@ +Fix :mod:`dbm.dumb` to accept ``bytearray`` keys, consistent with the +other :mod:`dbm` backends. Patch by Tony Leung.