Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d24fffe

Browse files
committed
Move shelve over to BytesIO as pickle.(Pickler | Unpickler) expect binary
files, not text files. test_shelve still fails thanks to bsddb not having been fixed.
1 parent 2f2fffb commit d24fffe

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

Lib/shelve.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"""
5858

5959
from pickle import Pickler, Unpickler
60-
from io import StringIO
60+
from io import BytesIO
6161

6262
import UserDict
6363
import warnings
@@ -97,7 +97,7 @@ def __getitem__(self, key):
9797
try:
9898
value = self.cache[key]
9999
except KeyError:
100-
f = StringIO(self.dict[key])
100+
f = BytesIO(self.dict[key])
101101
value = Unpickler(f).load()
102102
if self.writeback:
103103
self.cache[key] = value
@@ -106,7 +106,7 @@ def __getitem__(self, key):
106106
def __setitem__(self, key, value):
107107
if self.writeback:
108108
self.cache[key] = value
109-
f = StringIO()
109+
f = BytesIO()
110110
p = Pickler(f, self._protocol)
111111
p.dump(value)
112112
self.dict[key] = f.getvalue()
@@ -161,27 +161,27 @@ def __init__(self, dict, protocol=None, writeback=False):
161161

162162
def set_location(self, key):
163163
(key, value) = self.dict.set_location(key)
164-
f = StringIO(value)
164+
f = BytesIO(value)
165165
return (key, Unpickler(f).load())
166166

167167
def next(self):
168168
(key, value) = next(self.dict)
169-
f = StringIO(value)
169+
f = BytesIO(value)
170170
return (key, Unpickler(f).load())
171171

172172
def previous(self):
173173
(key, value) = self.dict.previous()
174-
f = StringIO(value)
174+
f = BytesIO(value)
175175
return (key, Unpickler(f).load())
176176

177177
def first(self):
178178
(key, value) = self.dict.first()
179-
f = StringIO(value)
179+
f = BytesIO(value)
180180
return (key, Unpickler(f).load())
181181

182182
def last(self):
183183
(key, value) = self.dict.last()
184-
f = StringIO(value)
184+
f = BytesIO(value)
185185
return (key, Unpickler(f).load())
186186

187187

Lib/test/test_anydbm.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def _delete_files():
2121
pass
2222

2323
class AnyDBMTestCase(unittest.TestCase):
24-
_dict = {'0': b'',
25-
'a': b'Python:',
26-
'b': b'Programming',
27-
'c': b'the',
28-
'd': b'way',
29-
'f': b'Guido',
30-
'g': b'intended',
24+
_dict = {str8('0'): b'',
25+
str8('a'): b'Python:',
26+
str8('b'): b'Programming',
27+
str8('c'): b'the',
28+
str8('d'): b'way',
29+
str8('f'): b'Guido',
30+
str8('g'): b'intended',
3131
}
3232

3333
def __init__(self, *args):
@@ -44,7 +44,7 @@ def test_anydbm_creation(self):
4444
def test_anydbm_modification(self):
4545
self.init_db()
4646
f = anydbm.open(_fname, 'c')
47-
self._dict['g'] = f['g'] = b"indented"
47+
self._dict[str8('g')] = f[str8('g')] = b"indented"
4848
self.read_helper(f)
4949
f.close()
5050

0 commit comments

Comments
 (0)