aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/python-console/Interpreter.h
diff options
context:
space:
mode:
authorMiodrag Milanovic <mmicko@gmail.com>2018-06-28 13:16:53 +0200
committerMiodrag Milanovic <mmicko@gmail.com>2018-06-28 17:57:26 +0200
commitc63274342f1aaa3d1087c0df5ffdaccfd0afcd56 (patch)
treeb04628f6c8c6768e2d5771737f3f13687cd0ed29 /3rdparty/python-console/Interpreter.h
parent66670831b89d934ef00c47c0527137f6ec38a0b0 (diff)
downloadnextpnr-c63274342f1aaa3d1087c0df5ffdaccfd0afcd56.tar.gz
nextpnr-c63274342f1aaa3d1087c0df5ffdaccfd0afcd56.tar.bz2
nextpnr-c63274342f1aaa3d1087c0df5ffdaccfd0afcd56.zip
initial import of python-console
Diffstat (limited to '3rdparty/python-console/Interpreter.h')
-rw-r--r--3rdparty/python-console/Interpreter.h97
1 files changed, 97 insertions, 0 deletions
diff --git a/3rdparty/python-console/Interpreter.h b/3rdparty/python-console/Interpreter.h
new file mode 100644
index 00000000..9e371179
--- /dev/null
+++ b/3rdparty/python-console/Interpreter.h
@@ -0,0 +1,97 @@
+/**
+python-console
+Copyright (C) 2014 Alex Tsui
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef INTERPRETER_H
+#define INTERPRETER_H
+#include <string>
+#include <list>
+#include <Python.h>
+#if defined(__APPLE__) && defined(__MACH__)
+/*
+ * The following undefs for C standard library macros prevent
+ * build errors of the following type on mac ox 10.7.4 and XCode 4.3.3
+ *
+/usr/include/c++/4.2.1/bits/localefwd.h:57:21: error: too many arguments provided to function-like macro invocation
+ isspace(_CharT, const locale&);
+ ^
+/usr/include/c++/4.2.1/bits/localefwd.h:56:5: error: 'inline' can only appear on functions
+ inline bool
+ ^
+/usr/include/c++/4.2.1/bits/localefwd.h:57:5: error: variable 'isspace' declared as a template
+ isspace(_CharT, const locale&);
+ ^
+*/
+#undef isspace
+#undef isupper
+#undef islower
+#undef isalpha
+#undef isalnum
+#undef toupper
+#undef tolower
+#endif
+
+/**
+Wraps a Python interpreter, which you can pass commands as strings to interpret
+and get strings of output/error in return.
+*/
+class Interpreter
+{
+protected:
+ static PyThreadState* MainThreadState;
+
+ PyThreadState* m_threadState;
+ PyObject* glb;
+ PyObject* loc;
+
+ std::list< std::string > m_suggestions;
+
+public:
+ /**
+ Instantiate a Python interpreter.
+ */
+ Interpreter( );
+ virtual ~Interpreter( );
+
+ void test( );
+ std::string interpret( const std::string& command, int* errorCode );
+ const std::list<std::string>& suggest( const std::string& hint );
+
+ /**
+ Call this before constructing and using Interpreter.
+ */
+ static void Initialize( );
+
+ /**
+ Call this when done using Interpreter.
+ */
+ static void Finalize( );
+
+protected:
+ static void SetupRedirector( PyThreadState* threadState );
+ static PyObject* RedirectorInit(PyObject *, PyObject *);
+ static PyObject* RedirectorWrite(PyObject *, PyObject *args);
+ static std::string& GetResultString( PyThreadState* threadState );
+ static PyMethodDef ModuleMethods[];
+ static PyMethodDef RedirectorMethods[];
+};
+#endif // INTERPRETER_H