]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-44827: Improve error if BOM on first line of .po file (GH-130187)
authorStan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Tue, 18 Feb 2025 13:59:34 +0000 (13:59 +0000)
committerGitHub <noreply@github.com>
Tue, 18 Feb 2025 13:59:34 +0000 (15:59 +0200)
Lib/test/test_tools/test_msgfmt.py
Tools/i18n/msgfmt.py

index e3e3035c4f4395d81ebd5c8f5922093655da266b..a6073b8be030731588497310bdab2c0d72a4e429 100644 (file)
@@ -39,6 +39,14 @@ class CompilationTest(unittest.TestCase):
 
                     self.assertDictEqual(actual._catalog, expected._catalog)
 
+    def test_po_with_bom(self):
+        with temp_cwd():
+            Path('bom.po').write_bytes(b'\xef\xbb\xbfmsgid "Python"\nmsgstr "Pioton"\n')
+
+            res = assert_python_failure(msgfmt, 'bom.po')
+            err = res.err.decode('utf-8')
+            self.assertIn('The file bom.po starts with a UTF-8 BOM', err)
+
     def test_invalid_msgid_plural(self):
         with temp_cwd():
             Path('invalid.po').write_text('''\
index 3f731e941eafe77418e9e8c451a52170ee2bc8fa..f005c4e7b5b79eaf3de26559ff5390dfdac776b3 100755 (executable)
@@ -32,9 +32,11 @@ import getopt
 import struct
 import array
 from email.parser import HeaderParser
+import codecs
 
 __version__ = "1.2"
 
+
 MESSAGES = {}
 
 
@@ -116,6 +118,14 @@ def make(filename, outfile):
         print(msg, file=sys.stderr)
         sys.exit(1)
 
+    if lines[0].startswith(codecs.BOM_UTF8):
+        print(
+            f"The file {infile} starts with a UTF-8 BOM which is not allowed in .po files.\n"
+            "Please save the file without a BOM and try again.",
+            file=sys.stderr
+        )
+        sys.exit(1)
+
     section = msgctxt = None
     fuzzy = 0