Debian Codegraph @

Lib/test/test_gettext.py

raw
1 import os
2 import base64
3 import gettext
4 import unittest
5 import unittest.mock
6 from functools import partial
7
8 from test import support
9 from test.support import cpython_only, os_helper
10 from test.support.import_helper import ensure_lazy_imports
11
12
13 # TODO:
14 # - Add new tests, for example for "dgettext"
15 # - Tests should have only one assert.
16
17 GNU_MO_DATA = b'''\
18 3hIElQAAAAAJAAAAHAAAAGQAAAAAAAAArAAAAAAAAACsAAAAFQAAAK0AAAAjAAAAwwAAAKEAAADn
19 AAAAMAAAAIkBAAAHAAAAugEAABYAAADCAQAAHAAAANkBAAALAAAA9gEAAEIBAAACAgAAFgAAAEUD
20 AAAeAAAAXAMAAKEAAAB7AwAAMgAAAB0EAAAFAAAAUAQAABsAAABWBAAAIQAAAHIEAAAJAAAAlAQA
21 AABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhlcmUgaXMgJXMgZmlsZQBUaGVyZSBhcmUgJXMgZmls
22 ZXMAVGhpcyBtb2R1bGUgcHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXph
23 dGlvbgpzdXBwb3J0IGZvciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50
24 ZXJmYWNlIHRvIHRoZSBHTlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AV2l0aCBj
25 b250ZXh0BFRoZXJlIGlzICVzIGZpbGUAVGhlcmUgYXJlICVzIGZpbGVzAG11bGx1c2sAbXkgY29u
26 dGV4dARudWRnZSBudWRnZQBteSBvdGhlciBjb250ZXh0BG51ZGdlIG51ZGdlAG51ZGdlIG51ZGdl
27 AFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDMtMDQtMTEgMTQ6
28 MzItMDQwMApMYXN0LVRyYW5zbGF0b3I6IEouIERhdmlkIEliYW5leiA8ai1kYXZpZEBub29zLmZy
29 PgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246
30 IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4NTktMQpDb250ZW50
31 LVRyYW5zZmVyLUVuY29kaW5nOiA4Yml0CkdlbmVyYXRlZC1CeTogcHlnZXR0ZXh0LnB5IDEuMQpQ
32 bHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0d29iYmxlciBNYW5n
33 cm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFoeXIgY2ViaXZxcmYg
34 dmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVnIHNiZSBsYmhlIENs
35 Z3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1ciBUQUgKdHJnZ3Jr
36 ZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4ASGF5ICVzIGZpY2hlcm8gKGNvbnRleHQpAEhheSAl
37 cyBmaWNoZXJvcyAoY29udGV4dCkAYmFjb24Ad2luayB3aW5rIChpbiAibXkgY29udGV4dCIpAHdp
38 bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
39 '''
40
41 # .mo file with an invalid magic number
42 GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD')
43
44 # This data contains an invalid major version number (5)
45 # An unexpected major version number should be treated as an error when
46 # parsing a .mo file
47
48 GNU_MO_DATA_BAD_MAJOR_VERSION = b'''\
49 3hIElQAABQAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
50 AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
51 AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
52 eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
53 aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
54 CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
55 Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
56 ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
57 MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
58 YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
59 SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
60 NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
61 ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
62 d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
63 eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
64 IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
65 ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
66 '''
67
68 # This data contains an invalid minor version number (7)
69 # An unexpected minor version number only indicates that some of the file's
70 # contents may not be able to be read. It does not indicate an error.
71
72 GNU_MO_DATA_BAD_MINOR_VERSION = b'''\
73 3hIElQcAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
74 AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
75 AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
76 eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
77 aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
78 CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
79 Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
80 ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
81 MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
82 YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
83 SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
84 NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
85 ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
86 d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
87 eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
88 IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
89 ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
90 '''
91
92 # Corrupt .mo file
93 # Generated from
94 #
95 # msgid "foo"
96 # msgstr "bar"
97 #
98 # with msgfmt --no-hash
99 #
100 # The translation offset is changed to 0xFFFFFFFF,
101 # making it larger than the file size, which should
102 # raise an error when parsing.
103 GNU_MO_DATA_CORRUPT = base64.b64encode(bytes([
104 0xDE, 0x12, 0x04, 0x95, # Magic
105 0x00, 0x00, 0x00, 0x00, # Version
106 0x01, 0x00, 0x00, 0x00, # Message count
107 0x1C, 0x00, 0x00, 0x00, # Message offset
108 0x24, 0x00, 0x00, 0x00, # Translation offset
109 0x00, 0x00, 0x00, 0x00, # Hash table size
110 0x2C, 0x00, 0x00, 0x00, # Hash table offset
111 0x03, 0x00, 0x00, 0x00, # 1st message length
112 0x2C, 0x00, 0x00, 0x00, # 1st message offset
113 0x03, 0x00, 0x00, 0x00, # 1st trans length
114 0xFF, 0xFF, 0xFF, 0xFF, # 1st trans offset (Modified to make it invalid)
115 0x66, 0x6F, 0x6F, 0x00, # Message data
116 0x62, 0x61, 0x72, 0x00, # Message data
117 ]))
118
119
120 GNU_MO_DATA_BIG_ENDIAN = base64.b64encode(bytes([
121 0x95, 0x04, 0x12, 0xDE, # Magic
122 0x00, 0x00, 0x00, 0x00, # Version
123 0x00, 0x00, 0x00, 0x01, # Message count
124 0x00, 0x00, 0x00, 0x1C, # Message offset
125 0x00, 0x00, 0x00, 0x24, # Translation offset
126 0x00, 0x00, 0x00, 0x00, # Hash table size
127 0x00, 0x00, 0x00, 0x2C, # Hash table offset
128 0x00, 0x00, 0x00, 0x03, # 1st message length
129 0x00, 0x00, 0x00, 0x2C, # 1st message offset
130 0x00, 0x00, 0x00, 0x03, # 1st trans length
131 0x00, 0x00, 0x00, 0x30, # 1st trans offset
132 0x66, 0x6F, 0x6F, 0x00, # Message data
133 0x62, 0x61, 0x72, 0x00, # Message data
134 ]))
135
136 UMO_DATA = b'''\
137 3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
138 AAAABAAAAHMBAAAWAAAAeAEAAABhYsOeAG15Y29udGV4dMOeBGFiw54AUHJvamVjdC1JZC1WZXJz
139 aW9uOiAyLjAKUE8tUmV2aXNpb24tRGF0ZTogMjAwMy0wNC0xMSAxMjo0Mi0wNDAwCkxhc3QtVHJh
140 bnNsYXRvcjogQmFycnkgQS4gV0Fyc2F3IDxiYXJyeUBweXRob24ub3JnPgpMYW5ndWFnZS1UZWFt
141 OiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5
142 cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgKQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzog
143 N2JpdApHZW5lcmF0ZWQtQnk6IG1hbnVhbGx5CgDCpHl6AMKkeXogKGNvbnRleHQgdmVyc2lvbikA
144 '''
145
146 MMO_DATA = b'''\
147 3hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA
148 UHJvamVjdC1JZC1WZXJzaW9uOiBObyBQcm9qZWN0IDAuMApQT1QtQ3JlYXRpb24tRGF0ZTogV2Vk
149 IERlYyAxMSAwNzo0NDoxNSAyMDAyClBPLVJldmlzaW9uLURhdGU6IDIwMDItMDgtMTQgMDE6MTg6
150 NTgrMDA6MDAKTGFzdC1UcmFuc2xhdG9yOiBKb2huIERvZSA8amRvZUBleGFtcGxlLmNvbT4KSmFu
151 ZSBGb29iYXIgPGpmb29iYXJAZXhhbXBsZS5jb20+Ckxhbmd1YWdlLVRlYW06IHh4IDx4eEBleGFt
152 cGxlLmNvbT4KTUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFy
153 c2V0PWlzby04ODU5LTE1CkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFi
154 bGUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4zCgA=
155 '''
156
157 LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
158 MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
159 MOFILE_BAD_MAGIC_NUMBER = os.path.join(LOCALEDIR, 'gettext_bad_magic_number.mo')
160 MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
161 MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
162 MOFILE_CORRUPT = os.path.join(LOCALEDIR, 'gettext_corrupt.mo')
163 MOFILE_BIG_ENDIAN = os.path.join(LOCALEDIR, 'gettext_big_endian.mo')
164 UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
165 MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
166
167
168 def reset_gettext():
169 gettext._localedirs.clear()
170 gettext._current_domain = 'messages'
171 gettext._translations.clear()
172
173
174 class GettextBaseTest(unittest.TestCase):
175 @classmethod
176 def setUpClass(cls):
177 cls.addClassCleanup(os_helper.rmtree, os.path.split(LOCALEDIR)[0])
178 if not os.path.isdir(LOCALEDIR):
179 os.makedirs(LOCALEDIR)
180 with open(MOFILE, 'wb') as fp:
181 fp.write(base64.decodebytes(GNU_MO_DATA))
182 with open(MOFILE_BAD_MAGIC_NUMBER, 'wb') as fp:
183 fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAGIC_NUMBER))
184 with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp:
185 fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
186 with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
187 fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MINOR_VERSION))
188 with open(MOFILE_CORRUPT, 'wb') as fp:
189 fp.write(base64.decodebytes(GNU_MO_DATA_CORRUPT))
190 with open(MOFILE_BIG_ENDIAN, 'wb') as fp:
191 fp.write(base64.decodebytes(GNU_MO_DATA_BIG_ENDIAN))
192 with open(UMOFILE, 'wb') as fp:
193 fp.write(base64.decodebytes(UMO_DATA))
194 with open(MMOFILE, 'wb') as fp:
195 fp.write(base64.decodebytes(MMO_DATA))
196
197 def setUp(self):
198 self.env = self.enterContext(os_helper.EnvironmentVarGuard())
199 self.env['LANGUAGE'] = 'xx'
200 reset_gettext()
201 self.addCleanup(reset_gettext)
202
203
204 GNU_MO_DATA_ISSUE_17898 = b'''\
205 3hIElQAAAAABAAAAHAAAACQAAAAAAAAAAAAAAAAAAAAsAAAAggAAAC0AAAAAUGx1cmFsLUZvcm1z
206 OiBucGx1cmFscz0yOyBwbHVyYWw9KG4gIT0gMSk7CiMtIy0jLSMtIyAgbWVzc2FnZXMucG8gKEVk
207 WCBTdHVkaW8pICAjLSMtIy0jLSMKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PVVU
208 Ri04CgA=
209 '''
210
211 class GettextTestCase1(GettextBaseTest):
212 def setUp(self):
213 GettextBaseTest.setUp(self)
214 self.localedir = os.curdir
215 self.mofile = MOFILE
216 gettext.install('gettext', self.localedir, names=['pgettext'])
217
218 def test_some_translations(self):
219 eq = self.assertEqual
220 # test some translations
221 eq(_('albatross'), 'albatross')
222 eq(_('mullusk'), 'bacon')
223 eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
224 eq(_(r'nudge nudge'), 'wink wink')
225
226 def test_some_translations_with_context(self):
227 eq = self.assertEqual
228 eq(pgettext('my context', 'nudge nudge'),
229 'wink wink (in "my context")')
230 eq(pgettext('my other context', 'nudge nudge'),
231 'wink wink (in "my other context")')
232
233 def test_multiline_strings(self):
234 eq = self.assertEqual
235 # multiline strings
236 eq(_('''This module provides internationalization and localization
237 support for your Python programs by providing an interface to the GNU
238 gettext message catalog library.'''),
239 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
240 fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
241 trggrkg zrffntr pngnybt yvoenel.''')
242
243 def test_the_alternative_interface(self):
244 eq = self.assertEqual
245 neq = self.assertNotEqual
246 # test the alternative interface
247 with open(self.mofile, 'rb') as fp:
248 t = gettext.GNUTranslations(fp)
249 # Install the translation object
250 t.install()
251 eq(_('nudge nudge'), 'wink wink')
252 # Try unicode return type
253 t.install()
254 eq(_('mullusk'), 'bacon')
255 # Test installation of other methods
256 import builtins
257 t.install(names=["gettext", "ngettext"])
258 eq(_, t.gettext)
259 eq(builtins.gettext, t.gettext)
260 eq(ngettext, t.ngettext)
261 neq(pgettext, t.pgettext)
262 del builtins.gettext
263 del builtins.ngettext
264
265
266 class GettextTestCase2(GettextBaseTest):
267 def setUp(self):
268 GettextBaseTest.setUp(self)
269 self.localedir = os.curdir
270 # Set up the bindings
271 gettext.bindtextdomain('gettext', self.localedir)
272 gettext.textdomain('gettext')
273 # For convenience
274 self._ = gettext.gettext
275
276 def test_bindtextdomain(self):
277 self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir)
278
279 def test_textdomain(self):
280 self.assertEqual(gettext.textdomain(), 'gettext')
281
282 def test_bad_magic_number(self):
283 with open(MOFILE_BAD_MAGIC_NUMBER, 'rb') as fp:
284 with self.assertRaises(OSError) as cm:
285 gettext.GNUTranslations(fp)
286
287 exception = cm.exception
288 self.assertEqual(exception.errno, 0)
289 self.assertEqual(exception.strerror, "Bad magic number")
290 self.assertEqual(exception.filename, MOFILE_BAD_MAGIC_NUMBER)
291
292 def test_bad_major_version(self):
293 with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
294 with self.assertRaises(OSError) as cm:
295 gettext.GNUTranslations(fp)
296
297 exception = cm.exception
298 self.assertEqual(exception.errno, 0)
299 self.assertEqual(exception.strerror, "Bad version number 5")
300 self.assertEqual(exception.filename, MOFILE_BAD_MAJOR_VERSION)
301
302 def test_bad_minor_version(self):
303 with open(MOFILE_BAD_MINOR_VERSION, 'rb') as fp:
304 # Check that no error is thrown with a bad minor version number
305 gettext.GNUTranslations(fp)
306
307 def test_corrupt_file(self):
308 with open(MOFILE_CORRUPT, 'rb') as fp:
309 with self.assertRaises(OSError) as cm:
310 gettext.GNUTranslations(fp)
311
312 exception = cm.exception
313 self.assertEqual(exception.errno, 0)
314 self.assertEqual(exception.strerror, "File is corrupt")
315 self.assertEqual(exception.filename, MOFILE_CORRUPT)
316
317 def test_big_endian_file(self):
318 with open(MOFILE_BIG_ENDIAN, 'rb') as fp:
319 t = gettext.GNUTranslations(fp)
320
321 self.assertEqual(t.gettext('foo'), 'bar')
322
323 def test_some_translations(self):
324 eq = self.assertEqual
325 # test some translations
326 eq(self._('albatross'), 'albatross')
327 eq(self._('mullusk'), 'bacon')
328 eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
329 eq(self._(r'nudge nudge'), 'wink wink')
330
331 def test_some_translations_with_context(self):
332 eq = self.assertEqual
333 eq(gettext.pgettext('my context', 'nudge nudge'),
334 'wink wink (in "my context")')
335 eq(gettext.pgettext('my other context', 'nudge nudge'),
336 'wink wink (in "my other context")')
337
338 def test_some_translations_with_context_and_domain(self):
339 eq = self.assertEqual
340 eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'),
341 'wink wink (in "my context")')
342 eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'),
343 'wink wink (in "my other context")')
344
345 def test_multiline_strings(self):
346 eq = self.assertEqual
347 # multiline strings
348 eq(self._('''This module provides internationalization and localization
349 support for your Python programs by providing an interface to the GNU
350 gettext message catalog library.'''),
351 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
352 fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
353 trggrkg zrffntr pngnybt yvoenel.''')
354
355
356 class PluralFormsTests:
357
358 def _test_plural_forms(self, ngettext, gettext,
359 singular, plural, tsingular, tplural,
360 numbers_only=True):
361 x = ngettext(singular, plural, 1)
362 self.assertEqual(x, tsingular)
363 x = ngettext(singular, plural, 2)
364 self.assertEqual(x, tplural)
365 x = gettext(singular)
366 self.assertEqual(x, tsingular)
367
368 lineno = self._test_plural_forms.__code__.co_firstlineno + 12
369 with self.assertWarns(DeprecationWarning) as cm:
370 x = ngettext(singular, plural, 1.0)
371 self.assertEqual(cm.filename, __file__)
372 self.assertEqual(cm.lineno, lineno)
373 self.assertEqual(x, tsingular)
374 with self.assertWarns(DeprecationWarning) as cm:
375 x = ngettext(singular, plural, 1.1)
376 self.assertEqual(cm.filename, __file__)
377 self.assertEqual(cm.lineno, lineno + 5)
378 self.assertEqual(x, tplural)
379
380 if numbers_only:
381 with self.assertRaises(TypeError):
382 ngettext(singular, plural, None)
383 else:
384 with self.assertWarns(DeprecationWarning) as cm:
385 x = ngettext(singular, plural, None)
386 self.assertEqual(x, tplural)
387
388 def test_plural_forms(self):
389 self._test_plural_forms(
390 self.ngettext, self.gettext,
391 'There is %s file', 'There are %s files',
392 'Hay %s fichero', 'Hay %s ficheros')
393 self._test_plural_forms(
394 self.ngettext, self.gettext,
395 '%d file deleted', '%d files deleted',
396 '%d file deleted', '%d files deleted')
397
398 def test_plural_context_forms(self):
399 ngettext = partial(self.npgettext, 'With context')
400 gettext = partial(self.pgettext, 'With context')
401 self._test_plural_forms(
402 ngettext, gettext,
403 'There is %s file', 'There are %s files',
404 'Hay %s fichero (context)', 'Hay %s ficheros (context)')
405 self._test_plural_forms(
406 ngettext, gettext,
407 '%d file deleted', '%d files deleted',
408 '%d file deleted', '%d files deleted')
409
410 def test_plural_wrong_context_forms(self):
411 self._test_plural_forms(
412 partial(self.npgettext, 'Unknown context'),
413 partial(self.pgettext, 'Unknown context'),
414 'There is %s file', 'There are %s files',
415 'There is %s file', 'There are %s files')
416
417
418 class GNUTranslationsPluralFormsTestCase(PluralFormsTests, GettextBaseTest):
419 def setUp(self):
420 GettextBaseTest.setUp(self)
421 # Set up the bindings
422 gettext.bindtextdomain('gettext', os.curdir)
423 gettext.textdomain('gettext')
424
425 self.gettext = gettext.gettext
426 self.ngettext = gettext.ngettext
427 self.pgettext = gettext.pgettext
428 self.npgettext = gettext.npgettext
429
430
431 class GNUTranslationsWithDomainPluralFormsTestCase(PluralFormsTests, GettextBaseTest):
432 def setUp(self):
433 GettextBaseTest.setUp(self)
434 # Set up the bindings
435 gettext.bindtextdomain('gettext', os.curdir)
436
437 self.gettext = partial(gettext.dgettext, 'gettext')
438 self.ngettext = partial(gettext.dngettext, 'gettext')
439 self.pgettext = partial(gettext.dpgettext, 'gettext')
440 self.npgettext = partial(gettext.dnpgettext, 'gettext')
441
442 def test_plural_forms_wrong_domain(self):
443 self._test_plural_forms(
444 partial(gettext.dngettext, 'unknown'),
445 partial(gettext.dgettext, 'unknown'),
446 'There is %s file', 'There are %s files',
447 'There is %s file', 'There are %s files',
448 numbers_only=False)
449
450 def test_plural_context_forms_wrong_domain(self):
451 self._test_plural_forms(
452 partial(gettext.dnpgettext, 'unknown', 'With context'),
453 partial(gettext.dpgettext, 'unknown', 'With context'),
454 'There is %s file', 'There are %s files',
455 'There is %s file', 'There are %s files',
456 numbers_only=False)
457
458
459 class GNUTranslationsClassPluralFormsTestCase(PluralFormsTests, GettextBaseTest):
460 def setUp(self):
461 GettextBaseTest.setUp(self)
462 with open(MOFILE, 'rb') as fp:
463 t = gettext.GNUTranslations(fp)
464
465 self.gettext = t.gettext
466 self.ngettext = t.ngettext
467 self.pgettext = t.pgettext
468 self.npgettext = t.npgettext
469
470 def test_plural_forms_null_translations(self):
471 t = gettext.NullTranslations()
472 self._test_plural_forms(
473 t.ngettext, t.gettext,
474 'There is %s file', 'There are %s files',
475 'There is %s file', 'There are %s files',
476 numbers_only=False)
477
478 def test_plural_context_forms_null_translations(self):
479 t = gettext.NullTranslations()
480 self._test_plural_forms(
481 partial(t.npgettext, 'With context'),
482 partial(t.pgettext, 'With context'),
483 'There is %s file', 'There are %s files',
484 'There is %s file', 'There are %s files',
485 numbers_only=False)
486
487
488 class PluralFormsInternalTestCase(unittest.TestCase):
489 # Examples from http://www.gnu.org/software/gettext/manual/gettext.html
490
491 def test_ja(self):
492 eq = self.assertEqual
493 f = gettext.c2py('0')
494 s = ''.join([ str(f(x)) for x in range(200) ])
495 eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
496
497 def test_de(self):
498 eq = self.assertEqual
499 f = gettext.c2py('n != 1')
500 s = ''.join([ str(f(x)) for x in range(200) ])
501 eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
502
503 def test_fr(self):
504 eq = self.assertEqual
505 f = gettext.c2py('n>1')
506 s = ''.join([ str(f(x)) for x in range(200) ])
507 eq(s, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
508
509 def test_lv(self):
510 eq = self.assertEqual
511 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2')
512 s = ''.join([ str(f(x)) for x in range(200) ])
513 eq(s, "20111111111111111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111111111111110111111111011111111101111111110111111111011111111101111111110111111111011111111")
514
515 def test_gd(self):
516 eq = self.assertEqual
517 f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
518 s = ''.join([ str(f(x)) for x in range(200) ])
519 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
520
521 def test_gd2(self):
522 eq = self.assertEqual
523 # Tests the combination of parentheses and "?:"
524 f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
525 s = ''.join([ str(f(x)) for x in range(200) ])
526 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
527
528 def test_ro(self):
529 eq = self.assertEqual
530 f = gettext.c2py('n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2')
531 s = ''.join([ str(f(x)) for x in range(200) ])
532 eq(s, "10111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222111111111111111111122222222222222222222222222222222222222222222222222222222222222222222222222222222")
533
534 def test_lt(self):
535 eq = self.assertEqual
536 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
537 s = ''.join([ str(f(x)) for x in range(200) ])
538 eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111")
539
540 def test_ru(self):
541 eq = self.assertEqual
542 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
543 s = ''.join([ str(f(x)) for x in range(200) ])
544 eq(s, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222")
545
546 def test_cs(self):
547 eq = self.assertEqual
548 f = gettext.c2py('(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2')
549 s = ''.join([ str(f(x)) for x in range(200) ])
550 eq(s, "20111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
551
552 def test_pl(self):
553 eq = self.assertEqual
554 f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
555 s = ''.join([ str(f(x)) for x in range(200) ])
556 eq(s, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222")
557
558 def test_sl(self):
559 eq = self.assertEqual
560 f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
561 s = ''.join([ str(f(x)) for x in range(200) ])
562 eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
563
564 def test_ar(self):
565 eq = self.assertEqual
566 f = gettext.c2py('n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5')
567 s = ''.join([ str(f(x)) for x in range(200) ])
568 eq(s, "01233333333444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445553333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
569
570 @support.skip_wasi_stack_overflow()
571 def test_security(self):
572 raises = self.assertRaises
573 # Test for a dangerous expression
574 raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)")
575 # issue28563
576 raises(ValueError, gettext.c2py, '"(eval(foo) && ""')
577 raises(ValueError, gettext.c2py, 'f"{os.system(\'sh\')}"')
578 # Maximum recursion depth exceeded during compilation
579 raises(ValueError, gettext.c2py, 'n+'*10000 + 'n')
580 self.assertEqual(gettext.c2py('n+'*100 + 'n')(1), 101)
581 # MemoryError during compilation
582 raises(ValueError, gettext.c2py, '('*100 + 'n' + ')'*100)
583 # Maximum recursion depth exceeded in C to Python translator
584 raises(ValueError, gettext.c2py, '('*10000 + 'n' + ')'*10000)
585 self.assertEqual(gettext.c2py('('*20 + 'n' + ')'*20)(1), 1)
586
587 def test_chained_comparison(self):
588 # C doesn't chain comparison as Python so 2 == 2 == 2 gets different results
589 f = gettext.c2py('n == n == n')
590 self.assertEqual(''.join(str(f(x)) for x in range(3)), '010')
591 f = gettext.c2py('1 < n == n')
592 self.assertEqual(''.join(str(f(x)) for x in range(3)), '100')
593 f = gettext.c2py('n == n < 2')
594 self.assertEqual(''.join(str(f(x)) for x in range(3)), '010')
595 f = gettext.c2py('0 < n < 2')
596 self.assertEqual(''.join(str(f(x)) for x in range(3)), '111')
597
598 def test_decimal_number(self):
599 self.assertEqual(gettext.c2py('0123')(1), 123)
600
601 def test_invalid_syntax(self):
602 invalid_expressions = [
603 'x>1', '(n>1', 'n>1)', '42**42**42', '0xa', '1.0', '1e2',
604 'n>0x1', '+n', '-n', 'n()', 'n(1)', '1+', 'nn', 'n n', 'n ? 1 2'
605 ]
606 for expr in invalid_expressions:
607 with self.assertRaises(ValueError):
608 gettext.c2py(expr)
609
610 def test_negation(self):
611 f = gettext.c2py('!!!n')
612 self.assertEqual(f(0), 1)
613 self.assertEqual(f(1), 0)
614 self.assertEqual(f(2), 0)
615
616 def test_nested_condition_operator(self):
617 self.assertEqual(gettext.c2py('n?1?2:3:4')(0), 4)
618 self.assertEqual(gettext.c2py('n?1?2:3:4')(1), 2)
619 self.assertEqual(gettext.c2py('n?1:3?4:5')(0), 4)
620 self.assertEqual(gettext.c2py('n?1:3?4:5')(1), 1)
621
622 def test_division(self):
623 f = gettext.c2py('2/n*3')
624 self.assertEqual(f(1), 6)
625 self.assertEqual(f(2), 3)
626 self.assertEqual(f(3), 0)
627 self.assertEqual(f(-1), -6)
628 self.assertRaises(ZeroDivisionError, f, 0)
629
630 def test_plural_number(self):
631 f = gettext.c2py('n != 1')
632 self.assertEqual(f(1), 0)
633 self.assertEqual(f(2), 1)
634 with self.assertWarns(DeprecationWarning):
635 self.assertEqual(f(1.0), 0)
636 with self.assertWarns(DeprecationWarning):
637 self.assertEqual(f(2.0), 1)
638 with self.assertWarns(DeprecationWarning):
639 self.assertEqual(f(1.1), 1)
640 self.assertRaises(TypeError, f, '2')
641 self.assertRaises(TypeError, f, b'2')
642 self.assertRaises(TypeError, f, [])
643 self.assertRaises(TypeError, f, object())
644
645
646 class GNUTranslationParsingTest(GettextBaseTest):
647 def test_plural_form_error_issue17898(self):
648 with open(MOFILE, 'wb') as fp:
649 fp.write(base64.decodebytes(GNU_MO_DATA_ISSUE_17898))
650 with open(MOFILE, 'rb') as fp:
651 # If this runs cleanly, the bug is fixed.
652 t = gettext.GNUTranslations(fp)
653
654 def test_ignore_comments_in_headers_issue36239(self):
655 """Checks that comments like:
656
657 #-#-#-#-# messages.po (EdX Studio) #-#-#-#-#
658
659 are ignored.
660 """
661 with open(MOFILE, 'wb') as fp:
662 fp.write(base64.decodebytes(GNU_MO_DATA_ISSUE_17898))
663 with open(MOFILE, 'rb') as fp:
664 t = gettext.GNUTranslations(fp)
665 self.assertEqual(t.info()["plural-forms"], "nplurals=2; plural=(n != 1);")
666
667
668 class UnicodeTranslationsTest(GettextBaseTest):
669 def setUp(self):
670 GettextBaseTest.setUp(self)
671 with open(UMOFILE, 'rb') as fp:
672 self.t = gettext.GNUTranslations(fp)
673 self._ = self.t.gettext
674 self.pgettext = self.t.pgettext
675
676 def test_unicode_msgid(self):
677 self.assertIsInstance(self._(''), str)
678
679 def test_unicode_msgstr(self):
680 self.assertEqual(self._('ab\xde'), '\xa4yz')
681
682 def test_unicode_context_msgstr(self):
683 t = self.pgettext('mycontext\xde', 'ab\xde')
684 self.assertTrue(isinstance(t, str))
685 self.assertEqual(t, '\xa4yz (context version)')
686
687
688 class UnicodeTranslationsPluralTest(GettextBaseTest):
689 def setUp(self):
690 GettextBaseTest.setUp(self)
691 with open(MOFILE, 'rb') as fp:
692 self.t = gettext.GNUTranslations(fp)
693 self.ngettext = self.t.ngettext
694 self.npgettext = self.t.npgettext
695
696 def test_unicode_msgid(self):
697 unless = self.assertTrue
698 unless(isinstance(self.ngettext('', '', 1), str))
699 unless(isinstance(self.ngettext('', '', 2), str))
700
701 def test_unicode_context_msgid(self):
702 unless = self.assertTrue
703 unless(isinstance(self.npgettext('', '', '', 1), str))
704 unless(isinstance(self.npgettext('', '', '', 2), str))
705
706 def test_unicode_msgstr(self):
707 eq = self.assertEqual
708 unless = self.assertTrue
709 t = self.ngettext("There is %s file", "There are %s files", 1)
710 unless(isinstance(t, str))
711 eq(t, "Hay %s fichero")
712 unless(isinstance(t, str))
713 t = self.ngettext("There is %s file", "There are %s files", 5)
714 unless(isinstance(t, str))
715 eq(t, "Hay %s ficheros")
716
717 def test_unicode_msgstr_with_context(self):
718 eq = self.assertEqual
719 unless = self.assertTrue
720 t = self.npgettext("With context",
721 "There is %s file", "There are %s files", 1)
722 unless(isinstance(t, str))
723 eq(t, "Hay %s fichero (context)")
724 t = self.npgettext("With context",
725 "There is %s file", "There are %s files", 5)
726 unless(isinstance(t, str))
727 eq(t, "Hay %s ficheros (context)")
728
729
730 class WeirdMetadataTest(GettextBaseTest):
731 def setUp(self):
732 GettextBaseTest.setUp(self)
733 with open(MMOFILE, 'rb') as fp:
734 try:
735 self.t = gettext.GNUTranslations(fp)
736 except:
737 self.tearDown()
738 raise
739
740 def test_weird_metadata(self):
741 info = self.t.info()
742 self.assertEqual(len(info), 9)
743 self.assertEqual(info['last-translator'],
744 'John Doe <[email protected]>\nJane Foobar <[email protected]>')
745
746
747 class DummyGNUTranslations(gettext.GNUTranslations):
748 def foo(self):
749 return 'foo'
750
751
752 class GettextCacheTestCase(GettextBaseTest):
753 def test_cache(self):
754 self.localedir = os.curdir
755 self.mofile = MOFILE
756
757 self.assertEqual(len(gettext._translations), 0)
758
759 t = gettext.translation('gettext', self.localedir)
760
761 self.assertEqual(len(gettext._translations), 1)
762
763 t = gettext.translation('gettext', self.localedir,
764 class_=DummyGNUTranslations)
765
766 self.assertEqual(len(gettext._translations), 2)
767 self.assertEqual(t.__class__, DummyGNUTranslations)
768
769 # Calling it again doesn't add to the cache
770
771 t = gettext.translation('gettext', self.localedir,
772 class_=DummyGNUTranslations)
773
774 self.assertEqual(len(gettext._translations), 2)
775 self.assertEqual(t.__class__, DummyGNUTranslations)
776
777
778 class FallbackTranslations(gettext.NullTranslations):
779 def gettext(self, message):
780 return f'gettext: {message}'
781
782 def ngettext(self, msgid1, msgid2, n):
783 return f'ngettext: {msgid1}, {msgid2}, {n}'
784
785 def pgettext(self, context, message):
786 return f'pgettext: {context}, {message}'
787
788 def npgettext(self, context, msgid1, msgid2, n):
789 return f'npgettext: {context}, {msgid1}, {msgid2}, {n}'
790
791
792 class FallbackTestCase(GettextBaseTest):
793 def test_null_translations_fallback(self):
794 t = gettext.NullTranslations()
795 t.add_fallback(FallbackTranslations())
796 self.assertEqual(t.gettext('foo'), 'gettext: foo')
797 self.assertEqual(t.ngettext('foo', 'foos', 1),
798 'ngettext: foo, foos, 1')
799 self.assertEqual(t.pgettext('context', 'foo'),
800 'pgettext: context, foo')
801 self.assertEqual(t.npgettext('context', 'foo', 'foos', 1),
802 'npgettext: context, foo, foos, 1')
803
804 def test_gnu_translations_fallback(self):
805 with open(MOFILE, 'rb') as fp:
806 t = gettext.GNUTranslations(fp)
807 t.add_fallback(FallbackTranslations())
808 self.assertEqual(t.gettext('foo'), 'gettext: foo')
809 self.assertEqual(t.ngettext('foo', 'foos', 1),
810 'ngettext: foo, foos, 1')
811 self.assertEqual(t.pgettext('context', 'foo'),
812 'pgettext: context, foo')
813 self.assertEqual(t.npgettext('context', 'foo', 'foos', 1),
814 'npgettext: context, foo, foos, 1')
815
816 def test_nested_fallbacks(self):
817 class NestedFallback(gettext.NullTranslations):
818 def gettext(self, message):
819 if message == 'foo':
820 return 'fallback'
821 return super().gettext(message)
822
823 fallback1 = NestedFallback()
824 fallback2 = FallbackTranslations()
825 t = gettext.NullTranslations()
826 t.add_fallback(fallback1)
827 t.add_fallback(fallback2)
828
829 self.assertEqual(fallback1.gettext('bar'), 'gettext: bar')
830 self.assertEqual(t.gettext('foo'), 'fallback')
831 self.assertEqual(t.gettext('bar'), 'gettext: bar')
832
833
834 class ExpandLangTestCase(unittest.TestCase):
835 def test_expand_lang(self):
836 # Test all combinations of territory, charset and
837 # modifier (locale extension)
838 locales = {
839 'cs': ['cs'],
840 'cs_CZ': ['cs_CZ', 'cs'],
841 'cs.ISO8859-2': ['cs.ISO8859-2', 'cs'],
842 'cs@euro': ['cs@euro', 'cs'],
843 'cs_CZ.ISO8859-2': ['cs_CZ.ISO8859-2', 'cs_CZ', 'cs.ISO8859-2',
844 'cs'],
845 'cs_CZ@euro': ['cs_CZ@euro', 'cs@euro', 'cs_CZ', 'cs'],
846 'cs.ISO8859-2@euro': ['cs.ISO8859-2@euro', 'cs@euro',
847 'cs.ISO8859-2', 'cs'],
848 'cs_CZ.ISO8859-2@euro': ['cs_CZ.ISO8859-2@euro', 'cs_CZ@euro',
849 'cs.ISO8859-2@euro', 'cs@euro',
850 'cs_CZ.ISO8859-2', 'cs_CZ',
851 'cs.ISO8859-2', 'cs'],
852 }
853 for locale, expanded in locales.items():
854 with self.subTest(locale=locale):
855 with unittest.mock.patch("locale.normalize",
856 return_value=locale):
857 self.assertEqual(gettext._expand_lang(locale), expanded)
858
859
860 class FindTestCase(unittest.TestCase):
861
862 def setUp(self):
863 self.env = self.enterContext(os_helper.EnvironmentVarGuard())
864 self.tempdir = self.enterContext(os_helper.temp_cwd())
865
866 for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
867 self.env.unset(key)
868
869 def create_mo_file(self, lang):
870 locale_dir = os.path.join(self.tempdir, "locale")
871 mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
872 os.makedirs(mofile_dir)
873 mo_file = os.path.join(mofile_dir, "mofile.mo")
874 with open(mo_file, "wb") as f:
875 f.write(GNU_MO_DATA)
876 return mo_file
877
878 def test_find_with_env_vars(self):
879 # test that find correctly finds the environment variables
880 # when languages are not supplied
881 mo_file = self.create_mo_file("ga_IE")
882 for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
883 self.env.set(var, 'ga_IE')
884 result = gettext.find("mofile",
885 localedir=os.path.join(self.tempdir, "locale"))
886 self.assertEqual(result, mo_file)
887 self.env.unset(var)
888
889 def test_find_with_languages(self):
890 # test that passed languages are used
891 self.env.set('LANGUAGE', 'pt_BR')
892 mo_file = self.create_mo_file("ga_IE")
893
894 result = gettext.find("mofile",
895 localedir=os.path.join(self.tempdir, "locale"),
896 languages=['ga_IE'])
897 self.assertEqual(result, mo_file)
898
899 @unittest.mock.patch('gettext._expand_lang')
900 def test_find_with_no_lang(self, patch_expand_lang):
901 # no language can be found
902 gettext.find('foo')
903 patch_expand_lang.assert_called_with('C')
904
905 @unittest.mock.patch('gettext._expand_lang')
906 def test_find_with_c(self, patch_expand_lang):
907 # 'C' is already in languages
908 self.env.set('LANGUAGE', 'C')
909 gettext.find('foo')
910 patch_expand_lang.assert_called_with('C')
911
912 def test_find_all(self):
913 # test that all are returned when all is set
914 paths = []
915 for lang in ["ga_IE", "es_ES"]:
916 paths.append(self.create_mo_file(lang))
917 result = gettext.find('mofile',
918 localedir=os.path.join(self.tempdir, "locale"),
919 languages=["ga_IE", "es_ES"], all=True)
920 self.assertEqual(sorted(result), sorted(paths))
921
922 def test_find_deduplication(self):
923 # test that find removes duplicate languages
924 mo_file = [self.create_mo_file('ga_IE')]
925 result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale"),
926 languages=['ga_IE', 'ga_IE'], all=True)
927 self.assertEqual(result, mo_file)
928
929
930 class MiscTestCase(unittest.TestCase):
931 def test__all__(self):
932 support.check__all__(self, gettext,
933 not_exported={'c2py', 'ENOENT'})
934
935 @cpython_only
936 def test_lazy_import(self):
937 ensure_lazy_imports("gettext", {"re", "warnings", "locale"})
938
939
940 class TranslationFallbackTestCase(unittest.TestCase):
941 def test_translation_fallback(self):
942 with os_helper.temp_cwd() as tempdir:
943 t = gettext.translation('gettext', localedir=tempdir, fallback=True)
944 self.assertIsInstance(t, gettext.NullTranslations)
945
946
947 if __name__ == '__main__':
948 unittest.main()
949
950
951 # For reference, here's the .po file used to created the GNU_MO_DATA above.
952 #
953 # The original version was automatically generated from the sources with
954 # pygettext. Later it was manually modified to add plural forms support.
955
956 b'''
957 # Dummy translation for the Python test_gettext.py module.
958 # Copyright (C) 2001 Python Software Foundation
959 # Barry Warsaw <[email protected]>, 2000.
960 #
961 msgid ""
962 msgstr ""
963 "Project-Id-Version: 2.0\n"
964 "PO-Revision-Date: 2003-04-11 14:32-0400\n"
965 "Last-Translator: J. David Ibanez <[email protected]>\n"
966 "Language-Team: XX <[email protected]>\n"
967 "MIME-Version: 1.0\n"
968 "Content-Type: text/plain; charset=iso-8859-1\n"
969 "Content-Transfer-Encoding: 8bit\n"
970 "Generated-By: pygettext.py 1.1\n"
971 "Plural-Forms: nplurals=2; plural=n!=1;\n"
972
973 #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
974 #: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
975 #: test_gettext.py:98
976 msgid "nudge nudge"
977 msgstr "wink wink"
978
979 msgctxt "my context"
980 msgid "nudge nudge"
981 msgstr "wink wink (in \"my context\")"
982
983 msgctxt "my other context"
984 msgid "nudge nudge"
985 msgstr "wink wink (in \"my other context\")"
986
987 #: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
988 #: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
989 msgid "albatross"
990 msgstr ""
991
992 #: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
993 #: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
994 msgid "Raymond Luxury Yach-t"
995 msgstr "Throatwobbler Mangrove"
996
997 #: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
998 #: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
999 #: test_gettext.py:96
1000 msgid "mullusk"
1001 msgstr "bacon"
1002
1003 #: test_gettext.py:40 test_gettext.py:101
1004 msgid ""
1005 "This module provides internationalization and localization\n"
1006 "support for your Python programs by providing an interface to the GNU\n"
1007 "gettext message catalog library."
1008 msgstr ""
1009 "Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
1010 "fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
1011 "trggrkg zrffntr pngnybt yvoenel."
1012
1013 # Manually added, as neither pygettext nor xgettext support plural forms
1014 # in Python.
1015 msgid "There is %s file"
1016 msgid_plural "There are %s files"
1017 msgstr[0] "Hay %s fichero"
1018 msgstr[1] "Hay %s ficheros"
1019
1020 # Manually added, as neither pygettext nor xgettext support plural forms
1021 # and context in Python.
1022 msgctxt "With context"
1023 msgid "There is %s file"
1024 msgid_plural "There are %s files"
1025 msgstr[0] "Hay %s fichero (context)"
1026 msgstr[1] "Hay %s ficheros (context)"
1027 '''
1028
1029 # Here's the second example po file example, used to generate the UMO_DATA
1030 # containing utf-8 encoded Unicode strings
1031
1032 b'''
1033 # Dummy translation for the Python test_gettext.py module.
1034 # Copyright (C) 2001 Python Software Foundation
1035 # Barry Warsaw <[email protected]>, 2000.
1036 #
1037 msgid ""
1038 msgstr ""
1039 "Project-Id-Version: 2.0\n"
1040 "PO-Revision-Date: 2003-04-11 12:42-0400\n"
1041 "Last-Translator: Barry A. WArsaw <[email protected]>\n"
1042 "Language-Team: XX <[email protected]>\n"
1043 "MIME-Version: 1.0\n"
1044 "Content-Type: text/plain; charset=utf-8\n"
1045 "Content-Transfer-Encoding: 7bit\n"
1046 "Generated-By: manually\n"
1047
1048 #: nofile:0
1049 msgid "ab\xc3\x9e"
1050 msgstr "\xc2\xa4yz"
1051
1052 #: nofile:1
1053 msgctxt "mycontext\xc3\x9e"
1054 msgid "ab\xc3\x9e"
1055 msgstr "\xc2\xa4yz (context version)"
1056 '''
1057
1058 # Here's the third example po file, used to generate MMO_DATA
1059
1060 b'''
1061 msgid ""
1062 msgstr ""
1063 "Project-Id-Version: No Project 0.0\n"
1064 "POT-Creation-Date: Wed Dec 11 07:44:15 2002\n"
1065 "PO-Revision-Date: 2002-08-14 01:18:58+00:00\n"
1066 "Last-Translator: John Doe <[email protected]>\n"
1067 "Jane Foobar <[email protected]>\n"
1068 "Language-Team: xx <[email protected]>\n"
1069 "MIME-Version: 1.0\n"
1070 "Content-Type: text/plain; charset=iso-8859-15\n"
1071 "Content-Transfer-Encoding: quoted-printable\n"
1072 "Generated-By: pygettext.py 1.3\n"
1073 '''
1074
1075 #
1076 # messages.po, used for bug 17898
1077 #
1078
1079 b'''
1080 # test file for http://bugs.python.org/issue17898
1081 msgid ""
1082 msgstr ""
1083 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
1084 "#-#-#-#-# messages.po (EdX Studio) #-#-#-#-#\n"
1085 "Content-Type: text/plain; charset=UTF-8\n"
1086 '''