Skip to content

Commit

Permalink
bpo-43651: PEP 597: Fix test_email (GH-25158)
Browse files Browse the repository at this point in the history
  • Loading branch information
methane authored Apr 5, 2021
1 parent 2b5913b commit de522a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_email/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, *args, **kw):
ndiffAssertEqual = unittest.TestCase.assertEqual

def _msgobj(self, filename):
with openfile(filename) as fp:
with openfile(filename, encoding="utf-8") as fp:
return email.message_from_file(fp, policy=self.policy)

def _str_msg(self, string, message=None, policy=None):
Expand Down
52 changes: 26 additions & 26 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_make_boundary(self):
def test_message_rfc822_only(self):
# Issue 7970: message/rfc822 not in multipart parsed by
# HeaderParser caused an exception when flattened.
with openfile('msg_46.txt') as fp:
with openfile('msg_46.txt', encoding="utf-8") as fp:
msgdata = fp.read()
parser = HeaderParser()
msg = parser.parsestr(msgdata)
Expand All @@ -225,7 +225,7 @@ def test_message_rfc822_only(self):

def test_byte_message_rfc822_only(self):
# Make sure new bytes header parser also passes this.
with openfile('msg_46.txt') as fp:
with openfile('msg_46.txt', encoding="utf-8") as fp:
msgdata = fp.read().encode('ascii')
parser = email.parser.BytesHeaderParser()
msg = parser.parsebytes(msgdata)
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_get_payload_n_raises_on_non_multipart(self):
def test_decoded_generator(self):
eq = self.assertEqual
msg = self._msgobj('msg_07.txt')
with openfile('msg_17.txt') as fp:
with openfile('msg_17.txt', encoding="utf-8") as fp:
text = fp.read()
s = StringIO()
g = DecodedGenerator(s)
Expand All @@ -295,7 +295,7 @@ def test__contains__(self):

def test_as_string(self):
msg = self._msgobj('msg_01.txt')
with openfile('msg_01.txt') as fp:
with openfile('msg_01.txt', encoding="utf-8") as fp:
text = fp.read()
self.assertEqual(text, str(msg))
fullrepr = msg.as_string(unixfrom=True)
Expand Down Expand Up @@ -349,7 +349,7 @@ def test_nonascii_as_string_without_content_type_and_cte(self):

def test_as_bytes(self):
msg = self._msgobj('msg_01.txt')
with openfile('msg_01.txt') as fp:
with openfile('msg_01.txt', encoding="utf-8") as fp:
data = fp.read().encode('ascii')
self.assertEqual(data, bytes(msg))
fullrepr = msg.as_bytes(unixfrom=True)
Expand Down Expand Up @@ -2436,7 +2436,7 @@ def test_multiline_header(self):
# Test the MIMEMessage class
class TestMIMEMessage(TestEmailBase):
def setUp(self):
with openfile('msg_11.txt') as fp:
with openfile('msg_11.txt', encoding="utf-8") as fp:
self._text = fp.read()

def test_type_error(self):
Expand Down Expand Up @@ -2555,7 +2555,7 @@ def test_dsn(self):

def test_epilogue(self):
eq = self.ndiffAssertEqual
with openfile('msg_21.txt') as fp:
with openfile('msg_21.txt', encoding="utf-8") as fp:
text = fp.read()
msg = Message()
msg['From'] = 'aperson@dom.ain'
Expand Down Expand Up @@ -2610,7 +2610,7 @@ def test_no_nl_preamble(self):

def test_default_type(self):
eq = self.assertEqual
with openfile('msg_30.txt') as fp:
with openfile('msg_30.txt', encoding="utf-8") as fp:
msg = email.message_from_file(fp)
container1 = msg.get_payload(0)
eq(container1.get_default_type(), 'message/rfc822')
Expand All @@ -2627,7 +2627,7 @@ def test_default_type(self):

def test_default_type_with_explicit_container_type(self):
eq = self.assertEqual
with openfile('msg_28.txt') as fp:
with openfile('msg_28.txt', encoding="utf-8") as fp:
msg = email.message_from_file(fp)
container1 = msg.get_payload(0)
eq(container1.get_default_type(), 'message/rfc822')
Expand Down Expand Up @@ -2753,7 +2753,7 @@ class TestIdempotent(TestEmailBase):
linesep = '\n'

def _msgobj(self, filename):
with openfile(filename) as fp:
with openfile(filename, encoding="utf-8") as fp:
data = fp.read()
msg = email.message_from_string(data)
return msg, data
Expand Down Expand Up @@ -2909,7 +2909,7 @@ def test_parser(self):
# Test various other bits of the package's functionality
class TestMiscellaneous(TestEmailBase):
def test_message_from_string(self):
with openfile('msg_01.txt') as fp:
with openfile('msg_01.txt', encoding="utf-8") as fp:
text = fp.read()
msg = email.message_from_string(text)
s = StringIO()
Expand All @@ -2920,7 +2920,7 @@ def test_message_from_string(self):
self.assertEqual(text, s.getvalue())

def test_message_from_file(self):
with openfile('msg_01.txt') as fp:
with openfile('msg_01.txt', encoding="utf-8") as fp:
text = fp.read()
fp.seek(0)
msg = email.message_from_file(fp)
Expand All @@ -2932,7 +2932,7 @@ def test_message_from_file(self):
self.assertEqual(text, s.getvalue())

