From 576d385ddb581190d21febfa724086797c47492a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 11 Dec 2019 18:04:44 +1100 Subject: PyAPI: add utility functions get the size from an evaluated string Allows including null bytes in the resulting string. --- source/blender/python/BPY_extern.h | 6 ++++++ source/blender/python/generic/py_capi_utils.c | 19 +++++++++++++++---- source/blender/python/generic/py_capi_utils.h | 5 +++++ source/blender/python/intern/bpy_interface.c | 18 +++++++++++++++--- 4 files changed, 41 insertions(+), 7 deletions(-) (limited to 'source') diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 11c06f6191e..be2fd6c4e53 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -85,6 +85,12 @@ bool BPY_execute_string_as_intptr(struct bContext *C, const char *expr, const bool verbose, intptr_t *r_value); +bool BPY_execute_string_as_string_and_size(struct bContext *C, + const char *imports[], + const char *expr, + const bool verbose, + char **r_value, + size_t *r_value_size); bool BPY_execute_string_as_string(struct bContext *C, const char *imports[], const char *expr, diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index 8470d1ccdf3..8ea627589f2 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -1257,10 +1257,11 @@ bool PyC_RunString_AsIntPtr(const char *imports[], return ok; } -bool PyC_RunString_AsString(const char *imports[], - const char *expr, - const char *filename, - char **r_value) +bool PyC_RunString_AsStringAndSize(const char *imports[], + const char *expr, + const char *filename, + char **r_value, + size_t *r_value_size) { PyObject *py_dict, *retval; bool ok = true; @@ -1288,6 +1289,7 @@ bool PyC_RunString_AsString(const char *imports[], char *val_alloc = MEM_mallocN(val_len + 1, __func__); memcpy(val_alloc, val, val_len + 1); *r_value = val_alloc; + *r_value_size = val_len; } Py_DECREF(retval); @@ -1298,6 +1300,15 @@ bool PyC_RunString_AsString(const char *imports[], return ok; } +bool PyC_RunString_AsString(const char *imports[], + const char *expr, + const char *filename, + char **r_value) +{ + size_t value_size; + return PyC_RunString_AsStringAndSize(imports, expr, filename, r_value, &value_size); +} + #endif /* #ifndef MATH_STANDALONE */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 5be44ad1af6..5fb5737e1f9 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -119,6 +119,11 @@ bool PyC_RunString_AsIntPtr(const char **imports, const char *expr, const char *filename, intptr_t *r_value); +bool PyC_RunString_AsStringAndSize(const char **imports, + const char *expr, + const char *filename, + char **r_value, + size_t *r_value_size); bool PyC_RunString_AsString(const char **imports, const char *expr, const char *filename, diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index a69ce068aec..2b9556e998a 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -618,8 +618,12 @@ bool BPY_execute_string_as_number( /** * \return success */ -bool BPY_execute_string_as_string( - bContext *C, const char *imports[], const char *expr, const bool verbose, char **r_value) +bool BPY_execute_string_as_string_and_size(bContext *C, + const char *imports[], + const char *expr, + const bool verbose, + char **r_value, + size_t *r_value_size) { BLI_assert(r_value && expr); PyGILState_STATE gilstate; @@ -632,7 +636,7 @@ bool BPY_execute_string_as_string( bpy_context_set(C, &gilstate); - ok = PyC_RunString_AsString(imports, expr, "", r_value); + ok = PyC_RunString_AsStringAndSize(imports, expr, "", r_value, r_value_size); if (ok == false) { if (verbose) { @@ -648,6 +652,14 @@ bool BPY_execute_string_as_string( return ok; } +bool BPY_execute_string_as_string( + bContext *C, const char *imports[], const char *expr, const bool verbose, char **r_value) +{ + size_t value_dummy_size; + return BPY_execute_string_as_string_and_size( + C, imports, expr, verbose, r_value, &value_dummy_size); +} + /** * Support both int and pointers. * -- cgit v1.2.3