From 58937676439255828c555480089b093afb11b3ea Mon Sep 17 00:00:00 2001 From: Florent Lamiraux <florent@laas.fr> Date: Mon, 24 Jan 2011 16:19:32 +0100 Subject: [PATCH] Define function display. This function is equivalent to print() under python, is equivalent to str() in dg-python. --- src/dynamic_graph/entity.py | 14 +++++++++----- src/interpreter.cc | 11 +++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/dynamic_graph/entity.py b/src/dynamic_graph/entity.py index 8360ea1..7c48880 100644 --- a/src/dynamic_graph/entity.py +++ b/src/dynamic_graph/entity.py @@ -6,6 +6,10 @@ import wrap, signal_base entityClassNameList = [] +if 'display' not in globals().keys(): + def display(s): + print(s) + def commandMethod(name, docstring) : def method(self, *arg): @@ -80,10 +84,10 @@ class Entity (object) : Print the list of signals into standard output: temporary. """ signals = self.signals() - print "--- <" + self.name + "> signal list: " + display ("--- <" + self.name + "> signal list: ") for s in signals[:-1]: - print " |-- <" + str(s) - print " `-- <" + str(signals[-1]) + display(" |-- <" + str(s)) + display(" `-- <" + str(signals[-1])) def signals(self) : """ @@ -108,7 +112,7 @@ class Entity (object) : ctitle+=' ' for docstr in wrap.entity_get_command_docstring(self.obj,cstr).split('\n'): if (len(docstr)>0) and (not docstr.isspace()): - print ctitle+"\t"+docstr + display(ctitle+"\t"+docstr) break def help( self,comm=None ): @@ -119,7 +123,7 @@ class Entity (object) : if comm is None: self.globalHelp() else: - print comm+":\n"+wrap.entity_get_command_docstring(self.obj,comm) + display(comm+":\n"+wrap.entity_get_command_docstring(self.obj,comm)) def __getattr__(self, name): diff --git a/src/interpreter.cc b/src/interpreter.cc index 9849382..23c8b27 100644 --- a/src/interpreter.cc +++ b/src/interpreter.cc @@ -20,11 +20,12 @@ // Python initialization commands namespace dynamicgraph { namespace python { - static const std::string pythonPrefix[4] = { + static const std::string pythonPrefix[5] = { "import sys\n", "import traceback\n", "if '' not in sys.path: sys.path.append('')\n", "sys.argv = ['']\n", + "def display(s): return str(s) if not s is None else None" }; } } @@ -43,6 +44,7 @@ Interpreter::Interpreter() PyRun_SimpleString(pythonPrefix[1].c_str()); PyRun_SimpleString(pythonPrefix[2].c_str()); PyRun_SimpleString(pythonPrefix[3].c_str()); + PyRun_SimpleString(pythonPrefix[4].c_str()); traceback_format_exception_ = PyDict_GetItemString (PyModule_GetDict(PyImport_AddModule("traceback")), "format_exception"); assert(PyCallable_Check(traceback_format_exception_)); @@ -51,9 +53,9 @@ Interpreter::Interpreter() Interpreter::~Interpreter() { - Py_DECREF(mainmod_); - Py_DECREF(globals_); - Py_DECREF(traceback_format_exception_); + //Py_DECREF(mainmod_); + //Py_DECREF(globals_); + //Py_DECREF(traceback_format_exception_); Py_Finalize(); } @@ -103,6 +105,7 @@ void Interpreter::runPythonFile( std::string filename ) PyRun_SimpleString(pythonPrefix[0].c_str()); PyRun_SimpleString(pythonPrefix[1].c_str()); PyRun_SimpleString(pythonPrefix[2].c_str()); + PyRun_SimpleString(pythonPrefix[4].c_str()); PyRun_SimpleFile(NULL, filename.c_str()); } -- GitLab