]> granicus.if.org Git - python/commitdiff
Fix interaction of custom translation classes and caching (#9042)
authorÉric Araujo <merwok@netwok.org>
Mon, 4 Oct 2010 23:52:37 +0000 (23:52 +0000)
committerÉric Araujo <merwok@netwok.org>
Mon, 4 Oct 2010 23:52:37 +0000 (23:52 +0000)
Lib/gettext.py
Lib/test/test_gettext.py
Misc/NEWS

index 4c957c0c6ad199a84e9ce46f39ea22643d7a2477..f9392d80d500a0304b875bc6fd22ae7fb1dd2ba6 100644 (file)
@@ -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:
index d667819ec2a079594be177ceaabf1c31fa8b8524..69ffcb7d296fbe3372dc0d2cd219c4a4f224f714 100644 (file)
@@ -335,6 +335,37 @@ class WeirdMetadataTest(GettextBaseTest):
            'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
 
 
+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__)
 
index 3dfff9b25e94802f41ed9378d3929a88958b950d..a0d30af7febdad120fdce1edfb8fe3ff89b4fdb5 100644 (file)
--- 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