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!
This commit is contained in:
2016-03-01 11:40:23 +01:00
parent 8f14baae92
commit 0187a5f292

View File

@@ -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. */ /* Iterate over all translations of the found language dict, and populate our ghash cache. */
while (PyDict_Next(lang_dict, &ppos, &pykey, &trans)) { while (PyDict_Next(lang_dict, &ppos, &pykey, &trans)) {
GHashKey *key;
const char *msgctxt = NULL, *msgid = NULL; const char *msgctxt = NULL, *msgid = NULL;
bool invalid_key = false; bool invalid_key = false;
@@ -229,14 +228,11 @@ static void _build_translations_cache(PyObject *py_messages, const char *locale)
continue; continue;
} }
key = _ghashutil_keyalloc(msgctxt, msgid);
/* Do not overwrite existing keys! */ /* Do not overwrite existing keys! */
if (BLI_ghash_lookup(_translations_cache, (void *)key)) { if (BPY_app_translations_py_pgettext(msgctxt, msgid) == msgid) {
continue; 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! */ #define STATIC_LOCALE_SIZE 32 /* Should be more than enough! */
GHashKey *key; GHashKey key;
static char locale[STATIC_LOCALE_SIZE] = ""; static char locale[STATIC_LOCALE_SIZE] = "";
const char *tmp; 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! */ /* And now, simply create the key (context, messageid) and find it in the cached dict! */
key = _ghashutil_keyalloc(msgctxt, msgid); key.msgctxt = BLT_is_default_context(msgctxt) ? BLT_I18NCONTEXT_DEFAULT_BPYRNA : msgctxt;
key.msgid = msgid;
tmp = BLI_ghash_lookup(_translations_cache, key); tmp = BLI_ghash_lookup(_translations_cache, &key);
_ghashutil_keyfree((void *)key);
return tmp ? tmp : msgid; return tmp ? tmp : msgid;