diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2014-03-28 22:46:02 +0100 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2014-03-28 22:46:02 +0100 |
commit | e2151d09e9a5edd171a4c3e4b291d7a82a12d16e (patch) | |
tree | 86aad9fce73b29d720b18aab74d745d2e688d9dd /pep8.py | |
parent | 7e6cadfcd256fc05e73969e18d357ceb7e614034 (diff) | |
download | pep8-e2151d09e9a5edd171a4c3e4b291d7a82a12d16e.tar.gz |
Use the open() context manager, supported since Python 2.5
Diffstat (limited to 'pep8.py')
-rwxr-xr-x | pep8.py | 22 |
1 files changed, 9 insertions, 13 deletions
@@ -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(): |