diff --git a/src/Base/Tools.cpp b/src/Base/Tools.cpp index 29d2f96ed2fb..8957f0c12622 100644 --- a/src/Base/Tools.cpp +++ b/src/Base/Tools.cpp @@ -30,6 +30,7 @@ # include #include "PyExport.h" +#include "Interpreter.h" #include "Tools.h" namespace Base { @@ -144,17 +145,28 @@ std::string Base::Tools::narrow(const std::wstring& str) std::string Base::Tools::escapedUnicodeFromUtf8(const char *s) { + Base::PyGILStateLocker lock; + std::string escapedstr; + PyObject* unicode = PyUnicode_FromString(s); + if (!unicode) + return escapedstr; + PyObject* escaped = PyUnicode_AsUnicodeEscapeString(unicode); + if (escaped) { + escapedstr = std::string(PyString_AsString(escaped)); + Py_DECREF(escaped); + } + Py_DECREF(unicode); - std::string escapedstr = std::string(PyString_AsString(escaped)); - Py_DECREF(escaped); return escapedstr; } std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s) { + Base::PyGILStateLocker lock; std::string string; + PyObject* unicode = PyUnicode_DecodeUnicodeEscape(s.c_str(), s.size(), "strict"); if (!unicode) return string;