Cleanup: add PyC_UnicodeFromStdStr utility function

Add a std::string wrapper for PyC_UnicodeFromBytesAndSize.
This commit is contained in:
Campbell Barton 2024-01-30 15:54:10 +11:00
parent a360bd3f31
commit 948e806a1a
4 changed files with 19 additions and 7 deletions

View File

@ -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<PyC_UnicodeAsBytesAndSize_Data *>(p);

View File

@ -335,6 +335,13 @@ bool PyC_StructFmt_type_is_bool(char format);
#ifdef __cplusplus
# include "BLI_span.hh"
# include <string>
/**
* 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<float> values)
{
return PyC_Tuple_PackArray_F32(values.data(), values.size());

View File

@ -78,11 +78,11 @@ static PyObject *bpy_script_paths(PyObject * /*self*/)
PyObject *item;
std::optional<std::string> 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<std::string> 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<std::string> 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);
}

View File

@ -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[] = {