Skip to content

Commit 07aa84f

Browse files
committed
Merge pull request #274 from imankulov/fix-encoding-detection
Fix UnicodeEncodeError on file encoding detection
2 parents 4f1e0c5 + f9b04a5 commit 07aa84f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

babel/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def parse_encoding(fp):
6565
try:
6666
import parser
6767
parser.suite(line1.decode('latin-1'))
68-
except (ImportError, SyntaxError):
68+
except (ImportError, SyntaxError, UnicodeEncodeError):
6969
# Either it's a real syntax error, in which case the source is
7070
# not valid python source, or line2 is a continuation of line1,
7171
# in which case we don't want to scan line2 for a magic

tests/test_util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import unittest
1515

1616
from babel import util
17+
from babel._compat import BytesIO
1718

1819

1920
def test_distinct():
@@ -52,3 +53,17 @@ def test_zone_zero_offset(self):
5253
def test_zone_positive_offset(self):
5354
self.assertEqual('Etc/GMT+330', util.FixedOffsetTimezone(330).zone)
5455

56+
57+
parse_encoding = lambda s: util.parse_encoding(BytesIO(s.encode('utf-8')))
58+
59+
60+
def test_parse_encoding_defined():
61+
assert parse_encoding(u'# coding: utf-8') == 'utf-8'
62+
63+
64+
def test_parse_encoding_undefined():
65+
assert parse_encoding(u'') is None
66+
67+
68+
def test_parse_encoding_non_ascii():
69+
assert parse_encoding(u'K\xf6ln') is None

0 commit comments

Comments
 (0)