diff --git a/3rdparty/python-console/CMakeLists.txt b/3rdparty/python-console/CMakeLists.txt index dd4d6047..89433edc 100644 --- a/3rdparty/python-console/CMakeLists.txt +++ b/3rdparty/python-console/CMakeLists.txt @@ -1,15 +1,16 @@ cmake_minimum_required( VERSION 2.8 ) project( PythonInterpreter ) -find_package( Qt4 REQUIRED ) -include( ${QT_USE_FILE} ) +set(CMAKE_CXX_STANDARD 11) +find_package(Qt5 COMPONENTS Core Widgets REQUIRED) + find_package( PythonLibs REQUIRED ) include_directories( ${PYTHON_INCLUDE_DIRS} ) add_executable( test_python_interpreter test_python_interpreter.cpp Interpreter.cpp ) target_link_libraries( test_python_interpreter ${PYTHON_LIBRARIES} ) -qt4_wrap_cpp( Console_MOC Console.h ) +qt5_wrap_cpp( Console_MOC Console.h ) add_executable( test_console test_console.cpp Console.cpp ${Console_MOC} ColumnFormatter.cpp @@ -20,11 +21,13 @@ add_executable( test_console test_console.cpp ParseHelper.ContinuationParseState.cpp ParseMessage.cpp ) -target_link_libraries( test_console ${QT_LIBRARIES} ${PYTHON_LIBRARIES} ) +target_compile_definitions( test_console PRIVATE QT_NO_KEYWORDS) +target_link_libraries( test_console Qt5::Widgets ${PYTHON_LIBRARIES} ) add_executable( test_parse_helper test_parse_helper.cpp ParseHelper.cpp ParseHelper.BlockParseState.cpp + ParseHelper.BracketParseState.cpp ParseHelper.ContinuationParseState.cpp ParseListener.cpp ParseMessage.cpp @@ -33,6 +36,7 @@ add_executable( test_parse_helper test_parse_helper.cpp add_executable( test_cli test_cli.cpp ParseHelper.cpp ParseHelper.BlockParseState.cpp + ParseHelper.BracketParseState.cpp ParseHelper.ContinuationParseState.cpp ParseListener.cpp ParseMessage.cpp diff --git a/3rdparty/python-console/Console.cpp b/3rdparty/python-console/Console.cpp index f9413542..51057385 100644 --- a/3rdparty/python-console/Console.cpp +++ b/3rdparty/python-console/Console.cpp @@ -241,6 +241,7 @@ void Console::autocomplete( ) append(""); displayPrompt( ); moveCursorToEnd( ); + QTextCursor cursor = textCursor(); cursor.insertText( line ); moveCursorToEnd( ); } diff --git a/3rdparty/python-console/Interpreter.cpp b/3rdparty/python-console/Interpreter.cpp index 95615ed2..f84d9c5b 100644 --- a/3rdparty/python-console/Interpreter.cpp +++ b/3rdparty/python-console/Interpreter.cpp @@ -7,12 +7,12 @@ PyThreadState* Interpreter::MainThreadState = NULL; Interpreter::Interpreter( ) { - PyEval_AcquireLock( ); + PyEval_AcquireThread( MainThreadState ); m_threadState = Py_NewInterpreter( ); PyObject *module = PyImport_ImportModule("__main__"); loc = glb = PyModule_GetDict(module); - SetupRedirector( m_threadState ); + PyRun_SimpleString("import sys\n" "import redirector\n" "sys.path.insert(0, \".\")\n" // add current path @@ -50,7 +50,7 @@ Interpreter::test( ) PyEval_ReleaseThread( m_threadState ); return; } - dum = PyEval_EvalCode ((PyCodeObject *)py_result, glb, loc); + dum = PyEval_EvalCode (py_result, glb, loc); Py_XDECREF (dum); Py_XDECREF (py_result); @@ -89,7 +89,7 @@ Interpreter::interpret( const std::string& command, int* errorCode ) PyEval_ReleaseThread( m_threadState ); return res; } - dum = PyEval_EvalCode ((PyCodeObject *)py_result, glb, loc); + dum = PyEval_EvalCode (py_result, glb, loc); Py_XDECREF (dum); Py_XDECREF (py_result); if ( PyErr_Occurred( ) ) @@ -123,7 +123,7 @@ const std::list& Interpreter::suggest( const std::string& hint ) PyObject* py_result; PyObject* dum; py_result = Py_CompileString(command.c_str(), "", Py_single_input); - dum = PyEval_EvalCode ((PyCodeObject *)py_result, glb, loc); + dum = PyEval_EvalCode (py_result, glb, loc); Py_XDECREF (dum); Py_XDECREF (py_result); res = GetResultString( m_threadState ); @@ -149,6 +149,7 @@ const std::list& Interpreter::suggest( const std::string& hint ) void Interpreter::Initialize( ) { + PyImport_AppendInittab("redirector", Interpreter::PyInit_redirector); Py_Initialize( ); PyEval_InitThreads( ); MainThreadState = PyEval_SaveThread( ); @@ -195,7 +196,6 @@ PyObject* Interpreter::RedirectorWrite(PyObject *, PyObject *args) return Py_None; } -PyMethodDef Interpreter::ModuleMethods[] = { {NULL,NULL,0,NULL} }; PyMethodDef Interpreter::RedirectorMethods[] = { {"__init__", Interpreter::RedirectorInit, METH_VARARGS, @@ -205,27 +205,51 @@ PyMethodDef Interpreter::RedirectorMethods[] = {NULL,NULL,0,NULL}, }; -void Interpreter::SetupRedirector( PyThreadState* threadState ) + +PyObject *createClassObject(const char *name, PyMethodDef methods[]) { + PyObject *pClassName = PyUnicode_FromString(name); + PyObject *pClassBases = PyTuple_New(0); // An empty tuple for bases is equivalent to `(object,)` + PyObject *pClassDic = PyDict_New(); + + PyMethodDef *def; - - /* create a new module and class */ - PyObject *module = Py_InitModule("redirector", ModuleMethods); - PyObject *moduleDict = PyModule_GetDict(module); - PyObject *classDict = PyDict_New(); - PyObject *className = PyString_FromString("redirector"); - PyObject *fooClass = PyClass_New(NULL, classDict, className); - PyDict_SetItemString(moduleDict, "redirector", fooClass); - Py_DECREF(classDict); - Py_DECREF(className); - Py_DECREF(fooClass); - - /* add methods to class */ - for (def = RedirectorMethods; def->ml_name != NULL; def++) { + // add methods to class + for (def = methods; def->ml_name != NULL; def++) + { PyObject *func = PyCFunction_New(def, NULL); - PyObject *method = PyMethod_New(func, NULL, fooClass); - PyDict_SetItemString(classDict, def->ml_name, method); + PyObject *method = PyInstanceMethod_New(func); + PyDict_SetItemString(pClassDic, def->ml_name, method); Py_DECREF(func); Py_DECREF(method); } + + // pClass = type(pClassName, pClassBases, pClassDic) + PyObject *pClass = PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, pClassName, pClassBases, pClassDic, NULL); + + Py_DECREF(pClassName); + Py_DECREF(pClassBases); + Py_DECREF(pClassDic); + + + return pClass; +} + +PyMODINIT_FUNC Interpreter::PyInit_redirector(void) +{ + static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "redirector", + 0, + -1, + 0 + }; + + PyObject *m = PyModule_Create(&moduledef); + if (m) { + PyObject *fooClass = createClassObject("redirector", RedirectorMethods); + PyModule_AddObject(m, "redirector", fooClass); + Py_DECREF(fooClass); + } + return m; } diff --git a/3rdparty/python-console/Interpreter.h b/3rdparty/python-console/Interpreter.h index 9e371179..bc72d07c 100644 --- a/3rdparty/python-console/Interpreter.h +++ b/3rdparty/python-console/Interpreter.h @@ -71,9 +71,9 @@ public: */ Interpreter( ); virtual ~Interpreter( ); - + void test( ); - std::string interpret( const std::string& command, int* errorCode ); + std::string interpret( const std::string& command, int* errorCode ); const std::list& suggest( const std::string& hint ); /** @@ -87,7 +87,7 @@ public: static void Finalize( ); protected: - static void SetupRedirector( PyThreadState* threadState ); + static PyObject* PyInit_redirector(void); static PyObject* RedirectorInit(PyObject *, PyObject *); static PyObject* RedirectorWrite(PyObject *, PyObject *args); static std::string& GetResultString( PyThreadState* threadState ); diff --git a/3rdparty/python-console/ParseHelper.BlockParseState.cpp b/3rdparty/python-console/ParseHelper.BlockParseState.cpp index 4dc568b2..39c17b60 100644 --- a/3rdparty/python-console/ParseHelper.BlockParseState.cpp +++ b/3rdparty/python-console/ParseHelper.BlockParseState.cpp @@ -20,6 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "ParseHelper.h" +#include ParseHelper::BlockParseState:: BlockParseState( ParseHelper& parent ): diff --git a/3rdparty/python-console/ParseHelper.BracketParseState.cpp b/3rdparty/python-console/ParseHelper.BracketParseState.cpp index c884c77d..017e6a71 100644 --- a/3rdparty/python-console/ParseHelper.BracketParseState.cpp +++ b/3rdparty/python-console/ParseHelper.BracketParseState.cpp @@ -22,6 +22,7 @@ THE SOFTWARE. #include "ParseHelper.h" #include #include +#include const std::string ParseHelper::BracketParseState::OpeningBrackets = "[({"; const std::string ParseHelper::BracketParseState::ClosingBrackets = "])}";