Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape backslashes in strings passed to Python interpreter #1736

Merged
merged 1 commit into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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