[Python-checkins] bpo-38971: Open file in codecs.open() closes if exception raised. (GH-17666)
Miss Islington (bot)
webhook-mailer at python.org
Mon Mar 2 03:02:21 EST 2020
https://github.com/python/cpython/commit/f4d709f4a3c69bd940bd6968a70241277132bed7
commit: f4d709f4a3c69bd940bd6968a70241277132bed7
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-02T00:02:16-08:00
summary:
bpo-38971: Open file in codecs.open() closes if exception raised. (GH-17666)
Open issue in the BPO indicated a desire to make the implementation of
codecs.open() at parity with io.open(), which implements a try/except to
assure file stream gets closed before an exception is raised.
(cherry picked from commit 2565edec2c974b2acca03b4cc5025e83f903ddd7)
Co-authored-by: Chris A <christopher.aporta at gmail.com>
files:
A Misc/NEWS.d/next/Library/2019-12-20-16-06-28.bpo-38971.fKRYlF.rst
M Lib/codecs.py
M Lib/test/test_codecs.py
diff --git a/Lib/codecs.py b/Lib/codecs.py
index cfca5d38b0795..5d95f25612e96 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -904,11 +904,16 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=1):
file = builtins.open(filename, mode, buffering)
if encoding is None:
return file
- info = lookup(encoding)
- srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
- # Add attributes to simplify introspection
- srw.encoding = encoding
- return srw
+
+ try:
+ info = lookup(encoding)
+ srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
+ # Add attributes to simplify introspection
+ srw.encoding = encoding
+ return srw
+ except:
+ file.close()
+ raise
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 5a450ebd9dc2b..ea9e719e7478b 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -1206,6 +1206,7 @@ def test_stream_bare(self):
got = ostream.getvalue()
self.assertEqual(got, unistring)
+
class EscapeDecodeTest(unittest.TestCase):
def test_empty(self):
self.assertEqual(codecs.escape_decode(b""), (b"", 0))
@@ -1869,6 +1870,14 @@ def test_undefined(self):
self.assertRaises(UnicodeError,
codecs.decode, b'abc', 'undefined', errors)
+ def test_file_closes_if_lookup_error_raised(self):
+ mock_open = mock.mock_open()
+ with mock.patch('builtins.open', mock_open) as file:
+ with self.assertRaises(LookupError):
+ codecs.open(support.TESTFN, 'wt', 'invalid-encoding')
+
+ file().close.assert_called()
+
class StreamReaderTest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2019-12-20-16-06-28.bpo-38971.fKRYlF.rst b/Misc/NEWS.d/next/Library/2019-12-20-16-06-28.bpo-38971.fKRYlF.rst
new file mode 100644
index 0000000000000..9676d72b44abc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-20-16-06-28.bpo-38971.fKRYlF.rst
@@ -0,0 +1,3 @@
+Open issue in the BPO indicated a desire to make the implementation of
+codecs.open() at parity with io.open(), which implements a try/except to
+assure file stream gets closed before an exception is raised.
\ No newline at end of file
More information about the Python-checkins
mailing list