mirror of
https://github.com/python/cpython.git
synced 2025-09-18 14:40:43 +00:00
#21476: Unwrap fp in BytesParser so the file isn't unexpectedly closed.
This makes the behavior match that of Parser. Patch by Vajrasky Kok.
This commit is contained in:
parent
19454563d8
commit
c6772c4d59
3 changed files with 31 additions and 1 deletions
|
@ -106,8 +106,10 @@ class BytesParser:
|
||||||
meaning it parses the entire contents of the file.
|
meaning it parses the entire contents of the file.
|
||||||
"""
|
"""
|
||||||
fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
|
fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
|
||||||
with fp:
|
try:
|
||||||
return self.parser.parse(fp, headersonly)
|
return self.parser.parse(fp, headersonly)
|
||||||
|
finally:
|
||||||
|
fp.detach()
|
||||||
|
|
||||||
|
|
||||||
def parsebytes(self, text, headersonly=False):
|
def parsebytes(self, text, headersonly=False):
|
||||||
|
|
|
@ -3390,6 +3390,31 @@ class TestParsers(TestEmailBase):
|
||||||
self.assertIsInstance(msg.get_payload(), str)
|
self.assertIsInstance(msg.get_payload(), str)
|
||||||
self.assertIsInstance(msg.get_payload(decode=True), bytes)
|
self.assertIsInstance(msg.get_payload(decode=True), bytes)
|
||||||
|
|
||||||
|
def test_bytes_parser_does_not_close_file(self):
|
||||||
|
with openfile('msg_02.txt', 'rb') as fp:
|
||||||
|
email.parser.BytesParser().parse(fp)
|
||||||
|
self.assertFalse(fp.closed)
|
||||||
|
|
||||||
|
def test_bytes_parser_on_exception_does_not_close_file(self):
|
||||||
|
with openfile('msg_15.txt', 'rb') as fp:
|
||||||
|
bytesParser = email.parser.BytesParser
|
||||||
|
self.assertRaises(email.errors.StartBoundaryNotFoundDefect,
|
||||||
|
bytesParser(policy=email.policy.strict).parse,
|
||||||
|
fp)
|
||||||
|
self.assertFalse(fp.closed)
|
||||||
|
|
||||||
|
def test_parser_does_not_close_file(self):
|
||||||
|
with openfile('msg_02.txt', 'r') 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:
|
||||||
|
parser = email.parser.Parser
|
||||||
|
self.assertRaises(email.errors.StartBoundaryNotFoundDefect,
|
||||||
|
parser(policy=email.policy.strict).parse, fp)
|
||||||
|
self.assertFalse(fp.closed)
|
||||||
|
|
||||||
def test_whitespace_continuation(self):
|
def test_whitespace_continuation(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
# This message contains a line after the Subject: header that has only
|
# This message contains a line after the Subject: header that has only
|
||||||
|
|
|
@ -27,6 +27,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #21476: Make sure the email.parser.BytesParser TextIOWrapper is
|
||||||
|
discarded after parsing, so the input file isn't unexpectedly closed.
|
||||||
|
|
||||||
- Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
|
- Issue #21729: Used the "with" statement in the dbm.dumb module to ensure
|
||||||
files closing. Patch by Claudiu Popa.
|
files closing. Patch by Claudiu Popa.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue