[Bf-blender-cvs] [f711c44b8dc] blender2.8: PyAPI: Support for 'None' string args from Python

Campbell Barton noreply at git.blender.org
Tue Oct 30 06:21:05 CET 2018


Commit: f711c44b8dc8e10b37b387cefabb7236ae77decd
Author: Campbell Barton
Date:   Tue Oct 30 16:11:39 2018 +1100
Branches: blender2.8
https://developer.blender.org/rBf711c44b8dc8e10b37b387cefabb7236ae77decd

PyAPI: Support for 'None' string args from Python

This is needed because some RNA functions differentiate a NULL 'char *'
argument from an empty string.

Previously a NULL argument could be passed when the C definition
defined the default as NULL and the argument wasn't passed
which is a fairly hidden way of handling things.

Now strings use `PROP_NEVER_NULL` by default
which can be cleared for function arguments that allow None -> NULL.

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

M	source/blender/makesrna/intern/rna_define.c
M	source/blender/python/intern/bpy_rna.c

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

diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c
index 42db80c83c0..6724315f376 100644
--- a/source/blender/makesrna/intern/rna_define.c
+++ b/source/blender/makesrna/intern/rna_define.c
@@ -1165,7 +1165,8 @@ PropertyRNA *RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier
 		case PROP_STRING:
 		{
 			StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
-
+			/* By default don't allow NULL string args, callers may clear. */
+			RNA_def_property_flag(prop, PROP_NEVER_NULL);
 			sprop->defaultvalue = "";
 			break;
 		}
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index 32d8c01be76..b4020c9fc8f 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -1729,7 +1729,25 @@ static int pyrna_py_to_prop(
 				const int subtype = RNA_property_subtype(prop);
 				const char *param;
 
-				if (subtype == PROP_BYTESTRING) {
+				if (value == Py_None) {
+					if ((RNA_property_flag(prop) & PROP_NEVER_NULL) == 0) {
+						if (data) {
+							*((char **)data) = (char *)NULL;
+						}
+						else {
+							RNA_property_string_set(ptr, prop, NULL);
+						}
+					}
+					else {
+						PyC_Err_Format_Prefix(
+						        PyExc_TypeError,
+						        "%.200s %.200s.%.200s doesn't support None from string types",
+						        error_prefix, RNA_struct_identifier(ptr->type),
+						        RNA_property_identifier(prop));
+						return -1;
+					}
+				}
+				else if (subtype == PROP_BYTESTRING) {
 
 					/* Byte String */



More information about the Bf-blender-cvs mailing list