From: Leonardo Pistone Date: Fri, 7 Aug 2015 14:43:41 +0000 (+0200) Subject: allow utf8 BOM + magic comment, closes #189 X-Git-Tag: dev-2a51c9b95d06~30^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da87edd1e5ff71ff7d072318b664f2940b12e3f2;p=thirdparty%2Fbabel.git allow utf8 BOM + magic comment, closes #189 --- diff --git a/babel/util.py b/babel/util.py index a65fce36..dd737872 100644 --- a/babel/util.py +++ b/babel/util.py @@ -77,9 +77,11 @@ def parse_encoding(fp): if has_bom: if m: - raise SyntaxError( - "python refuses to compile code with both a UTF8 " - "byte-order-mark and a magic encoding comment") + magic_comment_encoding = m.group(1).decode('latin-1') + if magic_comment_encoding != 'utf-8': + raise SyntaxError( + 'encoding problem: {0} with BOM'.format( + magic_comment_encoding)) return 'utf-8' elif m: return m.group(1).decode('latin-1') diff --git a/tests/messages/test_extract.py b/tests/messages/test_extract.py index 62c72277..ded697f7 100644 --- a/tests/messages/test_extract.py +++ b/tests/messages/test_extract.py @@ -343,6 +343,23 @@ msg = _('Bonjour à tous') self.assertEqual(u'Bonjour à tous', messages[0][2]) self.assertEqual([u'NOTE: hello'], messages[0][3]) + def test_utf8_message_with_utf8_bom_and_magic_comment(self): + buf = BytesIO(codecs.BOM_UTF8 + u"""# -*- coding: utf-8 -*- +# NOTE: hello +msg = _('Bonjour à tous') +""".encode('utf-8')) + messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) + self.assertEqual(u'Bonjour à tous', messages[0][2]) + self.assertEqual([u'NOTE: hello'], messages[0][3]) + + def test_utf8_bom_with_latin_magic_comment_fails(self): + buf = BytesIO(codecs.BOM_UTF8 + u"""# -*- coding: latin-1 -*- +# NOTE: hello +msg = _('Bonjour à tous') +""".encode('utf-8')) + self.assertRaises(SyntaxError, list, + extract.extract_python(buf, ('_',), ['NOTE:'], {})) + def test_utf8_raw_strings_match_unicode_strings(self): buf = BytesIO(codecs.BOM_UTF8 + u""" msg = _('Bonjour à tous')