diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2016-03-01 13:40:23 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2016-03-01 13:44:10 +0300 |
commit | 0187a5f29223ddc813c84a7f97bc6d26f109552c (patch) | |
tree | 579b9d69bd1c53f60489ceec759dbbfd00bb0407 | |
parent | 8f14baae92b950b7cf9532231f31f4dec4b2b7b8 (diff) |
Py (addons) i18n: fix memleak, enhance msgid lookup.
Probably did not happen yet (since nobody uses addons translations...), but there was an
nice memleak during creation of translation ghash in case a same msgid/msgctx would be
added more than once.
Also, no need to allocate (and free) a temp key each time we lookup a msgid, we can use
given const strings directly here!
-rw-r--r-- | source/blender/python/intern/bpy_app_translations.c | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/source/blender/python/intern/bpy_app_translations.c b/source/blender/python/intern/bpy_app_translations.c index dfff67e6ca1..b90deafb377 100644 --- a/source/blender/python/intern/bpy_app_translations.c +++ b/source/blender/python/intern/bpy_app_translations.c @@ -181,7 +181,6 @@ static void _build_translations_cache(PyObject *py_messages, const char *locale) /* Iterate over all translations of the found language dict, and populate our ghash cache. */ while (PyDict_Next(lang_dict, &ppos, &pykey, &trans)) { - GHashKey *key; const char *msgctxt = NULL, *msgid = NULL; bool invalid_key = false; @@ -229,14 +228,11 @@ static void _build_translations_cache(PyObject *py_messages, const char *locale) continue; } - key = _ghashutil_keyalloc(msgctxt, msgid); - /* Do not overwrite existing keys! */ - if (BLI_ghash_lookup(_translations_cache, (void *)key)) { - continue; + if (BPY_app_translations_py_pgettext(msgctxt, msgid) == msgid) { + GHashKey *key = _ghashutil_keyalloc(msgctxt, msgid); + BLI_ghash_insert(_translations_cache, key, BLI_strdup(_PyUnicode_AsString(trans))); } - - BLI_ghash_insert(_translations_cache, key, BLI_strdup(_PyUnicode_AsString(trans))); } } } @@ -251,7 +247,7 @@ const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *ms { #define STATIC_LOCALE_SIZE 32 /* Should be more than enough! */ - GHashKey *key; + GHashKey key; static char locale[STATIC_LOCALE_SIZE] = ""; const char *tmp; @@ -275,11 +271,10 @@ const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *ms } /* And now, simply create the key (context, messageid) and find it in the cached dict! */ - key = _ghashutil_keyalloc(msgctxt, msgid); - - tmp = BLI_ghash_lookup(_translations_cache, key); + key.msgctxt = BLT_is_default_context(msgctxt) ? BLT_I18NCONTEXT_DEFAULT_BPYRNA : msgctxt; + key.msgid = msgid; - _ghashutil_keyfree((void *)key); + tmp = BLI_ghash_lookup(_translations_cache, &key); return tmp ? tmp : msgid; |