Skip to content

Commit

Permalink
Merge pull request #1736 from dictoon/master
Browse files Browse the repository at this point in the history
Escape backslashes in strings passed to Python interpreter
  • Loading branch information
dictoon committed Nov 26, 2017
2 parents 1678b0d + b1ee7a1 commit 153757a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 10 additions & 14 deletions src/appleseed.studio/python/pythoninterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
// Boost headers.
#include "boost/filesystem.hpp"

// Standard headers.
#include <string>

using namespace appleseed::shared;
using namespace foundation;
using namespace std;
Expand Down Expand Up @@ -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&)
{
Expand Down
5 changes: 4 additions & 1 deletion src/appleseed.studio/python/pythoninterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "foundation/core/concepts/noncopyable.h"
#include "foundation/platform/python.h"

// Standard headers.
#include <string>

namespace appleseed {
namespace studio {

Expand All @@ -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();
Expand Down

0 comments on commit 153757a

Please sign in to comment.