summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <nedbat@gmail.com>2014-07-05 07:26:30 -0400
committerNed Batchelder <nedbat@gmail.com>2014-07-05 07:26:30 -0400
commitb3a105307dbdf2ac07887c7fbdcd439cf49d8e66 (patch)
treef42cfd2048f43fa521d25a887bce0dfc8c4ad4a7
parent1aeeca32c2c17eb99ab83e6e3ebf7feb5cafedff (diff)
parent460d303ed2361abe554da075c184a370d7d54701 (diff)
downloadpython-coveragepy-git-b3a105307dbdf2ac07887c7fbdcd439cf49d8e66.tar.gz
Merged in asottile/coverage.py/fix_source_encoding (pull request #38)
Fix non-comment encoding detection.
-rw-r--r--coverage/phystokens.py2
-rw-r--r--tests/test_phystokens.py5
2 files changed, 6 insertions, 1 deletions
diff --git a/coverage/phystokens.py b/coverage/phystokens.py
index e79ce01f..867388f7 100644
--- a/coverage/phystokens.py
+++ b/coverage/phystokens.py
@@ -120,7 +120,7 @@ def source_encoding(source):
# This is mostly code adapted from Py3.2's tokenize module.
- cookie_re = re.compile(r"coding[:=]\s*([-\w.]+)")
+ cookie_re = re.compile(r"^\s*#.*coding[:=]\s*([-\w.]+)")
# Do this so the detect_encode code we copied will work.
readline = iter(source.splitlines(True)).next
diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py
index e15400b6..4755c167 100644
--- a/tests/test_phystokens.py
+++ b/tests/test_phystokens.py
@@ -97,6 +97,11 @@ if sys.version_info < (3, 0):
source = "# This Python file uses this encoding: utf-8\n"
self.assertEqual(source_encoding(source), 'utf-8')
+ def test_detect_source_encoding_not_in_comment(self):
+ # Should not detect anything here
+ source = 'def parse(src, encoding=None):\n pass'
+ self.assertEqual(source_encoding(source), 'ascii')
+
def test_detect_source_encoding_on_second_line(self):
# A coding declaration should be found despite a first blank line.
source = "\n# coding=cp850\n\n"