diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-03-16 04:18:56 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-03-16 07:17:45 +0300 |
commit | e125305af41a7360c52b9a38024b7e24fde06d70 (patch) | |
tree | 89670ef582473388c7c87fe42f4ae81f0a44132d /source/blender/makesrna | |
parent | be51d671b500e8b6881295778de7272a70504b71 (diff) |
Fix T86332: Error using lambda in annotations in Python 3.10
Callbacks used in `bpy.props` didn't hold a references to the functions
they used.
While this has been the case since early 2.5x it didn't cause any
problems as long as the class held a reference.
With Python 3.10 or when using `from __future__ import annotations`,
the annotations are no longer owned by the class once evaluated.
Resolve this by holding a reference in the module, which now supports
traverse & clear callbacks so the objects are visible to Python's
garbage collector.
Also refactor storage of Python data, moving from an array into a struct.
Diffstat (limited to 'source/blender/makesrna')
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 1 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_define.h | 5 | ||||
-rw-r--r-- | source/blender/makesrna/intern/makesrna.c | 2 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_access.c | 9 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_define.c | 23 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_internal_types.h | 1 |
6 files changed, 21 insertions, 20 deletions
diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 4c60ffe4f16..d41220eb22c 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -1021,7 +1021,6 @@ int RNA_property_string_default_length(PointerRNA *ptr, PropertyRNA *prop); int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop); void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value); int RNA_property_enum_get_default(PointerRNA *ptr, PropertyRNA *prop); -void *RNA_property_enum_py_data_get(PropertyRNA *prop); int RNA_property_enum_step( const struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int from_value, int step); diff --git a/source/blender/makesrna/RNA_define.h b/source/blender/makesrna/RNA_define.h index c49a52ceed7..bc1a8f52b8d 100644 --- a/source/blender/makesrna/RNA_define.h +++ b/source/blender/makesrna/RNA_define.h @@ -465,8 +465,6 @@ void RNA_def_property_string_funcs_runtime(PropertyRNA *prop, StringPropertyLengthFunc lengthfunc, StringPropertySetFunc setfunc); -void RNA_def_property_enum_py_data(PropertyRNA *prop, void *py_data); - void RNA_def_property_translation_context(PropertyRNA *prop, const char *context); /* Function */ @@ -507,6 +505,9 @@ void RNA_def_property_duplicate_pointers(StructOrFunctionRNA *cont_, PropertyRNA void RNA_def_property_free_pointers(PropertyRNA *prop); int RNA_def_property_free_identifier(StructOrFunctionRNA *cont_, const char *identifier); +void RNA_def_property_free_pointers_set_py_data_callback( + void (*py_data_clear_fn)(PropertyRNA *prop)); + /* utilities */ const char *RNA_property_typename(PropertyType type); #define IS_DNATYPE_FLOAT_COMPAT(_str) (strcmp(_str, "float") == 0 || strcmp(_str, "double") == 0) diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c index 1f887c2eec3..6940e475f39 100644 --- a/source/blender/makesrna/intern/makesrna.c +++ b/source/blender/makesrna/intern/makesrna.c @@ -3989,7 +3989,7 @@ static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, Pr case PROP_ENUM: { EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop; fprintf(f, - "\t%s, %s, %s, %s, %s, NULL, ", + "\t%s, %s, %s, %s, %s, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->item_fn), diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 8e5e70642cc..f94dc38ddfe 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -3598,15 +3598,6 @@ int RNA_property_enum_get_default(PointerRNA *UNUSED(ptr), PropertyRNA *prop) return eprop->defaultvalue; } -void *RNA_property_enum_py_data_get(PropertyRNA *prop) -{ - EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop; - - BLI_assert(RNA_property_type(prop) == PROP_ENUM); - - return eprop->py_data; -} - /** * Get the value of the item that is \a step items away from \a from_value. * diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c index 5e188285e39..019823a7de3 100644 --- a/source/blender/makesrna/intern/rna_define.c +++ b/source/blender/makesrna/intern/rna_define.c @@ -3305,12 +3305,6 @@ void RNA_def_property_enum_funcs_runtime(PropertyRNA *prop, } } -void RNA_def_property_enum_py_data(PropertyRNA *prop, void *py_data) -{ - EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop; - eprop->py_data = py_data; -} - void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, @@ -4632,11 +4626,28 @@ void RNA_def_property_duplicate_pointers(StructOrFunctionRNA *cont_, PropertyRNA prop->flag_internal |= PROP_INTERN_FREE_POINTERS; } +static void (*g_py_data_clear_fn)(PropertyRNA *prop) = NULL; + +/** + * Set the callback used to decrement the user count of a property. + * + * This function is called when freeing each dynamically defined property. + */ +void RNA_def_property_free_pointers_set_py_data_callback( + void (*py_data_clear_fn)(PropertyRNA *prop)) +{ + g_py_data_clear_fn = py_data_clear_fn; +} + void RNA_def_property_free_pointers(PropertyRNA *prop) { if (prop->flag_internal & PROP_INTERN_FREE_POINTERS) { int a; + if (g_py_data_clear_fn) { + g_py_data_clear_fn(prop); + } + if (prop->identifier) { MEM_freeN((void *)prop->identifier); } diff --git a/source/blender/makesrna/intern/rna_internal_types.h b/source/blender/makesrna/intern/rna_internal_types.h index 1dd08bb1074..0c0260c889c 100644 --- a/source/blender/makesrna/intern/rna_internal_types.h +++ b/source/blender/makesrna/intern/rna_internal_types.h @@ -457,7 +457,6 @@ typedef struct EnumPropertyRNA { PropEnumGetFuncEx get_ex; PropEnumSetFuncEx set_ex; - void *py_data; /* store py callback here */ const EnumPropertyItem *item; int totitem; |