diff -r 04d3d67ef5ac Lib/email/parser.py --- a/Lib/email/parser.py Fri May 30 11:21:14 2014 -0400 +++ b/Lib/email/parser.py Fri May 30 23:15:13 2014 +0700 @@ -106,8 +106,9 @@ meaning it parses the entire contents of the file. """ fp = TextIOWrapper(fp, encoding='ascii', errors='surrogateescape') - with fp: - return self.parser.parse(fp, headersonly) + parsed_data = self.parser.parse(fp, headersonly) + fp.detach() + return parsed_data def parsebytes(self, text, headersonly=False): diff -r 04d3d67ef5ac Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Fri May 30 11:21:14 2014 -0400 +++ b/Lib/test/test_email/test_email.py Fri May 30 23:15:13 2014 +0700 @@ -3390,6 +3390,18 @@ self.assertIsInstance(msg.get_payload(), str) self.assertIsInstance(msg.get_payload(decode=True), bytes) + def test_bytes_parser_not_close_file(self): + fp = openfile('msg_02.txt', 'rb') + self.addCleanup(fp.close) + email.parser.BytesParser().parse(fp) + self.assertFalse(fp.closed) + + def test_parser_not_close_file(self): + fp = openfile('msg_02.txt', 'r') + self.addCleanup(fp.close) + email.parser.Parser().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