Browse Source

Add WIP PythonEngine.

python
Andrew Belt 5 years ago
parent
commit
7e77f5c45b
2 changed files with 61 additions and 0 deletions
  1. +59
    -0
      src/PythonEngine.cpp
  2. +2
    -0
      tests/hello.py

+ 59
- 0
src/PythonEngine.cpp View File

@@ -0,0 +1,59 @@
#include "ScriptEngine.hpp"
#include <Python.h>


extern rack::Plugin* pluginInstance;


struct PythonEngine : ScriptEngine {
PyInterpreterState* interp = NULL;

~PythonEngine() {
if (interp)
PyInterpreterState_Delete(interp);
}

std::string getEngineName() override {
return "Python";
}

int run(const std::string& path, const std::string& script) override {
// Initialize Python
if (!Py_IsInitialized()) {
std::string pythonPath = rack::asset::plugin(pluginInstance, "dep/lib/python3.7");
wchar_t* pythonPathW = Py_DecodeLocale(pythonPath.c_str(), NULL);
Py_SetPath(pythonPathW);
PyMem_RawFree(pythonPathW);
Py_InitializeEx(0);

PyEval_InitThreads();
}
if (!Py_IsInitialized()) {
return -1;
}

PyThreadState* tstate = PyThreadState_Get();
interp = PyInterpreterState_New();
PyThreadState_Swap(tstate);

if (PyRun_SimpleString(script.c_str())) {
return -1;
}

// rack::DEBUG("4");
return 0;
}

int process() override {
PyThreadState* tstate = PyThreadState_New(interp);
PyEval_RestoreThread(tstate);
PyThreadState_Clear(tstate);
PyThreadState_DeleteCurrent();
return 0;
}
};


ScriptEngine* createPythonEngine() {
return new PythonEngine;
}

+ 2
- 0
tests/hello.py View File

@@ -0,0 +1,2 @@

print("Hello, world!")

Loading…
Cancel
Save