summaryrefslogtreecommitdiffstats
path: root/Lib/codecs.py
diff options
context:
space:
mode:
authorChris A <christopher.aporta@gmail.com>2020-03-02 06:39:50 (GMT)
committerGitHub <noreply@github.com>2020-03-02 06:39:50 (GMT)
commit2565edec2c974b2acca03b4cc5025e83f903ddd7 (patch)
tree58c6999be2b2ad47bcfcf5e5f294c8a07e81ff84 /Lib/codecs.py
parent4edc95cf0a2960431621eee9bc194f6225f1690b (diff)
downloadcpython-2565edec2c974b2acca03b4cc5025e83f903ddd7.zip
cpython-2565edec2c974b2acca03b4cc5025e83f903ddd7.tar.gz
cpython-2565edec2c974b2acca03b4cc5025e83f903ddd7.tar.bz2
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/codecs.py')
-rw-r--r--Lib/codecs.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py
index 21c45a7..7f23e97 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -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'):