def test_message_from_string_with_class(self):
with openfile('msg_01.txt') as fp:
with openfile('msg_01.txt', encoding="utf-8") as fp:
text = fp.read()

# Create a subclass
Expand All @@ -2942,7 +2942,7 @@ class MyMessage(Message):
msg = email.message_from_string(text, MyMessage)
self.assertIsInstance(msg, MyMessage)
# Try something more complicated
with openfile('msg_02.txt') as fp:
with openfile('msg_02.txt', encoding="utf-8") as fp:
text = fp.read()
msg = email.message_from_string(text, MyMessage)
for subpart in msg.walk():
Expand All @@ -2953,11 +2953,11 @@ def test_message_from_file_with_class(self):
class MyMessage(Message):
pass

with openfile('msg_01.txt') as fp:
with openfile('msg_01.txt', encoding="utf-8") as fp:
msg = email.message_from_file(fp, MyMessage)
self.assertIsInstance(msg, MyMessage)
# Try something more complicated
with openfile('msg_02.txt') as fp:
with openfile('msg_02.txt', encoding="utf-8") as fp:
msg = email.message_from_file(fp, MyMessage)
for subpart in msg.walk():
self.assertIsInstance(subpart, MyMessage)
Expand Down Expand Up @@ -3386,7 +3386,7 @@ def test_make_msgid_default_domain(self):

def test_Generator_linend(self):
# Issue 14645.
with openfile('msg_26.txt', newline='\n') as f:
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as f:
msgtxt = f.read()
msgtxt_nl = msgtxt.replace('\r\n', '\n')
msg = email.message_from_string(msgtxt)
Expand All @@ -3397,7 +3397,7 @@ def test_Generator_linend(self):

def test_BytesGenerator_linend(self):
# Issue 14645.
with openfile('msg_26.txt', newline='\n') as f:
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as f:
msgtxt = f.read()
msgtxt_nl = msgtxt.replace('\r\n', '\n')
msg = email.message_from_string(msgtxt_nl)
Expand Down Expand Up @@ -3456,7 +3456,7 @@ def test_body_line_iterator(self):
it = iterators.body_line_iterator(msg)
lines = list(it)
eq(len(lines), 43)
with openfile('msg_19.txt') as fp:
with openfile('msg_19.txt', encoding="utf-8") as fp:
neq(EMPTYSTRING.join(lines), fp.read())

def test_typed_subpart_iterator(self):
Expand Down Expand Up @@ -3597,7 +3597,7 @@ class TestParsers(TestEmailBase):
def test_header_parser(self):
eq = self.assertEqual
# Parse only the headers of a complex multipart MIME document
with openfile('msg_02.txt') as fp:
with openfile('msg_02.txt', encoding="utf-8") as fp:
msg = HeaderParser().parse(fp)
eq(msg['from'], 'ppp-request@zzz.org')
eq(msg['to'], 'ppp@zzz.org')
Expand Down Expand Up @@ -3631,12 +3631,12 @@ def test_bytes_parser_on_exception_does_not_close_file(self):
self.assertFalse(fp.closed)

def test_parser_does_not_close_file(self):
with openfile('msg_02.txt', 'r') as fp:
with openfile('msg_02.txt', encoding="utf-8") as fp:
email.parser.Parser().parse(fp)
self.assertFalse(fp.closed)

def test_parser_on_exception_does_not_close_file(self):
with openfile('msg_15.txt', 'r') as fp:
with openfile('msg_15.txt', encoding="utf-8") as fp:
parser = email.parser.Parser
self.assertRaises(email.errors.StartBoundaryNotFoundDefect,
parser(policy=email.policy.strict).parse, fp)
Expand Down Expand Up @@ -3680,7 +3680,7 @@ def test_whitespace_continuation_last_header(self):

def test_crlf_separation(self):
eq = self.assertEqual
with openfile('msg_26.txt', newline='\n') as fp:
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as fp:
msg = Parser().parse(fp)
eq(len(msg.get_payload()), 2)
part1 = msg.get_payload(0)
Expand All @@ -3691,7 +3691,7 @@ def test_crlf_separation(self):

def test_crlf_flatten(self):
# Using newline='\n' preserves the crlfs in this input file.
with openfile('msg_26.txt', newline='\n') as fp:
with openfile('msg_26.txt', encoding="utf-8", newline='\n') as fp:
text = fp.read()
msg = email.message_from_string(text)
s = StringIO()
Expand All @@ -3704,7 +3704,7 @@ def test_crlf_flatten(self):
def test_multipart_digest_with_extra_mime_headers(self):
eq = self.assertEqual
neq = self.ndiffAssertEqual
with openfile('msg_28.txt') as fp:
with openfile('msg_28.txt', encoding="utf-8") as fp:
msg = email.message_from_file(fp)
# Structure is:
# multipart/digest
Expand Down Expand Up @@ -5447,7 +5447,7 @@ def test_should_not_hang_on_invalid_ew_messages(self):
class TestSigned(TestEmailBase):

def _msg_and_obj(self, filename):
with openfile(filename) as fp:
with openfile(filename, encoding="utf-8") as fp:
original = fp.read()
msg = email.message_from_string(original)
return original, msg
Expand Down

0 comments on commit de522a8

Please sign in to comment.