summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2014-03-28 22:46:02 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2014-03-28 22:46:02 +0100
commite2151d09e9a5edd171a4c3e4b291d7a82a12d16e (patch)
tree86aad9fce73b29d720b18aab74d745d2e688d9dd
parent7e6cadfcd256fc05e73969e18d357ceb7e614034 (diff)
downloadpep8-e2151d09e9a5edd171a4c3e4b291d7a82a12d16e.tar.gz
Use the open() context manager, supported since Python 2.5
-rwxr-xr-xpep8.py22
1 files changed, 9 insertions, 13 deletions
diff --git a/pep8.py b/pep8.py
index a1880fa..d5c652e 100755
--- a/pep8.py
+++ b/pep8.py
@@ -44,6 +44,8 @@ W warnings
700 statements
900 syntax error
"""
+from __future__ import with_statement
+
__version__ = '1.5.2a0'
import os
@@ -1039,29 +1041,23 @@ if '' == ''.encode():
# Python 2: implicit encoding.
def readlines(filename):
"""Read the source code."""
- f = open(filename)
- try:
+ with open(filename) as f:
return f.readlines()
- finally:
- f.close()
isidentifier = re.compile(r'[a-zA-Z_]\w*').match
stdin_get_value = sys.stdin.read
else:
# Python 3
def readlines(filename):
"""Read the source code."""
- f = open(filename, 'rb')
try:
- (coding, lines) = tokenize.detect_encoding(f.readline)
- f = TextIOWrapper(f, coding, line_buffering=True)
- return [l.decode(coding) for l in lines] + f.readlines()
+ with open(filename, 'rb') as f:
+ (coding, lines) = tokenize.detect_encoding(f.readline)
+ f = TextIOWrapper(f, coding, line_buffering=True)
+ return [l.decode(coding) for l in lines] + f.readlines()
except (LookupError, SyntaxError, UnicodeError):
- f.close()
# Fall back if files are improperly declared
- f = open(filename, encoding='latin-1')
- return f.readlines()
- finally:
- f.close()
+ with open(filename, encoding='latin-1') as f:
+ return f.readlines()
isidentifier = str.isidentifier
def stdin_get_value():