RNA: support functions returning allocated strings

Now it's possible for a C/RNA function to return a dynamic string,
The PROP_NEVER_NULL flag is also supported so a NULL string returns
None in Python.
This commit is contained in:
2022-03-11 14:42:39 +11:00
parent f527d6582b
commit 1032f111d0
2 changed files with 44 additions and 13 deletions

View File

@@ -5898,26 +5898,36 @@ static PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *dat
case PROP_STRING: {
const char *data_ch;
const int subtype = RNA_property_subtype(prop);
size_t data_ch_len;
if (flag & PROP_THICK_WRAP) {
data_ch = (char *)data;
if (flag & PROP_DYNAMIC) {
ParameterDynAlloc *data_alloc = data;
data_ch = data_alloc->array;
data_ch_len = data_alloc->array_tot;
BLI_assert((data_ch == NULL) || strlen(data_ch) == data_ch_len);
}
else {
data_ch = *(char **)data;
data_ch = (flag & PROP_THICK_WRAP) ? (char *)data : *(char **)data;
data_ch_len = data_ch ? 0 : strlen(data_ch);
}
if (UNLIKELY(data_ch == NULL)) {
BLI_assert((flag & PROP_NEVER_NULL) == 0);
ret = Py_None;
Py_INCREF(ret);
}
#ifdef USE_STRING_COERCE
if (subtype == PROP_BYTESTRING) {
ret = PyBytes_FromString(data_ch);
else if (subtype == PROP_BYTESTRING) {
ret = PyBytes_FromStringAndSize(data_ch, data_ch_len);
}
else if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
ret = PyC_UnicodeFromByte(data_ch);
ret = PyC_UnicodeFromByteAndSize(data_ch, data_ch_len);
}
else {
ret = PyUnicode_FromString(data_ch);
ret = PyUnicode_FromStringAndSize(data_ch, data_ch_len);
}
#else
if (subtype == PROP_BYTESTRING) {
else if (subtype == PROP_BYTESTRING) {
ret = PyBytes_FromString(buf);
}
else {