diff options
-rw-r--r-- | NEWS.txt | 3 | ||||
-rw-r--r-- | pyflakes/api.py | 8 | ||||
-rw-r--r-- | pyflakes/test/test_api.py | 16 |
3 files changed, 21 insertions, 6 deletions
@@ -1,3 +1,6 @@ +UNRELEASED: + - Detect the declared encoding in Python 3. + 0.8.0 (2014-03-22): - Adapt for the AST in Python 3.4. - Fix caret position on SyntaxError. diff --git a/pyflakes/api.py b/pyflakes/api.py index 03848c3..f32881e 100644 --- a/pyflakes/api.py +++ b/pyflakes/api.py @@ -13,8 +13,6 @@ from pyflakes import reporter as modReporter __all__ = ['check', 'checkPath', 'checkRecursive', 'iterSourceCode', 'main'] -universal_newline = ('U' if sys.version_info < (3, 0) else 'r') - def check(codeString, filename, reporter=None): """ @@ -76,8 +74,10 @@ def checkPath(filename, reporter=None): if reporter is None: reporter = modReporter._makeDefaultReporter() try: - with open(filename, universal_newline) as f: - codestr = f.read() + '\n' + with open(filename, 'rb') as f: + codestr = f.read() + if sys.version_info < (2, 7): + codestr += '\n' # Work around for Python <= 2.6 except UnicodeError: reporter.unexpectedError(filename, 'problem decoding source') return 1 diff --git a/pyflakes/test/test_api.py b/pyflakes/test/test_api.py index 1d58d80..82b92f6 100644 --- a/pyflakes/test/test_api.py +++ b/pyflakes/test/test_api.py @@ -15,7 +15,7 @@ from pyflakes.api import ( checkRecursive, iterSourceCode, ) -from pyflakes.test.harness import TestCase, skipIf +from pyflakes.test.harness import TestCase if sys.version_info < (3,): from cStringIO import StringIO @@ -437,7 +437,19 @@ foo = '\\xyz' self.assertEqual( errors, [('flake', str(UnusedImport(sourcePath, Node(1), 'foo')))]) - @skipIf(sys.version_info >= (3,), "not relevant") + def test_encodedFileUTF8(self): + """ + If a source file contains bytes which cannot be decoded, this is + reported on stderr. + """ + SNOWMAN = unichr(0x2603) + source = ("""\ +# coding: utf-8 +x = "%s" +""" % SNOWMAN).encode('utf-8') + sourcePath = self.makeTempFile(source) + self.assertHasErrors(sourcePath, []) + def test_misencodedFileUTF8(self): """ If a source file contains bytes which cannot be decoded, this is |