subinterpreter.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <sys/queue.h>
  2. #include <Python.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #define MAX_INTERPRETER_NAME_LEN 100
  6. #ifdef PYTHON311OLDER
  7. PyObject *PyErr_GetRaisedException() {
  8. PyObject *type, *value, *traceback;
  9. PyErr_Fetch(&type, &value, &traceback);
  10. PyErr_NormalizeException(&type, &value, &traceback);
  11. return value;
  12. }
  13. #endif
  14. #define SUBINTERPRETER_SWITCH \
  15. struct interpr *sub_interpreter = get_interpreter(interpreter_name); \
  16. PyGILState_STATE gil = PyGILState_Ensure(); \
  17. if (NULL == sub_interpreter) { \
  18. PyErr_Format(PyExc_KeyError, "Subinterpreter '%s' does not exist.", \
  19. interpreter_name); \
  20. PyGILState_Release(gil); \
  21. return PyErr_GetRaisedException(); \
  22. } \
  23. PyThreadState *orig_tstate = PyThreadState_Get(); \
  24. PyThreadState_Swap(sub_interpreter->python_interpreter); \
  25. PyObject *ret = NULL; \
  26. PyObject *exception = PyErr_GetRaisedException(); \
  27. if (exception) \
  28. return exception;
  29. #define SETUP_RET \
  30. if (PyUnicode_GetLength(target_name) > 0) { \
  31. ret = Py_True; \
  32. PyObject_SetItem(global_dict, target_name, obj); \
  33. exception = PyErr_GetRaisedException(); \
  34. Py_DECREF(obj); \
  35. } else { \
  36. ret = obj; \
  37. }
  38. #define SUBINTERPRETER_RETURN \
  39. PyThreadState_Swap(orig_tstate); \
  40. PyGILState_Release(gil); \
  41. assert(ret || exception); \
  42. if (NULL == exception) { \
  43. return ret; \
  44. } else { \
  45. Py_XDECREF(ret); \
  46. return exception; \
  47. }
  48. // man 3 list
  49. struct interpr {
  50. PyThreadState *python_interpreter;
  51. PyObject *main_module;
  52. char *name;
  53. LIST_ENTRY(interpr) entries;
  54. };
  55. LIST_HEAD(listhead, interpr);
  56. struct listhead head;
  57. void init_interpreter_list() {
  58. LIST_INIT(&head);
  59. }
  60. struct interpr *get_interpreter(char *name) {
  61. struct interpr *ret = NULL;
  62. struct interpr *iter;
  63. LIST_FOREACH(iter, &head, entries) {
  64. if (0 == strncmp(iter->name, name, MAX_INTERPRETER_NAME_LEN)) {
  65. ret = iter;
  66. break;
  67. }
  68. }
  69. return ret;
  70. }
  71. PyObject *make_interpreter(char *interpreter_name) {
  72. PyGILState_STATE gil = PyGILState_Ensure();
  73. PyThreadState *tstate = NULL;
  74. PyThreadState *orig_tstate = PyThreadState_Get();
  75. struct interpr *maybe_existing_interpreter = get_interpreter(interpreter_name);
  76. if (NULL != maybe_existing_interpreter) {
  77. PyGILState_Release(gil);
  78. return Py_True;
  79. }
  80. unsigned int name_len = strnlen(interpreter_name, MAX_INTERPRETER_NAME_LEN) + 1;
  81. char *name = malloc(name_len);
  82. assert(name);
  83. strncpy(name, interpreter_name, name_len);
  84. tstate = Py_NewInterpreter();
  85. if (NULL == tstate) {
  86. return Py_False;
  87. }
  88. PyThreadState_Swap(tstate);
  89. PyObject *main_module = PyImport_AddModule("__main__");
  90. PyThreadState_Swap(orig_tstate);
  91. PyGILState_Release(gil);
  92. struct interpr *new_interpreter = malloc(sizeof(struct interpr));
  93. assert(new_interpreter);
  94. new_interpreter->name = name;
  95. new_interpreter->python_interpreter = tstate;
  96. new_interpreter->main_module = main_module;
  97. LIST_INSERT_HEAD(&head, new_interpreter, entries);
  98. return Py_True;
  99. }
  100. PyObject *destroy_subinterpreter(char *interpreter_name) {
  101. SUBINTERPRETER_SWITCH;
  102. Py_EndInterpreter(sub_interpreter->python_interpreter);
  103. free(sub_interpreter->name);
  104. LIST_REMOVE(sub_interpreter, entries);
  105. free(sub_interpreter);
  106. ret = Py_True;
  107. SUBINTERPRETER_RETURN;
  108. }
  109. PyObject *list_subinterpreters() {
  110. struct interpr *iter;
  111. PyObject *ret_list = PyList_New(0);
  112. LIST_FOREACH(iter, &head, entries) {
  113. PyObject *interpreter_name = PyUnicode_FromString(iter->name);
  114. PyList_Append(ret_list, interpreter_name);
  115. Py_DECREF(interpreter_name);
  116. }
  117. return ret_list;
  118. }
  119. PyObject *import_module(char *interpreter_name, PyObject *name, PyObject *as) {
  120. SUBINTERPRETER_SWITCH;
  121. PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module);
  122. ret = Py_True;
  123. PyObject *module = PyImport_Import(name); // New reference
  124. exception = PyErr_GetRaisedException();
  125. if (exception)
  126. goto finish;
  127. PyObject_SetItem(global_dict, as, module);
  128. exception = PyErr_GetRaisedException();
  129. finish:
  130. Py_XDECREF(module);
  131. SUBINTERPRETER_RETURN;
  132. }
  133. PyObject *run_string(char *interpreter_name, char *string, PyObject *target_name) {
  134. SUBINTERPRETER_SWITCH;
  135. PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module);
  136. PyObject *local_dict = PyDict_New();
  137. PyObject *obj = PyRun_String(string, Py_eval_input, global_dict, local_dict);
  138. exception = PyErr_GetRaisedException();
  139. if (exception)
  140. goto finish;
  141. SETUP_RET;
  142. finish:
  143. SUBINTERPRETER_RETURN;
  144. }
  145. PyObject *call_py(char *interpreter_name, PyObject *obj_name, PyObject *method_name,
  146. PyObject *target_name, PyObject *args_pytuple, PyObject *kwargs_pydict) {
  147. SUBINTERPRETER_SWITCH;
  148. PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module);
  149. PyObject *call_obj = PyObject_GetItem(global_dict, obj_name); // New reference
  150. PyObject *obj = NULL;
  151. PyObject *method = NULL;
  152. if (NULL == call_obj) {
  153. PyErr_Clear();
  154. PyObject *builtins_name = PyUnicode_FromString("__builtins__");
  155. PyObject *builtins = PyObject_GetItem(global_dict, builtins_name);
  156. Py_DECREF(builtins_name);
  157. call_obj = PyObject_GetAttr(builtins, obj_name); // New reference
  158. }
  159. exception = PyErr_GetRaisedException();
  160. if (exception)
  161. goto finish;
  162. assert(call_obj);
  163. if (1 != PyObject_IsTrue(kwargs_pydict)) {
  164. kwargs_pydict = NULL;
  165. }
  166. if (1 == PyObject_IsTrue(method_name)) {
  167. method = PyObject_GetAttr(call_obj, method_name); // New reference
  168. exception = PyErr_GetRaisedException();
  169. if (exception)
  170. goto finish;
  171. if (0 == PyCallable_Check(method)) {
  172. PyErr_SetObject(PyExc_ValueError, method_name);
  173. exception = PyErr_GetRaisedException();
  174. goto finish;
  175. }
  176. obj = PyObject_Call(method, args_pytuple, kwargs_pydict); // New reference
  177. } else {
  178. obj = PyObject_Call(call_obj, args_pytuple, kwargs_pydict); // New reference
  179. }
  180. exception = PyErr_GetRaisedException();
  181. if (exception)
  182. goto finish;
  183. SETUP_RET;
  184. finish:
  185. Py_XDECREF(call_obj);
  186. Py_XDECREF(method);
  187. SUBINTERPRETER_RETURN;
  188. }
  189. PyObject *get_global_variable(char *interpreter_name, PyObject *var_name) {
  190. SUBINTERPRETER_SWITCH;
  191. PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module);
  192. // Doc don't mention it but it will raise exception on wrong key
  193. ret = PyObject_GetItem(global_dict, var_name); // New reference
  194. exception = PyErr_GetRaisedException();
  195. SUBINTERPRETER_RETURN;
  196. }
  197. PyObject *get_object_attr(char *interpreter_name, PyObject *obj_name, PyObject *attr_name,
  198. PyObject *target_name) {
  199. SUBINTERPRETER_SWITCH;
  200. PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module);
  201. PyObject *obj = NULL;
  202. PyObject *holding_obj = PyObject_GetItem(global_dict, obj_name); // New reference
  203. if (NULL == holding_obj) {
  204. PyErr_SetObject(PyExc_KeyError, holding_obj);
  205. exception = PyErr_GetRaisedException();
  206. goto finish;
  207. }
  208. obj = PyObject_GetAttr(holding_obj, attr_name); // New reference
  209. if (NULL == obj) {
  210. PyErr_SetObject(PyExc_AttributeError, attr_name);
  211. exception = PyErr_GetRaisedException();
  212. goto finish;
  213. }
  214. SETUP_RET;
  215. finish:
  216. Py_XDECREF(holding_obj);
  217. SUBINTERPRETER_RETURN;
  218. }
  219. PyObject *set_global(char *interpreter_name, PyObject *obj, PyObject *as) {
  220. SUBINTERPRETER_SWITCH;
  221. PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module);
  222. PyObject_SetItem(global_dict, as, obj);
  223. exception = PyErr_GetRaisedException();
  224. ret = Py_NewRef(as);
  225. SUBINTERPRETER_RETURN;
  226. }