Skip to content

Commit 6108bf5

Browse files
committed
Fix interaction of custom translation classes and caching (#9042)
1 parent 7017616 commit 6108bf5

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Lib/gettext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def translation(domain, localedir=None, languages=None,
419419
# once.
420420
result = None
421421
for mofile in mofiles:
422-
key = os.path.abspath(mofile)
422+
key = (class_, os.path.abspath(mofile))
423423
t = _translations.get(key)
424424
if t is None:
425425
with open(mofile, 'rb') as fp:

Lib/test/test_gettext.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,37 @@ def test_weird_metadata(self):
335335
'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
336336

337337

338+
class DummyGNUTranslations(gettext.GNUTranslations):
339+
def foo(self):
340+
return 'foo'
341+
342+
343+
class GettextCacheTestCase(GettextBaseTest):
344+
def test_cache(self):
345+
self.localedir = os.curdir
346+
self.mofile = MOFILE
347+
348+
self.assertEqual(len(gettext._translations), 0)
349+
350+
t = gettext.translation('gettext', self.localedir)
351+
352+
self.assertEqual(len(gettext._translations), 1)
353+
354+
t = gettext.translation('gettext', self.localedir,
355+
class_=DummyGNUTranslations)
356+
357+
self.assertEqual(len(gettext._translations), 2)
358+
self.assertEqual(t.__class__, DummyGNUTranslations)
359+
360+
# Calling it again doesn't add to the cache
361+
362+
t = gettext.translation('gettext', self.localedir,
363+
class_=DummyGNUTranslations)
364+
365+
self.assertEqual(len(gettext._translations), 2)
366+
self.assertEqual(t.__class__, DummyGNUTranslations)
367+
368+
338369
def test_main():
339370
support.run_unittest(__name__)
340371

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Core and Builtins
8888
Library
8989
-------
9090

91+
- Issue #9042: Fix interaction of custom translation classes and caching in
92+
gettext.
93+
9194
- Issue 6706: asyncore.dispatcher now provides a handle_accepted() method
9295
returning a (sock, addr) pair which is called when a connection has been
9396
established with a new remote endpoint. This is supposed to be used as a

0 commit comments

Comments
 (0)