summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-06-26 17:33:05 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-06-26 17:33:05 (GMT)
commit0c43ed87168a571adf1c2e0f7538daf5ad7a9c77 (patch)
tree2951127ad509f81b8135e12170094a17c314f0b5 /Lib
parent2f60820f4c1bdfce75f30ddc53d97587a3256dcf (diff)
parentc6772c4d592be2274a66ccd26b5956738fc6e4f8 (diff)
downloadcpython-0c43ed87168a571adf1c2e0f7538daf5ad7a9c77.zip
cpython-0c43ed87168a571adf1c2e0f7538daf5ad7a9c77.tar.gz
cpython-0c43ed87168a571adf1c2e0f7538daf5ad7a9c77.tar.bz2
Merge #21476: Unwrap fp in BytesParser so the file isn't unexpectedly closed.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/email/parser.py4
-rw-r--r--Lib/test/test_email/test_email.py25
2 files changed, 28 insertions, 1 deletions
diff --git a/Lib/email/parser.py b/Lib/email/parser.py
index 9f5f95d..8c9bc9e 100644
--- a/Lib/email/parser.py
+++ b/Lib/email/parser.py
@@ -106,8 +106,10 @@ class BytesParser:
meaning it parses the entire contents of the file.
"""
fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape')
- with fp:
+ try:
return self.parser.parse(fp, headersonly)
+ finally:
+ fp.detach()
def parsebytes(self, text, headersonly=False):
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 2f89a10..af8bc7b 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -3390,6 +3390,31 @@ class TestParsers(TestEmailBase):
self.assertIsInstance(msg.get_payload(), str)
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):
eq = self.assertEqual
# This message contains a line after the Subject: header that has only