From f6027d805cfa2e83f7e932ec996a81b9265acd29 Mon Sep 17 00:00:00 2001 From: bdieterm Date: Sun, 20 Aug 2023 01:04:01 +0200 Subject: [PATCH] Core: escape filepath characters in FreeCAD.loadFile --- src/App/ApplicationPy.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/App/ApplicationPy.cpp b/src/App/ApplicationPy.cpp index 46bb8d1016..850efb7724 100644 --- a/src/App/ApplicationPy.cpp +++ b/src/App/ApplicationPy.cpp @@ -210,12 +210,22 @@ PyObject* Application::sLoadFile(PyObject * /*self*/, PyObject *args) } } + // path could contain characters that need escaping, such as quote signs + // therefore use its representation in the Python code string + PyObject *pathObj = PyUnicode_FromString(path); + PyObject *pathReprObj = PyObject_Repr(pathObj); + const char *pathRepr = PyUnicode_AsUTF8(pathReprObj); + std::stringstream str; str << "import " << module << std::endl; if (fi.hasExtension("FCStd")) - str << module << ".openDocument('" << path << "')" << std::endl; + str << module << ".openDocument(" << pathRepr << ")" << std::endl; else - str << module << ".insert('" << path << "','" << doc << "')" << std::endl; + str << module << ".insert(" << pathRepr << ",'" << doc << "')" << std::endl; + + Py_DECREF(pathObj); + Py_DECREF(pathReprObj); + Base::Interpreter().runString(str.str().c_str()); Py_Return; }