Skip to content

Commit afd1e6d

Browse files
authored
bpo-36239: Skip comments in gettext infos (GH-12255)
1 parent 88db8bd commit afd1e6d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/gettext.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ def _parse(self, fp):
417417
item = b_item.decode().strip()
418418
if not item:
419419
continue
420+
# Skip over comment lines:
421+
if item.startswith('#-#-#-#-#') and item.endswith('#-#-#-#-#'):
422+
continue
420423
k = v = None
421424
if ':' in item:
422425
k, v = item.split(':', 1)

Lib/test/test_gettext.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,19 @@ def test_plural_form_error_issue17898(self):
684684
# If this runs cleanly, the bug is fixed.
685685
t = gettext.GNUTranslations(fp)
686686

687+
def test_ignore_comments_in_headers_issue36239(self):
688+
"""Checks that comments like:
689+
690+
#-#-#-#-# messages.po (EdX Studio) #-#-#-#-#
691+
692+
are ignored.
693+
"""
694+
with open(MOFILE, 'wb') as fp:
695+
fp.write(base64.decodebytes(GNU_MO_DATA_ISSUE_17898))
696+
with open(MOFILE, 'rb') as fp:
697+
t = gettext.GNUTranslations(fp)
698+
self.assertEqual(t.info()["plural-forms"], "nplurals=2; plural=(n != 1);")
699+
687700

688701
class UnicodeTranslationsTest(GettextBaseTest):
689702
def setUp(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Parsing .mo files now ignores comments starting and ending with #-#-#-#-#.

0 commit comments

Comments
 (0)