From b5bc9d80a11dc99a296d0621bf3fdd156b70b754 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 10 Dec 2020 14:09:29 +1100 Subject: [PATCH] PyAPI: add bpy.utils.unescape_identifier Utility to perform the reverse of `bpy.utils.escape_identifier` --- release/scripts/modules/bpy/utils/__init__.py | 2 + source/blender/python/intern/bpy.c | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py index 3ff259e0e3e..897010e80cf 100644 --- a/release/scripts/modules/bpy/utils/__init__.py +++ b/release/scripts/modules/bpy/utils/__init__.py @@ -26,6 +26,7 @@ not associated with blenders internal data. __all__ = ( "blend_paths", "escape_identifier", + "unescape_identifier", "keyconfig_init", "keyconfig_set", "load_scripts", @@ -60,6 +61,7 @@ from _bpy import ( _utils_units as units, blend_paths, escape_identifier, + unescape_identifier, register_class, resource_path, script_paths as _bpy_script_paths, diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c index 2e085f08946..bd2f40569f6 100644 --- a/source/blender/python/intern/bpy.c +++ b/source/blender/python/intern/bpy.c @@ -292,6 +292,50 @@ static PyObject *bpy_escape_identifier(PyObject *UNUSED(self), PyObject *value) return value_escape; } +PyDoc_STRVAR(bpy_unescape_identifier_doc, + ".. function:: unescape_identifier(string)\n" + "\n" + " Simple string un-escape function used for animation paths.\n" + " This performs the reverse of `escape_identifier`.\n" + "\n" + " :arg string: text\n" + " :type string: string\n" + " :return: The un-escaped string.\n" + " :rtype: string\n"); +static PyObject *bpy_unescape_identifier(PyObject *UNUSED(self), PyObject *value) +{ + const char *value_str; + Py_ssize_t value_str_len; + + char *value_unescape_str; + Py_ssize_t value_unescape_str_len; + PyObject *value_unescape; + size_t size; + + value_str = _PyUnicode_AsStringAndSize(value, &value_str_len); + + if (value_str == NULL) { + PyErr_SetString(PyExc_TypeError, "expected a string"); + return NULL; + } + + size = value_str_len + 1; + value_unescape_str = PyMem_MALLOC(size); + value_unescape_str_len = BLI_str_unescape(value_unescape_str, value_str, size); + + if (value_unescape_str_len == value_str_len) { + Py_INCREF(value); + value_unescape = value; + } + else { + value_unescape = PyUnicode_FromStringAndSize(value_unescape_str, value_unescape_str_len); + } + + PyMem_FREE(value_unescape_str); + + return value_unescape; +} + static PyMethodDef meth_bpy_script_paths = { "script_paths", (PyCFunction)bpy_script_paths, @@ -328,6 +372,12 @@ static PyMethodDef meth_bpy_escape_identifier = { METH_O, bpy_escape_identifier_doc, }; +static PyMethodDef meth_bpy_unescape_identifier = { + "unescape_identifier", + (PyCFunction)bpy_unescape_identifier, + METH_O, + bpy_unescape_identifier_doc, +}; static PyObject *bpy_import_test(const char *modname) { @@ -429,6 +479,9 @@ void BPy_init_modules(struct bContext *C) PyModule_AddObject(mod, meth_bpy_escape_identifier.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_escape_identifier, NULL)); + PyModule_AddObject(mod, + meth_bpy_unescape_identifier.ml_name, + (PyObject *)PyCFunction_New(&meth_bpy_unescape_identifier, NULL)); /* register funcs (bpy_rna.c) */ PyModule_AddObject(mod,