fix possible crashes in Tools::escapedUnicodeFromUtf8
This commit is contained in:
parent
796c8330fe
commit
fb9fb9873f
|
@ -30,6 +30,7 @@
|
|||
|
||||
# include <QTime>
|
||||
#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);
|
||||
Py_DECREF(unicode);
|
||||
std::string escapedstr = std::string(PyString_AsString(escaped));
|
||||
if (escaped) {
|
||||
escapedstr = std::string(PyString_AsString(escaped));
|
||||
Py_DECREF(escaped);
|
||||
}
|
||||
|
||||
Py_DECREF(unicode);
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user