From 9cf23d101041d1e990f57d78e4712226b8a449c7 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 28 Jun 2018 13:32:06 +0200 Subject: move to c++11 remove console writes --- 3rdparty/python-console/Interpreter.cpp | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) (limited to '3rdparty/python-console/Interpreter.cpp') diff --git a/3rdparty/python-console/Interpreter.cpp b/3rdparty/python-console/Interpreter.cpp index f84d9c5b..e66e6532 100644 --- a/3rdparty/python-console/Interpreter.cpp +++ b/3rdparty/python-console/Interpreter.cpp @@ -1,7 +1,7 @@ #include "Interpreter.h" #include #include -#include +#include PyThreadState* Interpreter::MainThreadState = NULL; @@ -27,9 +27,6 @@ Interpreter::Interpreter( ) Interpreter::~Interpreter( ) { -#ifndef NDEBUG - std::cout << "delete interpreter\n"; -#endif PyEval_AcquireThread( m_threadState ); Py_EndInterpreter( m_threadState ); PyEval_ReleaseLock( ); @@ -60,6 +57,15 @@ Interpreter::test( ) PyEval_ReleaseThread( m_threadState ); } +template +std::string string_format( const std::string& format, Args ... args ) +{ + size_t size = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' + std::unique_ptr buf( new char[ size ] ); + std::snprintf( buf.get(), size, format.c_str(), args ... ); + return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside +} + std::string Interpreter::interpret( const std::string& command, int* errorCode ) { @@ -69,15 +75,9 @@ Interpreter::interpret( const std::string& command, int* errorCode ) PyObject* py_result; PyObject* dum; std::string res; -#ifndef NDEBUG - std::cout << "interpreting (" << command << ")\n"; -#endif py_result = Py_CompileString(command.c_str(), "", Py_single_input); if ( py_result == 0 ) { -#ifndef NDEBUG - std::cout << "Huh?\n"; -#endif if ( PyErr_Occurred( ) ) { *errorCode = 1; @@ -110,13 +110,7 @@ const std::list& Interpreter::suggest( const std::string& hint ) PyEval_AcquireThread( m_threadState ); m_suggestions.clear(); int i = 0; - std::string command = boost::str( - boost::format("sys.completer.complete('%1%', %2%)\n") - % hint - % i); -#ifndef NDEBUG - std::cout << command << "\n"; -#endif + std::string command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i); std::string res; do { @@ -129,10 +123,7 @@ const std::list& Interpreter::suggest( const std::string& hint ) res = GetResultString( m_threadState ); GetResultString( m_threadState ) = ""; ++i; - command = boost::str( - boost::format("sys.completer.complete('%1%', %2%)\n") - % hint - % i); + command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i); if (res.size()) { // throw away the newline -- cgit v1.2.3