RNA: strict naming for types in bpy.types

Blender scripts already do this, some addons will need updating.
This commit is contained in:
Campbell Barton 2017-09-01 00:58:18 +10:00
parent 217fddcb8e
commit 0bbae3f3f6
Notes: blender-bot 2023-02-14 19:29:55 +01:00
Referenced by issue #52599, Python API, changes to type registration in 2.8
Referenced by issue blender/blender-addons#54701, Material Library VX - Add Active Material to library disfunctional
3 changed files with 53 additions and 0 deletions

View File

@ -784,6 +784,7 @@ const struct ListBase *RNA_struct_type_functions(StructRNA *srna);
char *RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len);
bool RNA_struct_available_or_report(struct ReportList *reports, const char *identifier);
bool RNA_struct_bl_idname_ok_or_report(struct ReportList *reports, const char *identifier, const char *sep);
/* Properties
*

View File

@ -850,6 +850,46 @@ bool RNA_struct_available_or_report(ReportList *reports, const char *identifier)
}
}
bool RNA_struct_bl_idname_ok_or_report(ReportList *reports, const char *identifier, const char *sep)
{
const int len_sep = strlen(sep);
const int len_id = strlen(identifier);
const char *p = strstr(identifier, sep);
if (p == NULL || p == identifier || p + len_sep >= identifier + len_id) {
BKE_reportf(reports, RPT_ERROR, "'%s' doesn't contain '%s' with prefix & suffix", identifier, sep);
return false;
}
const char *c, *start, *end, *last;
start = identifier;
end = p;
last = end - 1;
for (c = start; c != end; c++) {
if (((*c >= 'A' && *c <= 'Z') ||
((c != start) && (*c >= '0' && *c <= '9')) ||
((c != start) && (c != last) && (*c == '_'))) == 0)
{
BKE_reportf(reports, RPT_ERROR, "'%s' doesn't have upper case alpha-numeric prefix", identifier);
return false;
}
}
start = p + len_sep;
end = identifier + len_id;
last = end - 1;
for (c = start; c != end; c++) {
if (((*c >= 'A' && *c <= 'Z') ||
(*c >= 'a' && *c <= 'z') ||
(*c >= '0' && *c <= '9') ||
((c != start) && (c != last) && (*c == '_'))) == 0)
{
BKE_reportf(reports, RPT_ERROR, "'%s' doesn't have an alpha-numeric suffix", identifier);
return false;
}
}
return true;
}
/* Property Information */
const char *RNA_property_identifier(PropertyRNA *prop)

View File

@ -232,6 +232,9 @@ static StructRNA *rna_Panel_register(Main *bmain, ReportList *reports, void *dat
if (!RNA_struct_available_or_report(reports, dummypt.idname)) {
return NULL;
}
if (!RNA_struct_bl_idname_ok_or_report(reports, dummypt.idname, "_PT_")) {
return NULL;
}
/* create a new panel type */
pt = MEM_callocN(sizeof(PanelType), "python buttons panel");
@ -497,6 +500,9 @@ static StructRNA *rna_UIList_register(Main *bmain, ReportList *reports, void *da
if (!RNA_struct_available_or_report(reports, dummyult.idname)) {
return NULL;
}
if (!RNA_struct_bl_idname_ok_or_report(reports, dummyult.idname, "_UL_")) {
return NULL;
}
/* create a new menu type */
ult = MEM_callocN(sizeof(uiListType) + over_alloc, "python uilist");
@ -602,6 +608,9 @@ static StructRNA *rna_Header_register(Main *bmain, ReportList *reports, void *da
if (!RNA_struct_available_or_report(reports, dummyht.idname)) {
return NULL;
}
if (!RNA_struct_bl_idname_ok_or_report(reports, dummyht.idname, "_HT_")) {
return NULL;
}
/* create a new header type */
ht = MEM_callocN(sizeof(HeaderType), "python buttons header");
@ -728,6 +737,9 @@ static StructRNA *rna_Menu_register(Main *bmain, ReportList *reports, void *data
if (!RNA_struct_available_or_report(reports, dummymt.idname)) {
return NULL;
}
if (!RNA_struct_bl_idname_ok_or_report(reports, dummymt.idname, "_MT_")) {
return NULL;
}
/* create a new menu type */
if (_menu_descr[0]) {