summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2014-03-29 12:09:23 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2014-03-29 12:09:23 +0100
commita2cd4d1575fd13862ae43ee9e490040ffce0aad1 (patch)
tree0351316ab44d6ec2fc29e6eebcfca8cd7c22667f
parent7b85f86578d7cbd203192dcd189f3fc375163339 (diff)
downloadpyflakes-a2cd4d1575fd13862ae43ee9e490040ffce0aad1.tar.gz
Detect the declared encoding in Python 3; fixes lp:1299169
-rw-r--r--NEWS.txt3
-rw-r--r--pyflakes/api.py8
-rw-r--r--pyflakes/test/test_api.py16
3 files changed, 21 insertions, 6 deletions
diff --git a/NEWS.txt b/NEWS.txt
index aee125c..776fb6f 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -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