From 05a25967bd756405a688526f4b63c5fad6e71378 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 12 Sep 2016 20:58:41 +0200 Subject: [PATCH] convert utf-8 encoded path name to escaped unicode when saving document with Python --- src/App/DocumentPyImp.cpp | 13 ++++++++----- src/Gui/Document.cpp | 6 ++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index 2c3be6c25..7cc4b9f22 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -83,11 +83,14 @@ PyObject* DocumentPy::save(PyObject * args) PyObject* DocumentPy::saveAs(PyObject * args) { char* fn; - if (!PyArg_ParseTuple(args, "s", &fn)) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "et", "utf-8", &fn)) + return NULL; + + std::string utf8Name = fn; + PyMem_Free(fn); try { - if (!getDocumentPtr()->saveAs(fn)) { + if (!getDocumentPtr()->saveAs(utf8Name.c_str())) { PyErr_SetString(PyExc_ValueError, "Object attribute 'FileName' is not set"); return NULL; } @@ -101,9 +104,9 @@ PyObject* DocumentPy::saveAs(PyObject * args) return 0; } - Base::FileInfo fi(fn); + Base::FileInfo fi(utf8Name); if (!fi.isReadable()) { - PyErr_Format(PyExc_IOError, "No such file or directory: '%s'", fn); + PyErr_Format(PyExc_IOError, "No such file or directory: '%s'", utf8Name.c_str()); return NULL; } diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index 15406bdc3..a8bbe86b6 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -670,8 +671,9 @@ bool Document::saveAs(void) // save as new file name try { Gui::WaitCursor wc; - Command::doCommand(Command::Doc,"App.getDocument(\"%s\").saveAs(\"%s\")" - , DocName, (const char*)fn.toUtf8()); + std::string escapedstr = Base::Tools::escapedUnicodeFromUtf8(fn.toUtf8()); + Command::doCommand(Command::Doc,"App.getDocument(\"%s\").saveAs(u\"%s\")" + , DocName, escapedstr.c_str()); setModified(false); getMainWindow()->appendRecentFile(fi.filePath()); }