]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38971: Open file in codecs.open() closes if exception raised. (GH-17666)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Mar 2020 08:03:08 +0000 (00:03 -0800)
committerGitHub <noreply@github.com>
Mon, 2 Mar 2020 08:03:08 +0000 (00:03 -0800)
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@gmail.com>
Lib/codecs.py
Lib/test/test_codecs.py
Misc/NEWS.d/next/Library/2019-12-20-16-06-28.bpo-38971.fKRYlF.rst [new file with mode: 0644]

index 21c45a7d10a4c9adf3a864a2d7dab49551116602..7f23e9775df804e79e4d0869a29dc05e1396a892 100644 (file)
@@ -905,11 +905,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'):
 
index 8c10e948e8041d7d48da9c8da330d8ac37e97079..0fd258ca8abf6fe20afd6984c4d2cb5e34deeeee 100644 (file)
@@ -1142,6 +1142,7 @@ class UTF8SigTest(UTF8Test, unittest.TestCase):
             got = ostream.getvalue()
             self.assertEqual(got, unistring)
 
+
 class EscapeDecodeTest(unittest.TestCase):
     def test_empty(self):
         self.assertEqual(codecs.escape_decode(b""), (b"", 0))
@@ -1713,6 +1714,14 @@ class CodecsModuleTest(unittest.TestCase):
             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 (file)
index 0000000..9676d72
--- /dev/null
@@ -0,0 +1,3 @@
+Open issue in the BPO indicated a desire to make the implementation of\r
+codecs.open() at parity with io.open(), which implements a try/except to\r
+assure file stream gets closed before an exception is raised.
\ No newline at end of file