Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_gettext.py

    r2 r391  
    5959UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
    6060MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
    61 try:
    62     LANG = os.environ['LANGUAGE']
    63 except:
    64     LANG = 'en'
    6561
    6662
     
    6965        if not os.path.isdir(LOCALEDIR):
    7066            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()
    8177
    8278    def tearDown(self):
    83         os.environ['LANGUAGE'] = LANG
     79        self.env.__exit__()
     80        del self.env
    8481        shutil.rmtree(os.path.split(LOCALEDIR)[0])
    8582
     
    137134        eq = self.assertEqual
    138135        # 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)
    142138        # Install the translation object
    143139        t.install()
     
    229225    def test_plural_forms2(self):
    230226        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)
    234229        x = t.ngettext('There is %s file', 'There are %s files', 1)
    235230        eq(x, 'Hay %s fichero')
     
    301296    def setUp(self):
    302297        GettextBaseTest.setUp(self)
    303         fp = open(UMOFILE, 'rb')
    304         try:
     298        with open(UMOFILE, 'rb') as fp:
    305299            self.t = gettext.GNUTranslations(fp)
    306         finally:
    307             fp.close()
    308300        self._ = self.t.ugettext
    309301
    310302    def test_unicode_msgid(self):
    311         unless = self.failUnless
     303        unless = self.assertTrue
    312304        unless(isinstance(self._(''), unicode))
    313305        unless(isinstance(self._(u''), unicode))
     
    321313    def setUp(self):
    322314        GettextBaseTest.setUp(self)
    323         fp = open(MMOFILE, 'rb')
    324         try:
     315        with open(MMOFILE, 'rb') as fp:
    325316            try:
    326317                self.t = gettext.GNUTranslations(fp)
     
    328319                self.tearDown()
    329320                raise
    330         finally:
    331             fp.close()
    332321
    333322    def test_weird_metadata(self):
     
    335324        self.assertEqual(info['last-translator'],
    336325           'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
     326
     327
     328class DummyGNUTranslations(gettext.GNUTranslations):
     329    def foo(self):
     330        return 'foo'
     331
     332
     333class 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)
    337357
    338358
Note: See TracChangeset for help on using the changeset viewer.