[Python-checkins] cpython (3.4): #21476: Unwrap fp in BytesParser so the file isn't unexpectedly closed.
r.david.murray
python-checkins at python.org
Thu Jun 26 19:33:32 CEST 2014
http://hg.python.org/cpython/rev/0a16756dfcc0
changeset: 91427:0a16756dfcc0
branch: 3.4
parent: 91423:463f499ef591
user: R David Murray <rdmurray at bitdance.com>
date: Thu Jun 26 13:31:43 2014 -0400
summary:
#21476: Unwrap fp in BytesParser so the file isn't unexpectedly closed.
This makes the behavior match that of Parser. Patch by Vajrasky Kok.
files:
Lib/email/parser.py | 4 ++-
Lib/test/test_email/test_email.py | 25 +++++++++++++++++++
Misc/NEWS | 3 ++
3 files changed, 31 insertions(+), 1 deletions(-)
diff --git a/Lib/email/parser.py b/Lib/email/parser.py
--- a/Lib/email/parser.py
+++ b/Lib/email/parser.py
@@ -106,8 +106,10 @@
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
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -3390,6 +3390,31 @@
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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
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
files closing. Patch by Claudiu Popa.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list