|
44 | 44 | 700 statements |
45 | 45 | 900 syntax error |
46 | 46 | """ |
| 47 | +from __future__ import with_statement |
| 48 | + |
47 | 49 | __version__ = '1.5.2a0' |
48 | 50 |
|
49 | 51 | import os |
@@ -1039,29 +1041,23 @@ def python_3000_backticks(logical_line): |
1039 | 1041 | # Python 2: implicit encoding. |
1040 | 1042 | def readlines(filename): |
1041 | 1043 | """Read the source code.""" |
1042 | | - f = open(filename) |
1043 | | - try: |
| 1044 | + with open(filename) as f: |
1044 | 1045 | return f.readlines() |
1045 | | - finally: |
1046 | | - f.close() |
1047 | 1046 | isidentifier = re.compile(r'[a-zA-Z_]\w*').match |
1048 | 1047 | stdin_get_value = sys.stdin.read |
1049 | 1048 | else: |
1050 | 1049 | # Python 3 |
1051 | 1050 | def readlines(filename): |
1052 | 1051 | """Read the source code.""" |
1053 | | - f = open(filename, 'rb') |
1054 | 1052 | try: |
1055 | | - (coding, lines) = tokenize.detect_encoding(f.readline) |
1056 | | - f = TextIOWrapper(f, coding, line_buffering=True) |
1057 | | - return [l.decode(coding) for l in lines] + f.readlines() |
| 1053 | + with open(filename, 'rb') as f: |
| 1054 | + (coding, lines) = tokenize.detect_encoding(f.readline) |
| 1055 | + f = TextIOWrapper(f, coding, line_buffering=True) |
| 1056 | + return [l.decode(coding) for l in lines] + f.readlines() |
1058 | 1057 | except (LookupError, SyntaxError, UnicodeError): |
1059 | | - f.close() |
1060 | 1058 | # Fall back if files are improperly declared |
1061 | | - f = open(filename, encoding='latin-1') |
1062 | | - return f.readlines() |
1063 | | - finally: |
1064 | | - f.close() |
| 1059 | + with open(filename, encoding='latin-1') as f: |
| 1060 | + return f.readlines() |
1065 | 1061 | isidentifier = str.isidentifier |
1066 | 1062 |
|
1067 | 1063 | def stdin_get_value(): |
|
0 commit comments