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

Skip to content

Commit d4eda82

Browse files
committed
SF patch# 1757839 by Alexandre Vassalotti -- make test_mailbox and
test_old_mailbox pass.
1 parent bf4806b commit d4eda82

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

Lib/email/generator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
fcre = re.compile(r'^From ', re.MULTILINE)
2222

2323
def _is8bitstring(s):
24-
if isinstance(s, str):
24+
if isinstance(s, bytes):
2525
try:
2626
str(s, 'us-ascii')
27+
return True
2728
except UnicodeError:
29+
pass
30+
elif isinstance(s, str):
31+
try:
32+
s.decode('us-ascii')
2833
return True
34+
except UnicodeError:
35+
pass
2936
return False
3037

31-
3238

3339
class Generator:
3440
"""Generates output from a Message object tree.

Lib/email/header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def append(self, s, charset=None, errors='strict'):
253253
# We need to test that the string can be converted to unicode and
254254
# back to a byte string, given the input and output codecs of the
255255
# charset.
256-
if isinstance(s, str):
256+
if isinstance(s, bytes):
257257
# Possibly raise UnicodeError if the byte string can't be
258258
# converted to a unicode with the input codec of the charset.
259259
incodec = charset.input_codec or 'us-ascii'
@@ -263,7 +263,7 @@ def append(self, s, charset=None, errors='strict'):
263263
# than the iput coded. Still, use the original byte string.
264264
outcodec = charset.output_codec or 'us-ascii'
265265
ustr.encode(outcodec, errors)
266-
elif isinstance(s, str):
266+
elif isinstance(s, bytes):
267267
# Now we have to be sure the unicode string can be converted
268268
# to a byte string with a reasonable output codec. We want to
269269
# use the byte string in the chunk.

Lib/mailbox.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,15 @@ def __init__(self, path, factory=None, create=True):
498498
"""Initialize a single-file mailbox."""
499499
Mailbox.__init__(self, path, factory, create)
500500
try:
501-
f = open(self._path, 'rb+')
501+
f = open(self._path, 'r+')
502502
except IOError as e:
503503
if e.errno == errno.ENOENT:
504504
if create:
505-
f = open(self._path, 'wb+')
505+
f = open(self._path, 'w+')
506506
else:
507507
raise NoSuchMailboxError(self._path)
508508
elif e.errno == errno.EACCES:
509-
f = open(self._path, 'rb')
509+
f = open(self._path, 'r')
510510
else:
511511
raise
512512
self._file = f
@@ -1761,11 +1761,11 @@ def __init__(self, f, pos=None):
17611761

17621762
def read(self, size=None):
17631763
"""Read bytes."""
1764-
return self._read(size, self._file.read)
1764+
return str(self._read(size, self._file.read))
17651765

17661766
def readline(self, size=None):
17671767
"""Read a line."""
1768-
return self._read(size, self._file.readline)
1768+
return str(self._read(size, self._file.readline))
17691769

17701770
def readlines(self, sizehint=None):
17711771
"""Read multiple lines."""
@@ -1900,7 +1900,7 @@ def _create_carefully(path):
19001900
"""Create a file if it doesn't exist and open for reading and writing."""
19011901
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
19021902
try:
1903-
return open(path, 'rb+')
1903+
return open(path, 'r+')
19041904
finally:
19051905
os.close(fd)
19061906

Lib/test/test_mailbox.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def setUp(self):
5858
self._box = self._factory(self._path)
5959

6060
def tearDown(self):
61-
self._box.close()
6261
self._delete_recursively(self._path)
6362

6463
def test_add(self):
@@ -695,7 +694,6 @@ def test_directory_in_folder (self):
695694
class _TestMboxMMDF(TestMailbox):
696695

697696
def tearDown(self):
698-
self._box.close()
699697
self._delete_recursively(self._path)
700698
for lock_remnant in glob.glob(self._path + '.*'):
701699
test_support.unlink(lock_remnant)
@@ -736,7 +734,7 @@ def test_add_and_close(self):
736734
self._box._file.seek(0)
737735
contents = self._box._file.read()
738736
self._box.close()
739-
self.assert_(contents == open(self._path, 'rb').read())
737+
self.assert_(contents == open(self._path, 'r').read())
740738
self._box = self._factory(self._path)
741739

742740
def test_lock_conflict(self):
@@ -918,7 +916,6 @@ class TestBabyl(TestMailbox):
918916
_factory = lambda self, path, factory=None: mailbox.Babyl(path, factory)
919917

920918
def tearDown(self):
921-
self._box.close()
922919
self._delete_recursively(self._path)
923920
for lock_remnant in glob.glob(self._path + '.*'):
924921
test_support.unlink(lock_remnant)

0 commit comments

Comments
 (0)