summaryrefslogtreecommitdiff
path: root/pyflakes/api.py
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2013-04-21 11:32:10 +0200
committerFlorent Xicluna <florent.xicluna@gmail.com>2013-04-21 11:32:10 +0200
commite05ebfa4f48b6a1802f2a228b678c1e487f42c4e (patch)
tree76352b88369caf2cceceb81b48970c524eef48c1 /pyflakes/api.py
parent830f3851cb96359483071fb24e51759b7323882c (diff)
downloadpyflakes-e05ebfa4f48b6a1802f2a228b678c1e487f42c4e.tar.gz
Improve granularity of Unicode error handler
Diffstat (limited to 'pyflakes/api.py')
-rw-r--r--pyflakes/api.py25
1 files changed, 12 insertions, 13 deletions
diff --git a/pyflakes/api.py b/pyflakes/api.py
index 17a9257..b132566 100644
--- a/pyflakes/api.py
+++ b/pyflakes/api.py
@@ -1,6 +1,7 @@
"""
API for the command-line I{pyflakes} tool.
"""
+from __future__ import with_statement
import sys
import os
@@ -53,13 +54,12 @@ def check(codeString, filename, reporter=None):
except Exception:
reporter.unexpectedError(filename, 'problem decoding source')
return 1
- else:
- # Okay, it's syntactically valid. Now check it.
- w = checker.Checker(tree, filename)
- w.messages.sort(key=lambda m: m.lineno)
- for warning in w.messages:
- reporter.flake(warning)
- return len(w.messages)
+ # Okay, it's syntactically valid. Now check it.
+ w = checker.Checker(tree, filename)
+ w.messages.sort(key=lambda m: m.lineno)
+ for warning in w.messages:
+ reporter.flake(warning)
+ return len(w.messages)
def checkPath(filename, reporter=None):
@@ -74,17 +74,16 @@ def checkPath(filename, reporter=None):
if reporter is None:
reporter = modReporter._makeDefaultReporter()
try:
- f = open(filename, 'U')
- try:
- return check(f.read() + '\n', filename, reporter)
- finally:
- f.close()
+ with open(filename, 'U') as f:
+ codestr = f.read() + '\n'
except UnicodeError:
reporter.unexpectedError(filename, 'problem decoding source')
+ return 1
except IOError:
msg = sys.exc_info()[1]
reporter.unexpectedError(filename, msg.args[1])
- return 1
+ return 1
+ return check(codestr, filename, reporter)
def iterSourceCode(paths):