[Bf-blender-cvs] [de21f07] master: Generic check for string being a Py keyword

Campbell Barton noreply at git.blender.org
Fri Apr 1 00:46:10 CEST 2016


Commit: de21f07f6c192e8076ae091e8c73587411bda4fd
Author: Campbell Barton
Date:   Fri Apr 1 09:43:11 2016 +1100
Branches: master
https://developer.blender.org/rBde21f07f6c192e8076ae091e8c73587411bda4fd

Generic check for string being a Py keyword

Driver code used incomplete list of Py keywords

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

M	source/blender/blenkernel/intern/fcurve.c
M	source/blender/python/BPY_extern.h
M	source/blender/python/intern/bpy_interface.c

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

diff --git a/source/blender/blenkernel/intern/fcurve.c b/source/blender/blenkernel/intern/fcurve.c
index 845229e..29da218 100644
--- a/source/blender/blenkernel/intern/fcurve.c
+++ b/source/blender/blenkernel/intern/fcurve.c
@@ -1649,15 +1649,12 @@ void driver_variable_name_validate(DriverVar *dvar)
 	 * NOTE: These won't confuse Python, but it will be impossible to use the variable
 	 *       in an expression without Python misinterpreting what these are for
 	 */
-	if (STREQ(dvar->name, "if") || STREQ(dvar->name, "elif") || STREQ(dvar->name, "else") ||
-	    STREQ(dvar->name, "for") || STREQ(dvar->name, "while") || STREQ(dvar->name, "def") ||
-	    STREQ(dvar->name, "True") || STREQ(dvar->name, "False") || STREQ(dvar->name, "import") ||
-	    STREQ(dvar->name, "pass")  || STREQ(dvar->name, "with"))
-	{
+#ifdef WITH_PYTHON
+	if (BPY_string_is_keyword(dvar->name)) {
 		dvar->flag |= DVAR_FLAG_INVALID_PY_KEYWORD;
 	}
-	
-	
+#endif
+
 	/* If any these conditions match, the name is invalid */
 	if (dvar->flag & DVAR_ALL_INVALID_FLAGS)
 		dvar->flag |= DVAR_FLAG_INVALID_NAME;
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index d3be599..4006816 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -95,6 +95,8 @@ void	BPY_context_update(struct bContext *C);
 
 void	BPY_id_release(struct ID *id);
 
+bool	BPY_string_is_keyword(const char *str);
+
 /* I18n for addons */
 #ifdef WITH_INTERNATIONAL
 const char *BPY_app_translations_py_pgettext(const char *msgctxt, const char *msgid);
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 0c3048d..b559623 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -899,6 +899,32 @@ static void bpy_module_free(void *UNUSED(mod))
 
 #endif
 
+/**
+ * Avoids duplicating keyword list.
+ */
+bool BPY_string_is_keyword(const char *str)
+{
+	/* list is from...
+	* ", ".join(['"%s"' % kw for kw in  __import__("keyword").kwlist])
+	*/
+	const char *kwlist[] = {
+	    "False", "None", "True",
+	    "and", "as", "assert", "break",
+	    "class", "continue", "def", "del", "elif", "else", "except",
+	    "finally", "for", "from", "global", "if", "import", "in",
+	    "is", "lambda", "nonlocal", "not", "or", "pass", "raise",
+	    "return", "try", "while", "with", "yield", NULL,
+	};
+
+	for (int i = 0; kwlist[i]; i++) {
+		if (STREQ(str, kwlist[i])) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
 
 /* EVIL, define text.c functions here... */
 /* BKE_text.h */




More information about the Bf-blender-cvs mailing list