]> granicus.if.org Git - python/commitdiff
translation(): Minor optimization patch which avoids instantiating the
authorBarry Warsaw <barry@python.org>
Thu, 5 Oct 2000 18:48:12 +0000 (18:48 +0000)
committerBarry Warsaw <barry@python.org>
Thu, 5 Oct 2000 18:48:12 +0000 (18:48 +0000)
default value's instance unless it's absolutely necessary.

Lib/gettext.py

index 724cecb4c00a8981aeb21e2004a70411d2dfcdb4..578490f68cc8452ba7f3c2509aeda7ffbf7327e5 100644 (file)
@@ -235,7 +235,11 @@ def translation(domain, localedir=None, languages=None, class_=None):
         raise IOError(ENOENT, 'No translation file found for domain', domain)
     key = os.path.abspath(mofile)
     # TBD: do we need to worry about the file pointer getting collected?
-    t = _translations.setdefault(key, class_(open(mofile, 'rb')))
+    # Avoid opening, reading, and parsing the .mo file after it's been done
+    # once.
+    t = _translations.get(key)
+    if t is None:
+        t = _translations.setdefault(key, class_(open(mofile, 'rb')))
     return t