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

Skip to content

Commit 2b5d6eb

Browse files
committed
dbm.dumb was opening files without specifying the encoding. Caused problem on
at least OS X where the default is macroman. Closes issue #4382.
1 parent 02c3b5c commit 2b5d6eb

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

Lib/dbm/dumb.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def __init__(self, filebasename, mode):
6666

6767
# Mod by Jack: create data file if needed
6868
try:
69-
f = _io.open(self._datfile, 'r')
69+
f = _io.open(self._datfile, 'r', encoding="Latin-1")
7070
except IOError:
71-
f = _io.open(self._datfile, 'w')
71+
f = _io.open(self._datfile, 'w', encoding="Latin-1")
7272
self._chmod(self._datfile)
7373
f.close()
7474
self._update()
@@ -77,7 +77,7 @@ def __init__(self, filebasename, mode):
7777
def _update(self):
7878
self._index = {}
7979
try:
80-
f = _io.open(self._dirfile, 'r')
80+
f = _io.open(self._dirfile, 'r', encoding="Latin-1")
8181
except IOError:
8282
pass
8383
else:
@@ -108,7 +108,7 @@ def _commit(self):
108108
except self._os.error:
109109
pass
110110

111-
f = self._io.open(self._dirfile, 'w')
111+
f = self._io.open(self._dirfile, 'w', encoding="Latin-1")
112112
self._chmod(self._dirfile)
113113
for key, pos_and_siz_pair in self._index.items():
114114
# Use Latin-1 since it has no qualms with any value in any
@@ -159,18 +159,20 @@ def _setval(self, pos, val):
159159
# the in-memory index dict, and append one to the directory file.
160160
def _addkey(self, key, pos_and_siz_pair):
161161
self._index[key] = pos_and_siz_pair
162-
f = _io.open(self._dirfile, 'a')
162+
f = _io.open(self._dirfile, 'a', encoding="Latin-1")
163163
self._chmod(self._dirfile)
164-
f.write("%r, %r\n" % (key, pos_and_siz_pair))
164+
f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
165165
f.close()
166166

167167
def __setitem__(self, key, val):
168168
if isinstance(key, str):
169169
key = key.encode('utf-8')
170170
elif not isinstance(key, (bytes, bytearray)):
171171
raise TypeError("keys must be bytes or strings")
172-
if not isinstance(val, (bytes, bytearray)):
173-
raise TypeError("values must be bytes")
172+
if isinstance(val, str):
173+
val = val.encode('utf-8')
174+
elif not isinstance(val, (bytes, bytearray)):
175+
raise TypeError("values must be bytes or strings")
174176
if key not in self._index:
175177
self._addkey(key, self._addval(val))
176178
else:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #4382: dbm.dumb did not specify the expected file encoding for opened
26+
files.
27+
2528
- Issue #4383: When IDLE cannot make the connection to its subprocess, it would
2629
fail to properly display the error message.
2730

0 commit comments

Comments
 (0)