Skip to content

Commit 01ba7df

Browse files
gh-44827: Improve error if BOM on first line of .po file (GH-130187)
1 parent 185ac5a commit 01ba7df

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_tools/test_msgfmt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def test_compilation(self):
3939

4040
self.assertDictEqual(actual._catalog, expected._catalog)
4141

42+
def test_po_with_bom(self):
43+
with temp_cwd():
44+
Path('bom.po').write_bytes(b'\xef\xbb\xbfmsgid "Python"\nmsgstr "Pioton"\n')
45+
46+
res = assert_python_failure(msgfmt, 'bom.po')
47+
err = res.err.decode('utf-8')
48+
self.assertIn('The file bom.po starts with a UTF-8 BOM', err)
49+
4250
def test_invalid_msgid_plural(self):
4351
with temp_cwd():
4452
Path('invalid.po').write_text('''\

Tools/i18n/msgfmt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import struct
3333
import array
3434
from email.parser import HeaderParser
35+
import codecs
3536

3637
__version__ = "1.2"
3738

39+
3840
MESSAGES = {}
3941

4042

@@ -116,6 +118,14 @@ def make(filename, outfile):
116118
print(msg, file=sys.stderr)
117119
sys.exit(1)
118120

121+
if lines[0].startswith(codecs.BOM_UTF8):
122+
print(
123+
f"The file {infile} starts with a UTF-8 BOM which is not allowed in .po files.\n"
124+
"Please save the file without a BOM and try again.",
125+
file=sys.stderr
126+
)
127+
sys.exit(1)
128+
119129
section = msgctxt = None
120130
fuzzy = 0
121131

0 commit comments

Comments
 (0)