Skip to content

Commit 90c786e

Browse files
[3.13] pythongh-130655: Add a test for big-endian MO files in gettext (pythonGH-132469) (pythonGH-133248)
(cherry picked from commit 474f296) Co-authored-by: Tomas R <[email protected]>
1 parent 679e632 commit 90c786e

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
@@ -117,6 +117,23 @@
117117
0x62, 0x61, 0x72, 0x00, # Message data
118118
]))
119119

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

@@ -170,6 +188,8 @@ def setUpClass(cls):
170188
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MINOR_VERSION))
171189
with open(MOFILE_CORRUPT, 'wb') as fp:
172190
fp.write(base64.decodebytes(GNU_MO_DATA_CORRUPT))
191+
with open(MOFILE_BIG_ENDIAN, 'wb') as fp:
192+
fp.write(base64.decodebytes(GNU_MO_DATA_BIG_ENDIAN))
173193
with open(UMOFILE, 'wb') as fp:
174194
fp.write(base64.decodebytes(UMO_DATA))
175195
with open(MMOFILE, 'wb') as fp:
@@ -319,6 +339,12 @@ def test_corrupt_file(self):
319339
self.assertEqual(exception.strerror, "File is corrupt")
320340
self.assertEqual(exception.filename, MOFILE_CORRUPT)
321341

342+
def test_big_endian_file(self):
343+
with open(MOFILE_BIG_ENDIAN, 'rb') as fp:
344+
t = gettext.GNUTranslations(fp)
345+
346+
self.assertEqual(t.gettext('foo'), 'bar')
347+
322348
def test_some_translations(self):
323349
eq = self.assertEqual
324350
# test some translations

0 commit comments

Comments
 (0)