[Bf-blender-cvs] [0187a5f] master: Py (addons) i18n: fix memleak, enhance msgid lookup.

Bastien Montagne noreply at git.blender.org
Tue Mar 1 12:45:32 CET 2016


Commit: 0187a5f29223ddc813c84a7f97bc6d26f109552c
Author: Bastien Montagne
Date:   Tue Mar 1 11:40:23 2016 +0100
Branches: master
https://developer.blender.org/rB0187a5f29223ddc813c84a7f97bc6d26f109552c

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!

===================================================================

M	source/blender/python/intern/bpy_app_translations.c

===================================================================

diff --git a/source/blender/python/intern/bpy_app_translations.c b/source/blender/python/intern/bpy_app_translations.c
index dfff67e..b90deaf 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;




More information about the Bf-blender-cvs mailing list