diff options
| author | Chris A <christopher.aporta@gmail.com> | 2020-03-02 01:39:50 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-02 08:39:50 +0200 |
| commit | 2565edec2c974b2acca03b4cc5025e83f903ddd7 (patch) | |
| tree | 58c6999be2b2ad47bcfcf5e5f294c8a07e81ff84 /Lib/test/test_codecs.py | |
| parent | 4edc95cf0a2960431621eee9bc194f6225f1690b (diff) | |
| download | cpython-git-2565edec2c974b2acca03b4cc5025e83f903ddd7.tar.gz | |
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.
Diffstat (limited to 'Lib/test/test_codecs.py')
| -rw-r--r-- | Lib/test/test_codecs.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 8d9cb90890..dcdd574bc7 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1154,6 +1154,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)) @@ -1725,6 +1726,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): |
