123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "platform.h"
- #include "Python.h"
- #include "eclrtl.hpp"
- #include "jexcept.hpp"
- #include "jthread.hpp"
- #include "hqlplugins.hpp"
- #ifdef _WIN32
- #define EXPORT __declspec(dllexport)
- #else
- #define EXPORT
- #endif
- static const char * compatibleVersions[] = {
- "Python2.7 Embed Helper 1.0.0",
- NULL };
- static const char *version = "Python2.7 Embed Helper 1.0.0";
- static const char * EclDefinition =
- "EXPORT Language := SERVICE\n"
- " boolean getEmbedContext():cpp,pure,namespace='pyembed',entrypoint='getEmbedContext',prototype='IEmbedContext* getEmbedContext()';\n"
- " boolean syntaxCheck(const varstring src):cpp,pure,namespace='pyembed',entrypoint='syntaxCheck';\n"
- "END;"
- "EXPORT getEmbedContext := Language.getEmbedContext;"
- "EXPORT syntaxCheck := Language.syntaxCheck;"
- "EXPORT boolean supportsImport := true;"
- "EXPORT boolean supportsScript := true;";
- extern "C" EXPORT bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
- {
- if (pb->size == sizeof(ECLPluginDefinitionBlockEx))
- {
- ECLPluginDefinitionBlockEx * pbx = (ECLPluginDefinitionBlockEx *) pb;
- pbx->compatibleVersions = compatibleVersions;
- }
- else if (pb->size != sizeof(ECLPluginDefinitionBlock))
- return false;
- pb->magicVersion = PLUGIN_VERSION;
- pb->version = version;
- pb->moduleName = "python";
- pb->ECL = EclDefinition;
- pb->flags = PLUGIN_DLL_MODULE | PLUGIN_MULTIPLE_VERSIONS;
- pb->description = "Python2.7 Embed Helper";
- return true;
- }
- namespace pyembed {
- // Use class OwnedPyObject for any objects that are not 'borrowed references'
- // so that the appropriate Py_DECREF call is made when the OwnedPyObject goes
- // out of scope, even if the function returns prematurely (such as via an exception).
- // In particular, checkPythonError is a lot easier to call safely if this is used.
- class OwnedPyObject
- {
- PyObject *ptr;
- public:
- inline OwnedPyObject() : ptr(NULL) {}
- inline OwnedPyObject(PyObject *_ptr) : ptr(_ptr) {}
- inline ~OwnedPyObject() { if (ptr) Py_DECREF(ptr); }
- inline PyObject * get() const { return ptr; }
- inline PyObject * operator -> () const { return ptr; }
- inline operator PyObject *() const { return ptr; }
- inline void clear() { if (ptr) Py_DECREF(ptr); ptr = NULL; }
- inline void setown(PyObject *_ptr) { clear(); ptr = _ptr; }
- inline void set(PyObject *_ptr) { clear(); ptr = _ptr; if (ptr) Py_INCREF(ptr);}
- inline PyObject *getLink() { if (ptr) Py_INCREF(ptr); return ptr;}
- inline PyObject **ref() { return &ptr; }
- };
- // call checkPythonError to throw an exception if Python error state is set
- static void checkPythonError()
- {
- PyObject* err = PyErr_Occurred();
- if (err)
- {
- OwnedPyObject pType, pValue, pTraceBack;
- PyErr_Fetch(pType.ref(), pValue.ref(), pTraceBack.ref());
- OwnedPyObject valStr = PyObject_Str(pValue);
- PyErr_Clear();
- VStringBuffer errMessage("pyembed: %s", PyString_AsString(valStr));
- rtlFail(0, errMessage.str());
- }
- }
- // The Python Global Interpreter Lock (GIL) won't know about C++-created threads, so we need to
- // call PyGILState_Ensure() and PyGILState_Release at the start and end of every function.
- // Wrapping them in a class like this ensures that the release always happens even if
- // the function exists prematurely
- class GILstateWrapper
- {
- PyGILState_STATE gstate;
- public:
- GILstateWrapper()
- {
- gstate = PyGILState_Ensure();
- }
- ~GILstateWrapper()
- {
- PyGILState_Release(gstate);
- }
- };
- // There is a singleton PythonThreadContext per thread. This allows us to
- // ensure that we can make repeated calls to a Python function efficiently.
- class PythonThreadContext
- {
- public:
- PyThreadState *threadState;
- public:
- PythonThreadContext()
- {
- threadState = PyEval_SaveThread();
- }
- ~PythonThreadContext()
- {
- PyEval_RestoreThread(threadState);
- script.clear();
- }
- inline PyObject * importFunction(size32_t lenChars, const char *utf)
- {
- size32_t bytes = rtlUtf8Size(lenChars, utf);
- StringBuffer text(bytes, utf);
- if (!prevtext || strcmp(text, prevtext) != 0)
- {
- prevtext.clear();
- // Name should be in the form module.function
- const char *funcname = strrchr(text, '.');
- if (!funcname)
- rtlFail(0, "Expected module.function");
- StringBuffer modname(funcname-text, text);
- funcname++; // skip the '.'
- // If the modname is preceded by a path, add it to the python path before importing
- const char *pathsep = strrchr(modname, PATHSEPCHAR);
- if (pathsep)
- {
- StringBuffer path(pathsep-modname, modname);
- modname.remove(0, 1+pathsep-modname);
- PyObject *sys_path = PySys_GetObject((char *) "path");
- OwnedPyObject new_path = PyString_FromString(path);
- if (sys_path)
- {
- PyList_Append(sys_path, new_path);
- checkPythonError();
- }
- }
- module.setown(PyImport_ImportModule(modname));
- checkPythonError();
- PyObject *dict = PyModule_GetDict(module); // this is a borrowed reference and does not need to be released
- script.set(PyDict_GetItemString(dict, funcname));
- checkPythonError();
- if (!script || !PyCallable_Check(script))
- rtlFail(0, "Object is not callable");
- prevtext.set(text);
- }
- return script.getLink();
- }
- inline PyObject *compileEmbeddedScript(size32_t lenChars, const char *utf)
- {
- size32_t bytes = rtlUtf8Size(lenChars, utf);
- StringBuffer text(bytes, utf);
- if (!prevtext || strcmp(text, prevtext) != 0)
- {
- prevtext.clear();
- // Try compiling as a eval first... if that fails, try as a script.
- script.setown(Py_CompileString(text, "", Py_eval_input));
- if (!script)
- {
- PyErr_Clear();
- StringBuffer wrapped;
- wrapPythonText(wrapped, text);
- script.setown(Py_CompileString(wrapped, "<embed>", Py_file_input));
- }
- checkPythonError();
- prevtext.set(text);
- }
- return script.getLink();
- }
- private:
- static StringBuffer &wrapPythonText(StringBuffer &out, const char *in)
- {
- out.append("def __user__():\n ");
- char c;
- while ((c = *in++) != '\0')
- {
- out.append(c);
- if (c=='\n')
- out.append(" ");
- }
- out.append("\n__result__ = __user__()\n");
- return out;
- }
- GILstateWrapper GILState;
- OwnedPyObject module;
- OwnedPyObject script;
- StringAttr prevtext;
- };
- static __thread PythonThreadContext* threadContext; // We reuse per thread, for speed
- static __thread ThreadTermFunc threadHookChain;
- static void releaseContext()
- {
- delete threadContext;
- threadContext = NULL;
- if (threadHookChain)
- (*threadHookChain)();
- }
- // Use a global object to ensure that the Python interpreter is initialized on main thread
- static class Python27GlobalState
- {
- public:
- Python27GlobalState()
- {
- // Initialize the Python Interpreter
- Py_Initialize();
- PyEval_InitThreads();
- tstate = PyEval_SaveThread();
- }
- ~Python27GlobalState()
- {
- if (threadContext)
- delete threadContext; // The one on the main thread won't get picked up by the thread hook mechanism
- threadContext = NULL;
- PyEval_RestoreThread(tstate);
- // Finish the Python Interpreter
- Py_Finalize();
- }
- protected:
- PyThreadState *tstate;
- } globalState;
- // Each call to a Python function will use a new Python27EmbedFunctionContext object
- // This takes care of ensuring that the Python GIL is locked while we are executing python code,
- // and released when we are not
- class Python27EmbedContextBase : public CInterfaceOf<IEmbedFunctionContext>
- {
- public:
- Python27EmbedContextBase(PythonThreadContext *_sharedCtx)
- : sharedCtx(_sharedCtx)
- {
- PyEval_RestoreThread(sharedCtx->threadState);
- locals.setown(PyDict_New());
- globals.setown(PyDict_New());
- PyDict_SetItemString(locals, "__builtins__", PyEval_GetBuiltins()); // required for import to work
- }
- ~Python27EmbedContextBase()
- {
- // We need to clear these before calling savethread, or we won't own the GIL
- locals.clear();
- globals.clear();
- result.clear();
- script.clear();
- sharedCtx->threadState = PyEval_SaveThread();
- }
- virtual bool getBooleanResult()
- {
- assertex(result);
- if (!PyBool_Check(result))
- throw MakeStringException(MSGAUD_user, 0, "pyembed: Type mismatch on result");
- return result == Py_True;
- }
- virtual double getRealResult()
- {
- assertex(result && result != Py_None);
- return (__int64) PyFloat_AsDouble(result);
- }
- virtual __int64 getSignedResult()
- {
- assertex(result && result != Py_None);
- return (__int64) PyLong_AsLongLong(result);
- }
- virtual unsigned __int64 getUnsignedResult()
- {
- assertex(result && result != Py_None);
- return (__int64) PyLong_AsUnsignedLongLong(result);
- }
- virtual void getStringResult(size32_t &__chars, char * &__result)
- {
- assertex(result && result != Py_None);
- if (PyString_Check(result))
- {
- const char * text = PyString_AsString(result);
- checkPythonError();
- size_t lenBytes = PyString_Size(result);
- rtlStrToStrX(__chars, __result, lenBytes, text);
- }
- else
- rtlFail(0, "Python type mismatch - return value was not a string");
- }
- virtual void getUTF8Result(size32_t &__chars, char * &__result)
- {
- assertex(result && result != Py_None);
- if (PyUnicode_Check(result))
- {
- OwnedPyObject utf8 = PyUnicode_AsUTF8String(result);
- checkPythonError();
- size_t lenBytes = PyString_Size(utf8);
- const char * text = PyString_AsString(utf8);
- checkPythonError();
- size32_t numchars = rtlUtf8Length(lenBytes, text);
- rtlUtf8ToUtf8X(__chars, __result, numchars, text);
- }
- else
- rtlFail(0, "Python type mismatch - return value was not a unicode string");
- }
- virtual void getUnicodeResult(size32_t &__chars, UChar * &__result)
- {
- assertex(result && result != Py_None);
- if (PyUnicode_Check(result))
- {
- OwnedPyObject utf8 = PyUnicode_AsUTF8String(result);
- checkPythonError();
- size_t lenBytes = PyString_Size(utf8);
- const char * text = PyString_AsString(utf8);
- checkPythonError();
- size32_t numchars = rtlUtf8Length(lenBytes, text);
- rtlUtf8ToUnicodeX(__chars, __result, numchars, text);
- }
- else
- rtlFail(0, "Python type mismatch - return value was not a unicode string");
- }
- protected:
- PythonThreadContext *sharedCtx;
- OwnedPyObject locals;
- OwnedPyObject globals;
- OwnedPyObject result;
- OwnedPyObject script;
- };
- class Python27EmbedScriptContext : public Python27EmbedContextBase
- {
- public:
- Python27EmbedScriptContext(PythonThreadContext *_sharedCtx, const char *options)
- : Python27EmbedContextBase(_sharedCtx)
- {
- }
- ~Python27EmbedScriptContext()
- {
- }
- virtual void bindBooleanParam(const char *name, bool val)
- {
- OwnedPyObject vval = PyBool_FromLong(val ? 1 : 0);
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindRealParam(const char *name, double val)
- {
- OwnedPyObject vval = PyFloat_FromDouble(val);
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindSignedParam(const char *name, __int64 val)
- {
- OwnedPyObject vval = PyLong_FromLongLong(val);
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindUnsignedParam(const char *name, unsigned __int64 val)
- {
- OwnedPyObject vval = PyLong_FromUnsignedLongLong(val);
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindStringParam(const char *name, size32_t len, const char *val)
- {
- OwnedPyObject vval = PyString_FromStringAndSize(val, len);
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindVStringParam(const char *name, const char *val)
- {
- OwnedPyObject vval = PyString_FromString(val);
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindUTF8Param(const char *name, size32_t chars, const char *val)
- {
- size32_t sizeBytes = rtlUtf8Size(chars, val);
- OwnedPyObject vval = PyUnicode_FromStringAndSize(val, sizeBytes); // NOTE - requires size in bytes not chars
- PyDict_SetItemString(locals, name, vval);
- }
- virtual void bindUnicodeParam(const char *name, size32_t chars, const UChar *val)
- {
- // You don't really know what size Py_UNICODE is (varies from system to system), so go via utf8
- unsigned unicodeChars;
- char *unicode;
- rtlUnicodeToUtf8X(unicodeChars, unicode, chars, val);
- size32_t sizeBytes = rtlUtf8Size(unicodeChars, unicode);
- OwnedPyObject vval = PyUnicode_FromStringAndSize(unicode, sizeBytes); // NOTE - requires size in bytes not chars
- checkPythonError();
- PyDict_SetItemString(locals, name, vval);
- rtlFree(unicode);
- }
- virtual void importFunction(size32_t lenChars, const char *text)
- {
- throwUnexpected();
- }
- virtual void compileEmbeddedScript(size32_t lenChars, const char *utf)
- {
- script.setown(sharedCtx->compileEmbeddedScript(lenChars, utf));
- }
- virtual void callFunction()
- {
- result.setown(PyEval_EvalCode((PyCodeObject *) script.get(), locals, globals));
- checkPythonError();
- if (!result || result == Py_None)
- result.set(PyDict_GetItemString(locals, "__result__"));
- if (!result || result == Py_None)
- result.set(PyDict_GetItemString(globals, "__result__"));
- }
- };
- class Python27EmbedImportContext : public Python27EmbedContextBase
- {
- public:
- Python27EmbedImportContext(PythonThreadContext *_sharedCtx, const char *options)
- : Python27EmbedContextBase(_sharedCtx)
- {
- argcount = 0;
- }
- ~Python27EmbedImportContext()
- {
- }
- virtual void bindBooleanParam(const char *name, bool val)
- {
- addArg(PyBool_FromLong(val ? 1 : 0));
- }
- virtual void bindRealParam(const char *name, double val)
- {
- addArg(PyFloat_FromDouble(val));
- }
- virtual void bindSignedParam(const char *name, __int64 val)
- {
- addArg(PyLong_FromLongLong(val));
- }
- virtual void bindUnsignedParam(const char *name, unsigned __int64 val)
- {
- addArg(PyLong_FromUnsignedLongLong(val));
- }
- virtual void bindStringParam(const char *name, size32_t len, const char *val)
- {
- addArg(PyString_FromStringAndSize(val, len));
- }
- virtual void bindVStringParam(const char *name, const char *val)
- {
- addArg(PyString_FromString(val));
- }
- virtual void bindUTF8Param(const char *name, size32_t chars, const char *val)
- {
- size32_t sizeBytes = rtlUtf8Size(chars, val);
- addArg(PyUnicode_FromStringAndSize(val, sizeBytes)); // NOTE - requires size in bytes not chars
- }
- virtual void bindUnicodeParam(const char *name, size32_t chars, const UChar *val)
- {
- // You don't really know what size Py_UNICODE is (varies from system to system), so go via utf8
- unsigned unicodeChars;
- char *unicode;
- rtlUnicodeToUtf8X(unicodeChars, unicode, chars, val);
- size32_t sizeBytes = rtlUtf8Size(unicodeChars, unicode);
- PyObject *vval = PyUnicode_FromStringAndSize(unicode, sizeBytes); // NOTE - requires size in bytes not chars
- checkPythonError();
- addArg(vval);
- rtlFree(unicode);
- }
- virtual void importFunction(size32_t lenChars, const char *utf)
- {
- script.setown(sharedCtx->importFunction(lenChars, utf));
- }
- virtual void compileEmbeddedScript(size32_t len, const char *text)
- {
- throwUnexpected();
- }
- virtual void callFunction()
- {
- result.setown(PyObject_CallObject(script, args));
- checkPythonError();
- }
- private:
- void addArg(PyObject *arg)
- {
- if (argcount)
- _PyTuple_Resize(args.ref(), argcount+1);
- else
- args.setown(PyTuple_New(1));
- PyTuple_SET_ITEM((PyTupleObject *) args.get(), argcount++, arg); // Note - 'steals' the arg reference
- }
- int argcount;
- OwnedPyObject args;
- };
- class Python27EmbedContext : public CInterfaceOf<IEmbedContext>
- {
- public:
- virtual IEmbedFunctionContext *createFunctionContext(bool isImport, const char *options)
- {
- if (!threadContext)
- {
- threadContext = new PythonThreadContext;
- threadHookChain = addThreadTermFunc(releaseContext);
- }
- if (isImport)
- return new Python27EmbedImportContext(threadContext, options);
- else
- return new Python27EmbedScriptContext(threadContext, options);
- }
- };
- extern IEmbedContext* getEmbedContext()
- {
- return new Python27EmbedContext;
- }
- extern bool syntaxCheck(const char *script)
- {
- return true; // MORE
- }
- } // namespace
|