Skip to content

Commit 474f296

Browse files
authored
pythongh-130655: Add a test for big-endian MO files in gettext (pythonGH-132469)
1 parent f21e42d commit 474f296

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Lib/test/test_gettext.py

+26
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@
115115
0x62, 0x61, 0x72, 0x00, # Message data
116116
]))
117117

118+
119+
GNU_MO_DATA_BIG_ENDIAN = base64.b64encode(bytes([
120+
0x95, 0x04, 0x12, 0xDE, # Magic
121+
0x00, 0x00, 0x00, 0x00, # Version
122+
0x00, 0x00, 0x00, 0x01, # Message count
123+
0x00, 0x00, 0x00, 0x1C, # Message offset
124+
0x00, 0x00, 0x00, 0x24, # Translation offset
125+
0x00, 0x00, 0x00, 0x00, # Hash table size
126+
0x00, 0x00, 0x00, 0x2C, # Hash table offset
127+
0x00, 0x00, 0x00, 0x03, # 1st message length
128+
0x00, 0x00, 0x00, 0x2C, # 1st message offset
129+
0x00, 0x00, 0x00, 0x03, # 1st trans length
130+
0x00, 0x00, 0x00, 0x30, # 1st trans offset
131+
0x66, 0x6F, 0x6F, 0x00, # Message data
132+
0x62, 0x61, 0x72, 0x00, # Message data
133+
]))
134+
118135
UMO_DATA = b'''\
119136
3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
120137
AAAABAAAAHMBAAAWAAAAeAEAAABhYsOeAG15Y29udGV4dMOeBGFiw54AUHJvamVjdC1JZC1WZXJz
@@ -142,6 +159,7 @@
142159
MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
143160
MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
144161
MOFILE_CORRUPT = os.path.join(LOCALEDIR, 'gettext_corrupt.mo')
162+
MOFILE_BIG_ENDIAN = os.path.join(LOCALEDIR, 'gettext_big_endian.mo')
145163
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
146164
MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
147165

@@ -168,6 +186,8 @@ def setUpClass(cls):
168186
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MINOR_VERSION))
169187
with open(MOFILE_CORRUPT, 'wb') as fp:
170188
fp.write(base64.decodebytes(GNU_MO_DATA_CORRUPT))
189+
with open(MOFILE_BIG_ENDIAN, 'wb') as fp:
190+
fp.write(base64.decodebytes(GNU_MO_DATA_BIG_ENDIAN))
171191
with open(UMOFILE, 'wb') as fp:
172192
fp.write(base64.decodebytes(UMO_DATA))
173193
with open(MMOFILE, 'wb') as fp:
@@ -293,6 +313,12 @@ def test_corrupt_file(self):
293313
self.assertEqual(exception.strerror, "File is corrupt")
294314
self.assertEqual(exception.filename, MOFILE_CORRUPT)
295315

316+
def test_big_endian_file(self):
317+
with open(MOFILE_BIG_ENDIAN, 'rb') as fp:
318+
t = gettext.GNUTranslations(fp)
319+
320+
self.assertEqual(t.gettext('foo'), 'bar')
321+
296322
def test_some_translations(self):
297323
eq = self.assertEqual
298324
# test some translations

0 commit comments

Comments
 (0)