From 7923cd32cefeacec87171ec775d9aa1b97ed56a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20Gr=C3=A4ser?= <graeser@mi.fu-berlin.de> Date: Tue, 14 Apr 2009 13:18:56 +0000 Subject: [PATCH] You call now also construct functions from multiline python function bodies. [[Imported from SVN: r2386]] --- ag-common/functiontools/pythonfunction.hh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ag-common/functiontools/pythonfunction.hh b/ag-common/functiontools/pythonfunction.hh index 9462b6cc..60498dcd 100644 --- a/ag-common/functiontools/pythonfunction.hh +++ b/ag-common/functiontools/pythonfunction.hh @@ -184,13 +184,19 @@ class PythonFunction : /** * \brief Create PythonFunction from string expression. * - * This creates a python function with the given name. + * This creates a python function with the given name. If expression contains no newline + * it is directly evaluated and returned. If it contains a newline it is used as function + * body and expected to call return itself. */ PythonFunction(const std::string name, const std::string expression) { // create a python function std::stringstream s; - s << "def " << name << "(x,i=0):\n return " << expression; + std::string::size_type newline = expression.find("\n"); + if (newline != std::string::npos) + s << "def " << name << "(x,i=0):\n" << expression; + else + s << "def " << name << "(x,i=0):\n return " << expression; PyRun_SimpleString(s.str().c_str()); // get reference for python function -- GitLab