diff --git a/source/blender/python/generic/py_capi_utils.cc b/source/blender/python/generic/py_capi_utils.cc index 8d9e62e2e65..27a2ba9f956 100644 --- a/source/blender/python/generic/py_capi_utils.cc +++ b/source/blender/python/generic/py_capi_utils.cc @@ -1053,6 +1053,11 @@ PyObject *PyC_UnicodeFromBytes(const char *str) return PyC_UnicodeFromBytesAndSize(str, strlen(str)); } +PyObject *PyC_UnicodeFromStdStr(const std::string &str) +{ + return PyC_UnicodeFromBytesAndSize(str.c_str(), str.length()); +} + int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p) { PyC_UnicodeAsBytesAndSize_Data *data = static_cast(p); diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 8e728bf2ebe..0b72071be1b 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -335,6 +335,13 @@ bool PyC_StructFmt_type_is_bool(char format); #ifdef __cplusplus # include "BLI_span.hh" +# include + +/** + * Create a `str` from `std::string`, wraps #PyC_UnicodeFromBytesAndSize. + */ +PyObject *PyC_UnicodeFromStdStr(const std::string &str); + inline PyObject *PyC_Tuple_Pack_F32(const blender::Span values) { return PyC_Tuple_PackArray_F32(values.data(), values.size()); diff --git a/source/blender/python/intern/bpy.cc b/source/blender/python/intern/bpy.cc index 824c515aba1..aa0d505148d 100644 --- a/source/blender/python/intern/bpy.cc +++ b/source/blender/python/intern/bpy.cc @@ -78,11 +78,11 @@ static PyObject *bpy_script_paths(PyObject * /*self*/) PyObject *item; std::optional path = BKE_appdir_folder_id(BLENDER_SYSTEM_SCRIPTS, nullptr); - item = PyC_UnicodeFromBytes(path.has_value() ? path->c_str() : ""); + item = PyC_UnicodeFromStdStr(path.has_value() ? path.value() : ""); BLI_assert(item != nullptr); PyTuple_SET_ITEM(ret, 0, item); path = BKE_appdir_folder_id(BLENDER_USER_SCRIPTS, nullptr); - item = PyC_UnicodeFromBytes(path.has_value() ? path->c_str() : ""); + item = PyC_UnicodeFromStdStr(path.has_value() ? path.value() : ""); BLI_assert(item != nullptr); PyTuple_SET_ITEM(ret, 1, item); @@ -258,7 +258,7 @@ static PyObject *bpy_user_resource(PyObject * /*self*/, PyObject *args, PyObject subdir_data.value); Py_XDECREF(subdir_data.value_coerce); - return PyC_UnicodeFromBytes(path.has_value() ? path->c_str() : ""); + return PyC_UnicodeFromStdStr(path.has_value() ? path.value() : ""); } PyDoc_STRVAR( @@ -308,7 +308,7 @@ static PyObject *bpy_system_resource(PyObject * /*self*/, PyObject *args, PyObje std::optional path = BKE_appdir_folder_id(type.value_found, subdir_data.value); Py_XDECREF(subdir_data.value_coerce); - return PyC_UnicodeFromBytes(path.has_value() ? path->c_str() : ""); + return PyC_UnicodeFromStdStr(path.has_value() ? path.value() : ""); } PyDoc_STRVAR( @@ -358,7 +358,7 @@ static PyObject *bpy_resource_path(PyObject * /*self*/, PyObject *args, PyObject const std::optional path = BKE_appdir_resource_path_id_with_version( type.value_found, false, (major * 100) + minor); - return PyC_UnicodeFromBytes(path.has_value() ? path->c_str() : ""); + return PyC_UnicodeFromStdStr(path.has_value() ? path.value() : ""); } /* This is only exposed for tests, see: `tests/python/bl_pyapi_bpy_driver_secure_eval.py`. */ @@ -668,7 +668,7 @@ void BPy_init_modules(bContext *C) if (modpath.has_value()) { // printf("bpy: found module path '%s'.\n", modpath); PyObject *sys_path = PySys_GetObject("path"); /* borrow */ - PyObject *py_modpath = PyC_UnicodeFromBytes(modpath->c_str()); + PyObject *py_modpath = PyC_UnicodeFromStdStr(modpath.value()); PyList_Insert(sys_path, 0, py_modpath); /* add first */ Py_DECREF(py_modpath); } diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 1db2c81aee0..cbe3bdbdd45 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -176,7 +176,7 @@ static PyObject *cache_or_get_image_file_func(PyObject * /*self*/, PyObject *arg std::string image_path = io::hydra::cache_or_get_image_file( CTX_data_main(context), CTX_data_scene(context), image, nullptr); - return PyC_UnicodeFromBytes(image_path.c_str()); + return PyC_UnicodeFromStdStr(image_path); } static PyMethodDef methods[] = {