pyembed.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "Python.h"
  15. #include "jexcept.hpp"
  16. #include "jthread.hpp"
  17. #include "hqlplugins.hpp"
  18. #include "deftype.hpp"
  19. #include "eclrtl.hpp"
  20. #include "eclrtl_imp.hpp"
  21. #ifdef _WIN32
  22. #define EXPORT __declspec(dllexport)
  23. #else
  24. #define EXPORT
  25. #endif
  26. static const char * compatibleVersions[] = {
  27. "Python2.7 Embed Helper 1.0.0",
  28. NULL };
  29. static const char *version = "Python2.7 Embed Helper 1.0.0";
  30. extern "C" EXPORT bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
  31. {
  32. if (pb->size == sizeof(ECLPluginDefinitionBlockEx))
  33. {
  34. ECLPluginDefinitionBlockEx * pbx = (ECLPluginDefinitionBlockEx *) pb;
  35. pbx->compatibleVersions = compatibleVersions;
  36. }
  37. else if (pb->size != sizeof(ECLPluginDefinitionBlock))
  38. return false;
  39. pb->magicVersion = PLUGIN_VERSION;
  40. pb->version = version;
  41. pb->moduleName = "python";
  42. pb->ECL = NULL;
  43. pb->flags = PLUGIN_MULTIPLE_VERSIONS;
  44. pb->description = "Python2.7 Embed Helper";
  45. return true;
  46. }
  47. namespace pyembed {
  48. // Use class OwnedPyObject for any objects that are not 'borrowed references'
  49. // so that the appropriate Py_DECREF call is made when the OwnedPyObject goes
  50. // out of scope, even if the function returns prematurely (such as via an exception).
  51. // In particular, checkPythonError is a lot easier to call safely if this is used.
  52. class OwnedPyObject
  53. {
  54. PyObject *ptr;
  55. public:
  56. inline OwnedPyObject() : ptr(NULL) {}
  57. inline OwnedPyObject(PyObject *_ptr) : ptr(_ptr) {}
  58. inline ~OwnedPyObject() { if (ptr) Py_DECREF(ptr); }
  59. inline PyObject * get() const { return ptr; }
  60. inline PyObject * operator -> () const { return ptr; }
  61. inline operator PyObject *() const { return ptr; }
  62. inline void clear() { if (ptr) Py_DECREF(ptr); ptr = NULL; }
  63. inline void setown(PyObject *_ptr) { clear(); ptr = _ptr; }
  64. inline void set(PyObject *_ptr) { clear(); ptr = _ptr; if (ptr) Py_INCREF(ptr);}
  65. inline PyObject *getLink() { if (ptr) Py_INCREF(ptr); return ptr;}
  66. inline PyObject **ref() { return &ptr; }
  67. };
  68. // call checkPythonError to throw an exception if Python error state is set
  69. static void checkPythonError()
  70. {
  71. PyObject* err = PyErr_Occurred();
  72. if (err)
  73. {
  74. OwnedPyObject pType, pValue, pTraceBack;
  75. PyErr_Fetch(pType.ref(), pValue.ref(), pTraceBack.ref());
  76. OwnedPyObject valStr = PyObject_Str(pValue);
  77. PyErr_Clear();
  78. VStringBuffer errMessage("pyembed: %s", PyString_AsString(valStr));
  79. rtlFail(0, errMessage.str());
  80. }
  81. }
  82. // The Python Global Interpreter Lock (GIL) won't know about C++-created threads, so we need to
  83. // call PyGILState_Ensure() and PyGILState_Release at the start and end of every function.
  84. // Wrapping them in a class like this ensures that the release always happens even if
  85. // the function exists prematurely
  86. class GILstateWrapper
  87. {
  88. PyGILState_STATE gstate;
  89. public:
  90. GILstateWrapper()
  91. {
  92. gstate = PyGILState_Ensure();
  93. }
  94. ~GILstateWrapper()
  95. {
  96. PyGILState_Release(gstate);
  97. }
  98. };
  99. // There is a singleton PythonThreadContext per thread. This allows us to
  100. // ensure that we can make repeated calls to a Python function efficiently.
  101. class PythonThreadContext
  102. {
  103. public:
  104. PyThreadState *threadState;
  105. public:
  106. PythonThreadContext()
  107. {
  108. threadState = PyEval_SaveThread();
  109. }
  110. ~PythonThreadContext()
  111. {
  112. PyEval_RestoreThread(threadState);
  113. script.clear();
  114. }
  115. inline PyObject * importFunction(size32_t lenChars, const char *utf)
  116. {
  117. size32_t bytes = rtlUtf8Size(lenChars, utf);
  118. StringBuffer text(bytes, utf);
  119. if (!prevtext || strcmp(text, prevtext) != 0)
  120. {
  121. prevtext.clear();
  122. // Name should be in the form module.function
  123. const char *funcname = strrchr(text, '.');
  124. if (!funcname)
  125. rtlFail(0, "pyembed: Expected module.function");
  126. StringBuffer modname(funcname-text, text);
  127. funcname++; // skip the '.'
  128. // If the modname is preceded by a path, add it to the python path before importing
  129. const char *pathsep = strrchr(modname, PATHSEPCHAR);
  130. if (pathsep)
  131. {
  132. StringBuffer path(pathsep-modname, modname);
  133. modname.remove(0, 1+pathsep-modname);
  134. PyObject *sys_path = PySys_GetObject((char *) "path");
  135. OwnedPyObject new_path = PyString_FromString(path);
  136. if (sys_path)
  137. {
  138. PyList_Insert(sys_path, 0, new_path);
  139. checkPythonError();
  140. }
  141. }
  142. module.setown(PyImport_ImportModule(modname));
  143. checkPythonError();
  144. PyObject *dict = PyModule_GetDict(module); // this is a borrowed reference and does not need to be released
  145. script.set(PyDict_GetItemString(dict, funcname));
  146. checkPythonError();
  147. if (!script || !PyCallable_Check(script))
  148. rtlFail(0, "pyembed: Object is not callable");
  149. prevtext.set(text);
  150. }
  151. return script.getLink();
  152. }
  153. inline PyObject *compileEmbeddedScript(size32_t lenChars, const char *utf)
  154. {
  155. size32_t bytes = rtlUtf8Size(lenChars, utf);
  156. StringBuffer text(bytes, utf);
  157. if (!prevtext || strcmp(text, prevtext) != 0)
  158. {
  159. prevtext.clear();
  160. // Try compiling as a eval first... if that fails, try as a script.
  161. text.stripChar('\r');
  162. script.setown(Py_CompileString(text, "", Py_eval_input));
  163. if (!script)
  164. {
  165. PyErr_Clear();
  166. StringBuffer wrapped;
  167. wrapPythonText(wrapped, text);
  168. script.setown(Py_CompileString(wrapped, "<embed>", Py_file_input));
  169. }
  170. checkPythonError();
  171. prevtext.set(utf, bytes);
  172. }
  173. return script.getLink();
  174. }
  175. private:
  176. static StringBuffer &wrapPythonText(StringBuffer &out, const char *in)
  177. {
  178. out.append("def __user__():\n ");
  179. char c;
  180. while ((c = *in++) != '\0')
  181. {
  182. out.append(c);
  183. if (c=='\n')
  184. out.append(" ");
  185. }
  186. out.append("\n__result__ = __user__()\n");
  187. return out;
  188. }
  189. GILstateWrapper GILState;
  190. OwnedPyObject module;
  191. OwnedPyObject script;
  192. StringAttr prevtext;
  193. };
  194. static __thread PythonThreadContext* threadContext; // We reuse per thread, for speed
  195. static __thread ThreadTermFunc threadHookChain;
  196. static void releaseContext()
  197. {
  198. if (threadContext)
  199. {
  200. delete threadContext;
  201. threadContext = NULL;
  202. }
  203. if (threadHookChain)
  204. {
  205. (*threadHookChain)();
  206. threadHookChain = NULL;
  207. }
  208. }
  209. // Use a global object to ensure that the Python interpreter is initialized on main thread
  210. static class Python27GlobalState
  211. {
  212. public:
  213. Python27GlobalState()
  214. {
  215. #ifndef _WIN32
  216. // If Py_Initialize is called when stdin is set to a directory, it calls exit()
  217. // We don't want that to happen - just disable Python support in such situations
  218. struct stat sb;
  219. if (fstat(fileno(stdin), &sb) == 0 && S_ISDIR(sb.st_mode))
  220. {
  221. initialized = false;
  222. return;
  223. }
  224. #endif
  225. // Initialize the Python Interpreter
  226. Py_Initialize();
  227. PyEval_InitThreads();
  228. tstate = PyEval_SaveThread();
  229. initialized = true;
  230. }
  231. ~Python27GlobalState()
  232. {
  233. if (threadContext)
  234. delete threadContext; // The one on the main thread won't get picked up by the thread hook mechanism
  235. threadContext = NULL;
  236. if (initialized)
  237. {
  238. PyEval_RestoreThread(tstate);
  239. // Finish the Python Interpreter
  240. Py_Finalize();
  241. }
  242. }
  243. bool isInitialized()
  244. {
  245. return initialized;
  246. }
  247. protected:
  248. PyThreadState *tstate;
  249. bool initialized;
  250. } globalState;
  251. // Each call to a Python function will use a new Python27EmbedFunctionContext object
  252. // This takes care of ensuring that the Python GIL is locked while we are executing python code,
  253. // and released when we are not
  254. class Python27EmbedContextBase : public CInterfaceOf<IEmbedFunctionContext>
  255. {
  256. public:
  257. Python27EmbedContextBase(PythonThreadContext *_sharedCtx)
  258. : sharedCtx(_sharedCtx)
  259. {
  260. PyEval_RestoreThread(sharedCtx->threadState);
  261. locals.setown(PyDict_New());
  262. globals.setown(PyDict_New());
  263. PyDict_SetItemString(locals, "__builtins__", PyEval_GetBuiltins()); // required for import to work
  264. }
  265. ~Python27EmbedContextBase()
  266. {
  267. // We need to clear these before calling savethread, or we won't own the GIL
  268. locals.clear();
  269. globals.clear();
  270. result.clear();
  271. script.clear();
  272. sharedCtx->threadState = PyEval_SaveThread();
  273. }
  274. virtual bool getBooleanResult()
  275. {
  276. assertex(result && result != Py_None);
  277. if (!PyBool_Check(result))
  278. rtlFail(0, "pyembed: Type mismatch on result - value is not BOOLEAN ");
  279. return result == Py_True;
  280. }
  281. virtual void getDataResult(size32_t &__chars, void * &__result)
  282. {
  283. assertex(result && result != Py_None);
  284. if (!PyByteArray_Check(result))
  285. rtlFail(0, "pyembed: Type mismatch on result - value is not a bytearray");
  286. rtlStrToDataX(__chars, __result, PyByteArray_Size(result), PyByteArray_AsString(result));
  287. }
  288. virtual double getRealResult()
  289. {
  290. assertex(result && result != Py_None);
  291. return PyFloat_AsDouble(result);
  292. }
  293. virtual __int64 getSignedResult()
  294. {
  295. assertex(result && result != Py_None);
  296. return (__int64) PyLong_AsLongLong(result);
  297. }
  298. virtual unsigned __int64 getUnsignedResult()
  299. {
  300. assertex(result && result != Py_None);
  301. return (__int64) PyLong_AsUnsignedLongLong(result);
  302. }
  303. virtual void getStringResult(size32_t &__chars, char * &__result)
  304. {
  305. assertex(result && result != Py_None);
  306. if (PyString_Check(result))
  307. {
  308. const char * text = PyString_AsString(result);
  309. checkPythonError();
  310. size_t lenBytes = PyString_Size(result);
  311. rtlStrToStrX(__chars, __result, lenBytes, text);
  312. }
  313. else
  314. rtlFail(0, "pyembed: type mismatch - return value was not a string");
  315. }
  316. virtual void getUTF8Result(size32_t &__chars, char * &__result)
  317. {
  318. assertex(result && result != Py_None);
  319. if (PyUnicode_Check(result))
  320. {
  321. OwnedPyObject utf8 = PyUnicode_AsUTF8String(result);
  322. checkPythonError();
  323. size_t lenBytes = PyString_Size(utf8);
  324. const char * text = PyString_AsString(utf8);
  325. checkPythonError();
  326. size32_t numchars = rtlUtf8Length(lenBytes, text);
  327. rtlUtf8ToUtf8X(__chars, __result, numchars, text);
  328. }
  329. else
  330. rtlFail(0, "pyembed: type mismatch - return value was not a unicode string");
  331. }
  332. virtual void getUnicodeResult(size32_t &__chars, UChar * &__result)
  333. {
  334. assertex(result && result != Py_None);
  335. if (PyUnicode_Check(result))
  336. {
  337. OwnedPyObject utf8 = PyUnicode_AsUTF8String(result);
  338. checkPythonError();
  339. size_t lenBytes = PyString_Size(utf8);
  340. const char * text = PyString_AsString(utf8);
  341. checkPythonError();
  342. size32_t numchars = rtlUtf8Length(lenBytes, text);
  343. rtlUtf8ToUnicodeX(__chars, __result, numchars, text);
  344. }
  345. else
  346. rtlFail(0, "pyembed: type mismatch - return value was not a unicode string");
  347. }
  348. virtual void getSetResult(bool & __isAllResult, size32_t & __resultBytes, void * & __result, int elemType, size32_t elemSize)
  349. {
  350. assertex(result && result != Py_None);
  351. if (!PyList_Check(result))
  352. rtlFail(0, "pyembed: type mismatch - return value was not a list");
  353. Py_ssize_t numResults = PyList_Size(result);
  354. rtlRowBuilder out;
  355. byte *outData = NULL;
  356. size32_t outBytes = 0;
  357. if (elemSize != UNKNOWN_LENGTH)
  358. {
  359. out.ensureAvailable(numResults * elemSize); // MORE - check for overflow?
  360. outData = out.getbytes();
  361. }
  362. for (int i = 0; i < numResults; i++)
  363. {
  364. PyObject *elem = PyList_GetItem(result, i); // note - borrowed reference
  365. switch ((type_t) elemType)
  366. {
  367. case type_int:
  368. rtlWriteInt(outData, PyLong_AsLongLong(elem), elemSize);
  369. break;
  370. case type_unsigned:
  371. rtlWriteInt(outData, PyLong_AsUnsignedLongLong(elem), elemSize);
  372. break;
  373. case type_real:
  374. if (!PyFloat_Check(elem))
  375. rtlFail(0, "pyembed: type mismatch - return value in list was not a REAL");
  376. if (elemSize == sizeof(double))
  377. * (double *) outData = (double) PyFloat_AsDouble(elem);
  378. else
  379. {
  380. assertex(elemSize == sizeof(float));
  381. * (float *) outData = (float) PyFloat_AsDouble(elem);
  382. }
  383. break;
  384. case type_boolean:
  385. assertex(elemSize == sizeof(bool));
  386. if (!PyBool_Check(elem))
  387. rtlFail(0, "pyembed: type mismatch - return value in list was not a BOOLEAN");
  388. * (bool *) outData = (result == Py_True);
  389. break;
  390. case type_string:
  391. case type_varstring:
  392. {
  393. if (!PyString_Check(elem))
  394. rtlFail(0, "pyembed: type mismatch - return value in list was not a STRING");
  395. const char * text = PyString_AsString(elem);
  396. checkPythonError();
  397. size_t lenBytes = PyString_Size(elem);
  398. if (elemSize == UNKNOWN_LENGTH)
  399. {
  400. if (elemType == type_string)
  401. {
  402. out.ensureAvailable(outBytes + lenBytes + sizeof(size32_t));
  403. outData = out.getbytes() + outBytes;
  404. * (size32_t *) outData = lenBytes;
  405. rtlStrToStr(lenBytes, outData+sizeof(size32_t), lenBytes, text);
  406. outBytes += lenBytes + sizeof(size32_t);
  407. }
  408. else
  409. {
  410. out.ensureAvailable(outBytes + lenBytes + 1);
  411. outData = out.getbytes() + outBytes;
  412. rtlStrToVStr(0, outData, lenBytes, text);
  413. outBytes += lenBytes + 1;
  414. }
  415. }
  416. else
  417. {
  418. if (elemType == type_string)
  419. rtlStrToStr(elemSize, outData, lenBytes, text);
  420. else
  421. rtlStrToVStr(elemSize, outData, lenBytes, text); // Fixed size null terminated strings... weird.
  422. }
  423. break;
  424. }
  425. case type_unicode:
  426. case type_utf8:
  427. {
  428. if (!PyUnicode_Check(elem))
  429. rtlFail(0, "pyembed: type mismatch - return value in list was not a unicode STRING");
  430. OwnedPyObject utf8 = PyUnicode_AsUTF8String(elem);
  431. checkPythonError();
  432. size_t lenBytes = PyString_Size(utf8);
  433. const char * text = PyString_AsString(utf8);
  434. checkPythonError();
  435. size32_t numchars = rtlUtf8Length(lenBytes, text);
  436. if (elemType == type_utf8)
  437. {
  438. assertex (elemSize == UNKNOWN_LENGTH);
  439. out.ensureAvailable(outBytes + lenBytes + sizeof(size32_t));
  440. outData = out.getbytes() + outBytes;
  441. * (size32_t *) outData = numchars;
  442. rtlStrToStr(lenBytes, outData+sizeof(size32_t), lenBytes, text);
  443. outBytes += lenBytes + sizeof(size32_t);
  444. }
  445. else
  446. {
  447. if (elemSize == UNKNOWN_LENGTH)
  448. {
  449. out.ensureAvailable(outBytes + numchars*sizeof(UChar) + sizeof(size32_t));
  450. outData = out.getbytes() + outBytes;
  451. // You can't assume that number of chars in utf8 matches number in unicode16 ...
  452. size32_t numchars16;
  453. rtlDataAttr unicode16;
  454. rtlUtf8ToUnicodeX(numchars16, unicode16.refustr(), numchars, text);
  455. * (size32_t *) outData = numchars16;
  456. rtlUnicodeToUnicode(numchars16, (UChar *) (outData+sizeof(size32_t)), numchars16, unicode16.getustr());
  457. outBytes += numchars16*sizeof(UChar) + sizeof(size32_t);
  458. }
  459. else
  460. rtlUtf8ToUnicode(elemSize / sizeof(UChar), (UChar *) outData, numchars, text);
  461. }
  462. break;
  463. }
  464. case type_data:
  465. {
  466. if (!PyByteArray_Check(elem))
  467. rtlFail(0, "pyembed: type mismatch - return value in list was not a bytearray");
  468. size_t lenBytes = PyByteArray_Size(elem); // Could check does not overflow size32_t
  469. const char *data = PyByteArray_AsString(elem);
  470. if (elemSize == UNKNOWN_LENGTH)
  471. {
  472. out.ensureAvailable(outBytes + lenBytes + sizeof(size32_t));
  473. outData = out.getbytes() + outBytes;
  474. * (size32_t *) outData = lenBytes;
  475. rtlStrToData(lenBytes, outData+sizeof(size32_t), lenBytes, data);
  476. outBytes += lenBytes + sizeof(size32_t);
  477. }
  478. else
  479. rtlStrToData(elemSize, outData, lenBytes, data);
  480. break;
  481. }
  482. default:
  483. rtlFail(0, "pyembed: type mismatch - unsupported return type");
  484. }
  485. checkPythonError();
  486. if (elemSize != UNKNOWN_LENGTH)
  487. {
  488. outData += elemSize;
  489. outBytes += elemSize;
  490. }
  491. }
  492. __isAllResult = false;
  493. __resultBytes = outBytes;
  494. __result = out.detachdata();
  495. }
  496. virtual void bindBooleanParam(const char *name, bool val)
  497. {
  498. addArg(name, PyBool_FromLong(val ? 1 : 0));
  499. }
  500. virtual void bindDataParam(const char *name, size32_t len, const void *val)
  501. {
  502. addArg(name, PyByteArray_FromStringAndSize((const char *) val, len));
  503. }
  504. virtual void bindRealParam(const char *name, double val)
  505. {
  506. addArg(name, PyFloat_FromDouble(val));
  507. }
  508. virtual void bindSignedParam(const char *name, __int64 val)
  509. {
  510. addArg(name, PyLong_FromLongLong(val));
  511. }
  512. virtual void bindUnsignedParam(const char *name, unsigned __int64 val)
  513. {
  514. addArg(name, PyLong_FromUnsignedLongLong(val));
  515. }
  516. virtual void bindStringParam(const char *name, size32_t len, const char *val)
  517. {
  518. addArg(name, PyString_FromStringAndSize(val, len));
  519. }
  520. virtual void bindVStringParam(const char *name, const char *val)
  521. {
  522. addArg(name, PyString_FromString(val));
  523. }
  524. virtual void bindUTF8Param(const char *name, size32_t chars, const char *val)
  525. {
  526. size32_t sizeBytes = rtlUtf8Size(chars, val);
  527. PyObject *vval = PyUnicode_FromStringAndSize(val, sizeBytes); // NOTE - requires size in bytes not chars
  528. checkPythonError();
  529. addArg(name, vval);
  530. }
  531. virtual void bindUnicodeParam(const char *name, size32_t chars, const UChar *val)
  532. {
  533. // You don't really know what size Py_UNICODE is (varies from system to system), so go via utf8
  534. unsigned unicodeChars;
  535. char *unicode;
  536. rtlUnicodeToUtf8X(unicodeChars, unicode, chars, val);
  537. size32_t sizeBytes = rtlUtf8Size(unicodeChars, unicode);
  538. PyObject *vval = PyUnicode_FromStringAndSize(unicode, sizeBytes); // NOTE - requires size in bytes not chars
  539. checkPythonError();
  540. addArg(name, vval);
  541. rtlFree(unicode);
  542. }
  543. virtual void bindSetParam(const char *name, int elemType, size32_t elemSize, bool isAll, size32_t totalBytes, void *setData)
  544. {
  545. if (isAll)
  546. rtlFail(0, "pyembed: Cannot pass ALL");
  547. type_t typecode = (type_t) elemType;
  548. const byte *inData = (const byte *) setData;
  549. const byte *endData = inData + totalBytes;
  550. OwnedPyObject vval = PyList_New(0);
  551. while (inData < endData)
  552. {
  553. OwnedPyObject thisElem;
  554. size32_t thisSize = elemSize;
  555. switch (typecode)
  556. {
  557. case type_int:
  558. thisElem.setown(PyLong_FromLongLong(rtlReadInt(inData, elemSize)));
  559. break;
  560. case type_unsigned:
  561. thisElem.setown(PyLong_FromUnsignedLongLong(rtlReadUInt(inData, elemSize)));
  562. break;
  563. case type_varstring:
  564. {
  565. size32_t numChars = strlen((const char *) inData);
  566. thisElem.setown(PyString_FromStringAndSize((const char *) inData, numChars));
  567. if (elemSize == UNKNOWN_LENGTH)
  568. thisSize = numChars + 1;
  569. break;
  570. }
  571. case type_string:
  572. if (elemSize == UNKNOWN_LENGTH)
  573. {
  574. thisSize = * (size32_t *) inData;
  575. inData += sizeof(size32_t);
  576. }
  577. thisElem.setown(PyString_FromStringAndSize((const char *) inData, thisSize));
  578. break;
  579. case type_real:
  580. if (elemSize == sizeof(double))
  581. thisElem.setown(PyFloat_FromDouble(* (double *) inData));
  582. else
  583. thisElem.setown(PyFloat_FromDouble(* (float *) inData));
  584. break;
  585. case type_boolean:
  586. assertex(elemSize == sizeof(bool));
  587. thisElem.setown(PyBool_FromLong(*(bool*)inData ? 1 : 0));
  588. break;
  589. case type_unicode:
  590. {
  591. if (elemSize == UNKNOWN_LENGTH)
  592. {
  593. thisSize = (* (size32_t *) inData) * sizeof(UChar); // NOTE - it's in chars...
  594. inData += sizeof(size32_t);
  595. }
  596. unsigned unicodeChars;
  597. rtlDataAttr unicode;
  598. rtlUnicodeToUtf8X(unicodeChars, unicode.refstr(), thisSize / sizeof(UChar), (const UChar *) inData);
  599. size32_t sizeBytes = rtlUtf8Size(unicodeChars, unicode.getstr());
  600. thisElem.setown(PyUnicode_FromStringAndSize(unicode.getstr(), sizeBytes)); // NOTE - requires size in bytes not chars
  601. checkPythonError();
  602. break;
  603. }
  604. case type_utf8:
  605. {
  606. assertex (elemSize == UNKNOWN_LENGTH);
  607. size32_t numChars = * (size32_t *) inData;
  608. inData += sizeof(size32_t);
  609. thisSize = rtlUtf8Size(numChars, inData);
  610. thisElem.setown(PyUnicode_FromStringAndSize((const char *) inData, thisSize)); // NOTE - requires size in bytes not chars
  611. break;
  612. }
  613. case type_data:
  614. if (elemSize == UNKNOWN_LENGTH)
  615. {
  616. thisSize = * (size32_t *) inData;
  617. inData += sizeof(size32_t);
  618. }
  619. thisElem.setown(PyByteArray_FromStringAndSize((const char *) inData, thisSize));
  620. break;
  621. }
  622. checkPythonError();
  623. inData += thisSize;
  624. PyList_Append(vval, thisElem);
  625. }
  626. addArg(name, vval.getLink());
  627. }
  628. protected:
  629. virtual void addArg(const char *name, PyObject *arg) = 0;
  630. PythonThreadContext *sharedCtx;
  631. OwnedPyObject locals;
  632. OwnedPyObject globals;
  633. OwnedPyObject result;
  634. OwnedPyObject script;
  635. };
  636. class Python27EmbedScriptContext : public Python27EmbedContextBase
  637. {
  638. public:
  639. Python27EmbedScriptContext(PythonThreadContext *_sharedCtx, const char *options)
  640. : Python27EmbedContextBase(_sharedCtx)
  641. {
  642. }
  643. ~Python27EmbedScriptContext()
  644. {
  645. }
  646. virtual void importFunction(size32_t lenChars, const char *text)
  647. {
  648. throwUnexpected();
  649. }
  650. virtual void compileEmbeddedScript(size32_t lenChars, const char *utf)
  651. {
  652. script.setown(sharedCtx->compileEmbeddedScript(lenChars, utf));
  653. }
  654. virtual void callFunction()
  655. {
  656. result.setown(PyEval_EvalCode((PyCodeObject *) script.get(), locals, globals));
  657. checkPythonError();
  658. if (!result || result == Py_None)
  659. result.set(PyDict_GetItemString(locals, "__result__"));
  660. if (!result || result == Py_None)
  661. result.set(PyDict_GetItemString(globals, "__result__"));
  662. }
  663. protected:
  664. virtual void addArg(const char *name, PyObject *arg)
  665. {
  666. assertex(arg);
  667. PyDict_SetItemString(locals, name, arg);
  668. Py_DECREF(arg);
  669. checkPythonError();
  670. }
  671. };
  672. class Python27EmbedImportContext : public Python27EmbedContextBase
  673. {
  674. public:
  675. Python27EmbedImportContext(PythonThreadContext *_sharedCtx, const char *options)
  676. : Python27EmbedContextBase(_sharedCtx)
  677. {
  678. argcount = 0;
  679. }
  680. ~Python27EmbedImportContext()
  681. {
  682. }
  683. virtual void importFunction(size32_t lenChars, const char *utf)
  684. {
  685. script.setown(sharedCtx->importFunction(lenChars, utf));
  686. }
  687. virtual void compileEmbeddedScript(size32_t len, const char *text)
  688. {
  689. throwUnexpected();
  690. }
  691. virtual void callFunction()
  692. {
  693. result.setown(PyObject_CallObject(script, args));
  694. checkPythonError();
  695. }
  696. private:
  697. virtual void addArg(const char *name, PyObject *arg)
  698. {
  699. if (argcount)
  700. _PyTuple_Resize(args.ref(), argcount+1);
  701. else
  702. args.setown(PyTuple_New(1));
  703. PyTuple_SET_ITEM((PyTupleObject *) args.get(), argcount++, arg); // Note - 'steals' the arg reference
  704. }
  705. int argcount;
  706. OwnedPyObject args;
  707. };
  708. class Python27EmbedContext : public CInterfaceOf<IEmbedContext>
  709. {
  710. public:
  711. virtual IEmbedFunctionContext *createFunctionContext(bool isImport, const char *options)
  712. {
  713. if (!threadContext)
  714. {
  715. if (!globalState.isInitialized())
  716. rtlFail(0, "Python not initialized");
  717. threadContext = new PythonThreadContext;
  718. threadHookChain = addThreadTermFunc(releaseContext);
  719. }
  720. if (isImport)
  721. return new Python27EmbedImportContext(threadContext, options);
  722. else
  723. return new Python27EmbedScriptContext(threadContext, options);
  724. }
  725. };
  726. extern IEmbedContext* getEmbedContext()
  727. {
  728. return new Python27EmbedContext;
  729. }
  730. extern bool syntaxCheck(const char *script)
  731. {
  732. return true; // MORE
  733. }
  734. } // namespace