Skip to content

Commit ed1ffca

Browse files
committed
Replace encode strs to bytes directly
1 parent 818a34b commit ed1ffca

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

zarr/storage.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,12 +1577,6 @@ def migrate_1to2(store):
15771577
del store['attrs']
15781578

15791579

1580-
def _dbm_encode_key(key):
1581-
if hasattr(key, 'encode'):
1582-
key = key.encode('ascii')
1583-
return key
1584-
1585-
15861580
# noinspection PyShadowingBuiltins
15871581
class DBMStore(MutableMapping):
15881582
"""Storage class using a DBM-style database.
@@ -1728,17 +1722,20 @@ def __exit__(self, *args):
17281722
self.close()
17291723

17301724
def __getitem__(self, key):
1731-
key = _dbm_encode_key(key)
1725+
if isinstance(key, str):
1726+
key = key.encode("ascii")
17321727
return self.db[key]
17331728

17341729
def __setitem__(self, key, value):
1735-
key = _dbm_encode_key(key)
1730+
if isinstance(key, str):
1731+
key = key.encode("ascii")
17361732
value = ensure_bytes(value)
17371733
with self.write_mutex:
17381734
self.db[key] = value
17391735

17401736
def __delitem__(self, key):
1741-
key = _dbm_encode_key(key)
1737+
if isinstance(key, str):
1738+
key = key.encode("ascii")
17421739
with self.write_mutex:
17431740
del self.db[key]
17441741

@@ -1761,7 +1758,8 @@ def __len__(self):
17611758
return sum(1 for _ in self.keys())
17621759

17631760
def __contains__(self, key):
1764-
key = _dbm_encode_key(key)
1761+
if isinstance(key, str):
1762+
key = key.encode("ascii")
17651763
return key in self.db
17661764

17671765

@@ -1885,7 +1883,8 @@ def __exit__(self, *args):
18851883
self.close()
18861884

18871885
def __getitem__(self, key):
1888-
key = _dbm_encode_key(key)
1886+
if isinstance(key, str):
1887+
key = key.encode("ascii")
18891888
# use the buffers option, should avoid a memory copy
18901889
with self.db.begin(buffers=self.buffers) as txn:
18911890
value = txn.get(key)
@@ -1894,18 +1893,21 @@ def __getitem__(self, key):
18941893
return value
18951894

18961895
def __setitem__(self, key, value):
1897-
key = _dbm_encode_key(key)
1896+
if isinstance(key, str):
1897+
key = key.encode("ascii")
18981898
with self.db.begin(write=True, buffers=self.buffers) as txn:
18991899
txn.put(key, value)
19001900

19011901
def __delitem__(self, key):
1902-
key = _dbm_encode_key(key)
1902+
if isinstance(key, str):
1903+
key = key.encode("ascii")
19031904
with self.db.begin(write=True) as txn:
19041905
if not txn.delete(key):
19051906
raise KeyError(key)
19061907

19071908
def __contains__(self, key):
1908-
key = _dbm_encode_key(key)
1909+
if isinstance(key, str):
1910+
key = key.encode("ascii")
19091911
with self.db.begin(buffers=self.buffers) as txn:
19101912
with txn.cursor() as cursor:
19111913
return cursor.set_key(key)

0 commit comments

Comments
 (0)