From d358b4a1df0da259f5b2354f694feab6a14b963c Mon Sep 17 00:00:00 2001
From: Olivier Stasse <olivier.stasse@gmail.com>
Date: Fri, 20 Jul 2012 14:37:07 +0200
Subject: [PATCH] Robustify the python interpreter when some python objects
 cannot be stringified.

---
 src/interpreter.cc | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/interpreter.cc b/src/interpreter.cc
index 209363a..a0cd181 100644
--- a/src/interpreter.cc
+++ b/src/interpreter.cc
@@ -110,7 +110,11 @@ std::string Interpreter::python( const std::string& command )
   } else {
     result = PyObject_Repr(result);
   }
-  std::string value = PyString_AsString(result);
+  std::string value = "";
+  // PyString_AsString will generate a segv if result is NULL.
+  // This might be the case if PyObject_Repr fails.
+  if (result!=NULL)
+    value = PyString_AsString(result);
   return value;
 }
 
@@ -157,9 +161,13 @@ void Interpreter::python( const std::string& command, std::string& res,
                                       Py_eval_input, globals_,
                                       globals_);
   out = PyString_AsString(stdout_obj);
+  // Local display for the robot (in debug mode or for the logs)
   std::cout << out;
   result = PyObject_Repr(result);
-  res = PyString_AsString(result);
+  // If python cannot build a string representation of result
+  // then results is equal to NULL. This will trigger a SEGV
+  if (result!=NULL)
+    res = PyString_AsString(result);
   return;
 }
 
-- 
GitLab