diff --git a/src/appleseed.studio/mainwindow/pythonconsole/pythonconsolewidget.cpp b/src/appleseed.studio/mainwindow/pythonconsole/pythonconsolewidget.cpp index c23376426e..def2930a24 100644 --- a/src/appleseed.studio/mainwindow/pythonconsole/pythonconsolewidget.cpp +++ b/src/appleseed.studio/mainwindow/pythonconsole/pythonconsolewidget.cpp @@ -184,7 +184,7 @@ void PythonConsoleWidget::slot_execute_all() void PythonConsoleWidget::execute(const QString& script) { - PythonInterpreter::instance().execute(script.toStdString().c_str()); + PythonInterpreter::instance().execute(script.toStdString()); } void PythonConsoleWidget::slot_clear_output() diff --git a/src/appleseed.studio/python/pythoninterpreter.cpp b/src/appleseed.studio/python/pythoninterpreter.cpp index 4c943d4ea6..525b3e6ea1 100644 --- a/src/appleseed.studio/python/pythoninterpreter.cpp +++ b/src/appleseed.studio/python/pythoninterpreter.cpp @@ -44,9 +44,6 @@ // Boost headers. #include "boost/filesystem.hpp" -// Standard headers. -#include - using namespace appleseed::shared; using namespace foundation; using namespace std; @@ -182,30 +179,29 @@ void PythonInterpreter::initialize(OutputRedirector redirector) void PythonInterpreter::import_python_module(const char* module_name, const char* alias_name) { - const string command = format("import {0}\n{1} = {0}\n", module_name, alias_name); - execute(command.c_str()); + execute(format("import {0}\n{1} = {0}\n", module_name, alias_name)); } void PythonInterpreter::load_plugins() { - const string bundled_plugins_path = compute_bundled_plugins_path(); - const string command = format( - "import appleseed.studio.plugins\n" - "appleseed.studio.plugins.load_plugins('{0}')\n", - bundled_plugins_path); - execute(command.c_str()); + execute( + format( + "import appleseed.studio.plugins\n" + "appleseed.studio.plugins.load_plugins('{0}')\n", + compute_bundled_plugins_path())); } -bpy::object PythonInterpreter::execute(const char* command) +bpy::object PythonInterpreter::execute(const string& command) { if (!m_is_initialized) - throw Exception("Attempt to execute command while interpreter is not initialized"); + throw Exception("Attempt to execute Python command while interpreter is not initialized"); bpy::object result; try { - result = bpy::exec(command, m_main_namespace, m_main_namespace); + const string escaped_command = replace(command, "\\", "\\\\"); + result = bpy::exec(escaped_command.c_str(), m_main_namespace, m_main_namespace); } catch (const bpy::error_already_set&) { diff --git a/src/appleseed.studio/python/pythoninterpreter.h b/src/appleseed.studio/python/pythoninterpreter.h index e91d2985ec..40d34f66a6 100644 --- a/src/appleseed.studio/python/pythoninterpreter.h +++ b/src/appleseed.studio/python/pythoninterpreter.h @@ -33,6 +33,9 @@ #include "foundation/core/concepts/noncopyable.h" #include "foundation/platform/python.h" +// Standard headers. +#include + namespace appleseed { namespace studio { @@ -53,7 +56,7 @@ class PythonInterpreter void load_plugins(); - boost::python::object execute(const char* command); + boost::python::object execute(const std::string& command); private: PythonInterpreter();