summaryrefslogtreecommitdiffstats
path: root/Lib/tokenize.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-05-25 22:43:58 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-05-25 22:43:58 (GMT)
commit387729e183365a366c48fce7a9abfcaf4ec6ff4e (patch)
treef04fdaba2e72148ae51f2cb9778f64d3bea4f7e8 /Lib/tokenize.py
parent410d77f230078900371d67eaec9ce190a67828ee (diff)
downloadcpython-387729e183365a366c48fce7a9abfcaf4ec6ff4e.zip
cpython-387729e183365a366c48fce7a9abfcaf4ec6ff4e.tar.gz
cpython-387729e183365a366c48fce7a9abfcaf4ec6ff4e.tar.bz2
Issue #23840: tokenize.open() now closes the temporary binary file on error to
fix a resource warning.
Diffstat (limited to 'Lib/tokenize.py')
-rw-r--r--Lib/tokenize.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/tokenize.py b/Lib/tokenize.py
index ed4153c..cf18bf9 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -435,11 +435,15 @@ def open(filename):
detect_encoding().
"""
buffer = _builtin_open(filename, 'rb')
- encoding, lines = detect_encoding(buffer.readline)
- buffer.seek(0)
- text = TextIOWrapper(buffer, encoding, line_buffering=True)
- text.mode = 'r'
- return text
+ try:
+ encoding, lines = detect_encoding(buffer.readline)
+ buffer.seek(0)
+ text = TextIOWrapper(buffer, encoding, line_buffering=True)
+ text.mode = 'r'
+ return text
+ except:
+ buffer.close()
+ raise
def tokenize(readline):