[Bf-blender-cvs] [a40c256] temp-pyapi-units: Some minor tweaking on temps buffers...

Bastien Montagne noreply at git.blender.org
Mon Jun 16 20:25:51 CEST 2014


Commit: a40c2568ab607a7531633dbd6f7678778f1c2a0e
Author: Bastien Montagne
Date:   Mon Jun 16 20:17:22 2014 +0200
https://developer.blender.org/rBa40c2568ab607a7531633dbd6f7678778f1c2a0e

Some minor tweaking on temps buffers...

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

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

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

diff --git a/source/blender/python/intern/bpy_utils_units.c b/source/blender/python/intern/bpy_utils_units.c
index b78e035..3fef39c 100644
--- a/source/blender/python/intern/bpy_utils_units.c
+++ b/source/blender/python/intern/bpy_utils_units.c
@@ -267,7 +267,6 @@ static PyObject *utils_units_to_string(BlenderUtilsUnits *UNUSED(self), PyObject
 	double value = 0.0;
 	int precision = 3, split_unit = false, compatible_unit = false;
 
-	char buf1[512], buf2[512], *str;
 	int usys, ucat;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kw, "ssd|ipp:bpy.utils.units.to_string", (char **)kwlist,
@@ -280,17 +279,37 @@ static PyObject *utils_units_to_string(BlenderUtilsUnits *UNUSED(self), PyObject
 		return NULL;
 	}
 
-	bUnit_AsString(buf1, sizeof(buf1), value, precision, usys, ucat, (bool)split_unit, false);
+	{
+		/* Maximum expected length of string result:
+		 *     * number itself: precision + decimal dot + up to four 'above dot' digits.
+		 *     * unit: up to ten chars (six currently, let's be conservative, also because we use some utf8 chars).
+		 * This can be repeated twice (e.g. 1m20cm), and we add ten more spare chars (spaces, trailing '\0'...).
+		 */
+		const size_t buflen = (precision + 5 + 10) * 2 + 10;
+		char *buf1 = MEM_mallocN(sizeof(char) * buflen, __func__), *buf2 = NULL, *str;
+		PyObject *result;
+
+		bUnit_AsString(buf1, buflen, value, precision, usys, ucat, (bool)split_unit, false);
+
+		if (compatible_unit) {
+			buf2 = MEM_mallocN(sizeof(char) * buflen, __func__);
+			bUnit_ToUnitAltName(buf2, buflen, buf1, usys, ucat);
+			str = buf2;
+		}
+		else {
+			str = buf1;
+		}
 
-	if (compatible_unit) {
-		bUnit_ToUnitAltName(buf2, sizeof(buf2), buf1, usys, ucat);
-		str = buf2;
-	}
-	else {
-		str = buf1;
-	}
+		/* Just for test, to remove before final commit! */
+		printf("%d for %d used...\n", (int)buflen, (int)strlen(str));
+
+		result = PyUnicode_FromString(str);
 
-	return PyUnicode_FromString(str);
+		MEM_SAFE_FREE(buf1);
+		MEM_SAFE_FREE(buf2);
+
+		return result;
+	}
 }
 
 static PyMethodDef utils_units_methods[] = {




More information about the Bf-blender-cvs mailing list