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')
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')