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.
This commit is contained in:
parent
4edc95cf0a
commit
2565edec2c
|
@ -905,11 +905,16 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
|
||||||
file = builtins.open(filename, mode, buffering)
|
file = builtins.open(filename, mode, buffering)
|
||||||
if encoding is None:
|
if encoding is None:
|
||||||
return file
|
return file
|
||||||
|
|
||||||
|
try:
|
||||||
info = lookup(encoding)
|
info = lookup(encoding)
|
||||||
srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
|
srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
|
||||||
# Add attributes to simplify introspection
|
# Add attributes to simplify introspection
|
||||||
srw.encoding = encoding
|
srw.encoding = encoding
|
||||||
return srw
|
return srw
|
||||||
|
except:
|
||||||
|
file.close()
|
||||||
|
raise
|
||||||
|
|
||||||
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
|
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
|
||||||
|
|
||||||
|
|
|
@ -1154,6 +1154,7 @@ class UTF8SigTest(UTF8Test, unittest.TestCase):
|
||||||
got = ostream.getvalue()
|
got = ostream.getvalue()
|
||||||
self.assertEqual(got, unistring)
|
self.assertEqual(got, unistring)
|
||||||
|
|
||||||
|
|
||||||
class EscapeDecodeTest(unittest.TestCase):
|
class EscapeDecodeTest(unittest.TestCase):
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
self.assertEqual(codecs.escape_decode(b""), (b"", 0))
|
self.assertEqual(codecs.escape_decode(b""), (b"", 0))
|
||||||
|
@ -1725,6 +1726,14 @@ class CodecsModuleTest(unittest.TestCase):
|
||||||
self.assertRaises(UnicodeError,
|
self.assertRaises(UnicodeError,
|
||||||
codecs.decode, b'abc', 'undefined', errors)
|
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):
|
class StreamReaderTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue