Skip to content

Instantly share code, notes, and snippets.

@itsmemattchung
Created April 1, 2015 20:48
Show Gist options
  • Save itsmemattchung/dd7344397766c0a05605 to your computer and use it in GitHub Desktop.
Save itsmemattchung/dd7344397766c0a05605 to your computer and use it in GitHub Desktop.
Patch for tokenize.py
diff --git a/Lib/tokenize.py b/Lib/tokenize.py
index 8bc83fd..96edf6c 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -434,12 +434,16 @@ def open(filename):
"""Open a file in read only mode using the encoding detected by
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:
+ 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
+ except Exception as err:
+ buffer.close()
+ raise
def tokenize(readline):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment