@@ -1577,12 +1577,6 @@ def migrate_1to2(store):
1577
1577
del store ['attrs' ]
1578
1578
1579
1579
1580
- def _dbm_encode_key (key ):
1581
- if hasattr (key , 'encode' ):
1582
- key = key .encode ('ascii' )
1583
- return key
1584
-
1585
-
1586
1580
# noinspection PyShadowingBuiltins
1587
1581
class DBMStore (MutableMapping ):
1588
1582
"""Storage class using a DBM-style database.
@@ -1728,17 +1722,20 @@ def __exit__(self, *args):
1728
1722
self .close ()
1729
1723
1730
1724
def __getitem__ (self , key ):
1731
- key = _dbm_encode_key (key )
1725
+ if isinstance (key , str ):
1726
+ key = key .encode ("ascii" )
1732
1727
return self .db [key ]
1733
1728
1734
1729
def __setitem__ (self , key , value ):
1735
- key = _dbm_encode_key (key )
1730
+ if isinstance (key , str ):
1731
+ key = key .encode ("ascii" )
1736
1732
value = ensure_bytes (value )
1737
1733
with self .write_mutex :
1738
1734
self .db [key ] = value
1739
1735
1740
1736
def __delitem__ (self , key ):
1741
- key = _dbm_encode_key (key )
1737
+ if isinstance (key , str ):
1738
+ key = key .encode ("ascii" )
1742
1739
with self .write_mutex :
1743
1740
del self .db [key ]
1744
1741
@@ -1761,7 +1758,8 @@ def __len__(self):
1761
1758
return sum (1 for _ in self .keys ())
1762
1759
1763
1760
def __contains__ (self , key ):
1764
- key = _dbm_encode_key (key )
1761
+ if isinstance (key , str ):
1762
+ key = key .encode ("ascii" )
1765
1763
return key in self .db
1766
1764
1767
1765
@@ -1885,7 +1883,8 @@ def __exit__(self, *args):
1885
1883
self .close ()
1886
1884
1887
1885
def __getitem__ (self , key ):
1888
- key = _dbm_encode_key (key )
1886
+ if isinstance (key , str ):
1887
+ key = key .encode ("ascii" )
1889
1888
# use the buffers option, should avoid a memory copy
1890
1889
with self .db .begin (buffers = self .buffers ) as txn :
1891
1890
value = txn .get (key )
@@ -1894,18 +1893,21 @@ def __getitem__(self, key):
1894
1893
return value
1895
1894
1896
1895
def __setitem__ (self , key , value ):
1897
- key = _dbm_encode_key (key )
1896
+ if isinstance (key , str ):
1897
+ key = key .encode ("ascii" )
1898
1898
with self .db .begin (write = True , buffers = self .buffers ) as txn :
1899
1899
txn .put (key , value )
1900
1900
1901
1901
def __delitem__ (self , key ):
1902
- key = _dbm_encode_key (key )
1902
+ if isinstance (key , str ):
1903
+ key = key .encode ("ascii" )
1903
1904
with self .db .begin (write = True ) as txn :
1904
1905
if not txn .delete (key ):
1905
1906
raise KeyError (key )
1906
1907
1907
1908
def __contains__ (self , key ):
1908
- key = _dbm_encode_key (key )
1909
+ if isinstance (key , str ):
1910
+ key = key .encode ("ascii" )
1909
1911
with self .db .begin (buffers = self .buffers ) as txn :
1910
1912
with txn .cursor () as cursor :
1911
1913
return cursor .set_key (key )
0 commit comments