From: Éric Araujo Date: Mon, 4 Oct 2010 23:52:37 +0000 (+0000) Subject: Fix interaction of custom translation classes and caching (#9042) X-Git-Tag: v3.2a3~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6108bf5ed002925f2092deb025f336d8c2024299;p=python Fix interaction of custom translation classes and caching (#9042) --- diff --git a/Lib/gettext.py b/Lib/gettext.py index 4c957c0c6a..f9392d80d5 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -419,7 +419,7 @@ def translation(domain, localedir=None, languages=None, # once. result = None for mofile in mofiles: - key = os.path.abspath(mofile) + key = (class_, os.path.abspath(mofile)) t = _translations.get(key) if t is None: with open(mofile, 'rb') as fp: diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index d667819ec2..69ffcb7d29 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -335,6 +335,37 @@ class WeirdMetadataTest(GettextBaseTest): 'John Doe \nJane Foobar ') +class DummyGNUTranslations(gettext.GNUTranslations): + def foo(self): + return 'foo' + + +class GettextCacheTestCase(GettextBaseTest): + def test_cache(self): + self.localedir = os.curdir + self.mofile = MOFILE + + self.assertEqual(len(gettext._translations), 0) + + t = gettext.translation('gettext', self.localedir) + + self.assertEqual(len(gettext._translations), 1) + + t = gettext.translation('gettext', self.localedir, + class_=DummyGNUTranslations) + + self.assertEqual(len(gettext._translations), 2) + self.assertEqual(t.__class__, DummyGNUTranslations) + + # Calling it again doesn't add to the cache + + t = gettext.translation('gettext', self.localedir, + class_=DummyGNUTranslations) + + self.assertEqual(len(gettext._translations), 2) + self.assertEqual(t.__class__, DummyGNUTranslations) + + def test_main(): support.run_unittest(__name__) diff --git a/Misc/NEWS b/Misc/NEWS index 3dfff9b25e..a0d30af7fe 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -88,6 +88,9 @@ Core and Builtins Library ------- +- Issue #9042: Fix interaction of custom translation classes and caching in + gettext. + - Issue 6706: asyncore.dispatcher now provides a handle_accepted() method returning a (sock, addr) pair which is called when a connection has been established with a new remote endpoint. This is supposed to be used as a