Changeset 391 for python/trunk/Lib/test/test_gettext.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_gettext.py
r2 r391 59 59 UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo') 60 60 MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo') 61 try:62 LANG = os.environ['LANGUAGE']63 except:64 LANG = 'en'65 61 66 62 … … 69 65 if not os.path.isdir(LOCALEDIR): 70 66 os.makedirs(LOCALEDIR) 71 fp = open(MOFILE, 'wb')72 fp.write(base64.decodestring(GNU_MO_DATA))73 fp.close()74 fp = open(UMOFILE, 'wb')75 fp.write(base64.decodestring(UMO_DATA))76 fp.close()77 fp = open(MMOFILE, 'wb') 78 fp.write(base64.decodestring(MMO_DATA))79 fp.close()80 os.environ['LANGUAGE'] = 'xx'67 with open(MOFILE, 'wb') as fp: 68 fp.write(base64.decodestring(GNU_MO_DATA)) 69 with open(UMOFILE, 'wb') as fp: 70 fp.write(base64.decodestring(UMO_DATA)) 71 with open(MMOFILE, 'wb') as fp: 72 fp.write(base64.decodestring(MMO_DATA)) 73 74 self.env = test_support.EnvironmentVarGuard() 75 self.env['LANGUAGE'] = 'xx' 76 gettext._translations.clear() 81 77 82 78 def tearDown(self): 83 os.environ['LANGUAGE'] = LANG 79 self.env.__exit__() 80 del self.env 84 81 shutil.rmtree(os.path.split(LOCALEDIR)[0]) 85 82 … … 137 134 eq = self.assertEqual 138 135 # test the alternative interface 139 fp = open(self.mofile, 'rb') 140 t = gettext.GNUTranslations(fp) 141 fp.close() 136 with open(self.mofile, 'rb') as fp: 137 t = gettext.GNUTranslations(fp) 142 138 # Install the translation object 143 139 t.install() … … 229 225 def test_plural_forms2(self): 230 226 eq = self.assertEqual 231 fp = open(self.mofile, 'rb') 232 t = gettext.GNUTranslations(fp) 233 fp.close() 227 with open(self.mofile, 'rb') as fp: 228 t = gettext.GNUTranslations(fp) 234 229 x = t.ngettext('There is %s file', 'There are %s files', 1) 235 230 eq(x, 'Hay %s fichero') … … 301 296 def setUp(self): 302 297 GettextBaseTest.setUp(self) 303 fp = open(UMOFILE, 'rb') 304 try: 298 with open(UMOFILE, 'rb') as fp: 305 299 self.t = gettext.GNUTranslations(fp) 306 finally:307 fp.close()308 300 self._ = self.t.ugettext 309 301 310 302 def test_unicode_msgid(self): 311 unless = self. failUnless303 unless = self.assertTrue 312 304 unless(isinstance(self._(''), unicode)) 313 305 unless(isinstance(self._(u''), unicode)) … … 321 313 def setUp(self): 322 314 GettextBaseTest.setUp(self) 323 fp = open(MMOFILE, 'rb') 324 try: 315 with open(MMOFILE, 'rb') as fp: 325 316 try: 326 317 self.t = gettext.GNUTranslations(fp) … … 328 319 self.tearDown() 329 320 raise 330 finally:331 fp.close()332 321 333 322 def test_weird_metadata(self): … … 335 324 self.assertEqual(info['last-translator'], 336 325 'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>') 326 327 328 class DummyGNUTranslations(gettext.GNUTranslations): 329 def foo(self): 330 return 'foo' 331 332 333 class GettextCacheTestCase(GettextBaseTest): 334 def test_cache(self): 335 self.localedir = os.curdir 336 self.mofile = MOFILE 337 338 self.assertEqual(len(gettext._translations), 0) 339 340 t = gettext.translation('gettext', self.localedir) 341 342 self.assertEqual(len(gettext._translations), 1) 343 344 t = gettext.translation('gettext', self.localedir, 345 class_=DummyGNUTranslations) 346 347 self.assertEqual(len(gettext._translations), 2) 348 self.assertEqual(t.__class__, DummyGNUTranslations) 349 350 # Calling it again doesn't add to the cache 351 352 t = gettext.translation('gettext', self.localedir, 353 class_=DummyGNUTranslations) 354 355 self.assertEqual(len(gettext._translations), 2) 356 self.assertEqual(t.__class__, DummyGNUTranslations) 337 357 338 358
Note:
See TracChangeset
for help on using the changeset viewer.