1 /** 2 python-console 3 Copyright (C) 2014 Alex Tsui 4 5 Permission is hereby granted, free of charge, to any person obtaining a copy of 6 this software and associated documentation files (the "Software"), to deal in 7 the Software without restriction, including without limitation the rights to 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 of the Software, and to permit persons to whom the Software is furnished to do 10 so, subject to the following conditions: 11 12 The above copyright notice and this permission notice shall be included in all 13 copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 SOFTWARE. 22 */ 23 24 #ifndef INTERPRETER_H 25 #define INTERPRETER_H 26 #include <string> 27 #include <list> 28 #include <Python.h> 29 #if defined(__APPLE__) && defined(__MACH__) 30 /* 31 * The following undefs for C standard library macros prevent 32 * build errors of the following type on mac ox 10.7.4 and XCode 4.3.3 33 * 34 /usr/include/c++/4.2.1/bits/localefwd.h:57:21: error: too many arguments provided to function-like macro invocation 35 isspace(_CharT, const locale&); 36 ^ 37 /usr/include/c++/4.2.1/bits/localefwd.h:56:5: error: 'inline' can only appear on functions 38 inline bool 39 ^ 40 /usr/include/c++/4.2.1/bits/localefwd.h:57:5: error: variable 'isspace' declared as a template 41 isspace(_CharT, const locale&); 42 ^ 43 */ 44 #undef isspace 45 #undef isupper 46 #undef islower 47 #undef isalpha 48 #undef isalnum 49 #undef toupper 50 #undef tolower 51 #endif 52 53 /** 54 Wraps a Python interpreter, which you can pass commands as strings to interpret 55 and get strings of output/error in return. 56 */ 57 class Interpreter 58 { 59 protected: 60 static PyThreadState* MainThreadState; 61 62 PyThreadState* m_threadState; 63 PyObject* glb; 64 PyObject* loc; 65 66 std::list< std::string > m_suggestions; 67 68 public: 69 /** 70 Instantiate a Python interpreter. 71 */ 72 Interpreter( ); 73 virtual ~Interpreter( ); 74 75 void test( ); 76 std::string interpret( const std::string& command, int* errorCode ); 77 const std::list<std::string>& suggest( const std::string& hint ); 78 79 /** 80 Call this before constructing and using Interpreter. 81 */ 82 static void Initialize( ); 83 84 /** 85 Call this when done using Interpreter. 86 */ 87 static void Finalize( ); 88 89 protected: 90 static PyObject* PyInit_redirector(void); 91 static PyObject* RedirectorInit(PyObject *, PyObject *); 92 static PyObject* RedirectorWrite(PyObject *, PyObject *args); 93 static std::string& GetResultString( PyThreadState* threadState ); 94 static PyMethodDef ModuleMethods[]; 95 static PyMethodDef RedirectorMethods[]; 96 }; 97 #endif // INTERPRETER_H 98