PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/branch/0.3.x/pykd_ext/pyinterpret.cpp

#
C++ | 856 lines | 664 code | 184 blank | 8 comment | 56 complexity | 320bae7caac31051ca7799d66a7b5ea1 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Unlicense
  1. #include "stdafx.h"
  2. #include "pyinterpret.h"
  3. #include <memory>
  4. #include <sstream>
  5. #include <map>
  6. #include <set>
  7. #include <algorithm>
  8. #include "pymodule.h"
  9. #include "pyclass.h"
  10. #include "dbgout.h"
  11. class PyModule;
  12. class PythonInterpreter;
  13. class HKey
  14. {
  15. public:
  16. HKey() : m_key(NULL)
  17. {}
  18. ~HKey()
  19. {
  20. if (m_key)
  21. RegCloseKey(m_key);
  22. }
  23. operator HKEY*()
  24. {
  25. return &m_key;
  26. }
  27. operator HKEY() const
  28. {
  29. return m_key;
  30. }
  31. private:
  32. HKEY m_key;
  33. };
  34. class PyModule
  35. {
  36. public:
  37. PyModule(int majorVesion, int minorVersion);
  38. ~PyModule();
  39. bool isPy3;
  40. void checkPykd();
  41. void deactivate();
  42. PyObject* PyType_Type;
  43. PyObject* PyProperty_Type;
  44. PyObject* Py_None;
  45. PyObject* PyExc_SystemExit;
  46. void( *Py_Initialize)();
  47. void( *Py_Finalize)();
  48. PyThreadState* ( *Py_NewInterpreter)();
  49. void( *Py_EndInterpreter)(PyThreadState *tstate);
  50. PyObject* ( *PyEval_GetGlobals)();
  51. PyObject* ( *PyImport_Import)(PyObject *name);
  52. PyObject* ( *PyImport_ImportModule)(const char *name);
  53. void( *PyEval_InitThreads)();
  54. PyThreadState* ( *PyEval_SaveThread)();
  55. void( *PyEval_RestoreThread)(PyThreadState *tstate);
  56. PyThreadState* ( *PyThreadState_Swap)(PyThreadState *tstate);
  57. PyObject* ( *PyRun_String)(const char *str, int start, PyObject *globals, PyObject *locals);
  58. int( *PyRun_SimpleString)(const char* str);
  59. PyObject* ( *PyRun_File)(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals);
  60. PyObject* ( *PyDict_New)();
  61. int( *PyDict_SetItemString)(PyObject *p, const char *key, PyObject *val);
  62. PyObject*( *PyDict_GetItemString)(PyObject *p, const char* key);
  63. void( *Py_IncRef)(PyObject* object);
  64. void( *Py_DecRef)(PyObject* object);
  65. PyObject* ( *PyObject_Call)(PyObject *callable_object, PyObject *args, PyObject *kw);
  66. PyObject* ( *PyObject_GetAttr)(PyObject *object, PyObject *attr_name);
  67. PyObject* ( *PyObject_GetAttrString)(PyObject *object, const char *attr_name);
  68. int( *PyObject_SetAttr)(PyObject *object, PyObject *attr_name, PyObject *value);
  69. PyObject* ( *PyObject_CallObject)(PyObject *callable_object, PyObject *args);
  70. PyObject* ( *PyTuple_New)(size_t len);
  71. int( *PyTuple_SetItem)(PyObject *p, size_t pos, PyObject *o);
  72. PyObject* ( *PyTuple_GetItem)(PyObject *p, size_t pos);
  73. size_t( *PyTuple_Size)(PyObject *p);
  74. PyObject* ( *PyCFunction_NewEx)(PyMethodDef *, PyObject *, PyObject *);
  75. PyObject* ( *PySys_GetObject)(char *name);
  76. int( *PySys_SetObject)(char *name, PyObject *v);
  77. void( *PySys_SetArgv)(int argc, char **argv);
  78. void( *PySys_SetArgv_Py3)(int argc, wchar_t **argv);
  79. PyObject* ( *PyString_FromString)(const char *v);
  80. char* ( *PyString_AsString)(PyObject *string);
  81. void( *PyErr_Fetch)(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback);
  82. void( *PyErr_NormalizeException)(PyObject**exc, PyObject**val, PyObject**tb);
  83. void( *PyErr_SetString)(PyObject *type, const char *message);
  84. void( *PyErr_Clear)();
  85. PyObject* ( *PyImport_AddModule)(const char *name);
  86. PyObject* ( *PyClass_New)(PyObject* className, PyObject* classBases, PyObject* classDict);
  87. PyObject* ( *PyInstance_New)(PyObject *classobj, PyObject *arg, PyObject *kw);
  88. PyObject* ( *PyMethod_New)(PyObject *func, PyObject *self, PyObject *classobj);
  89. PyObject* ( *PyCapsule_New)(void *pointer, const char *name, PyCapsule_Destructor destructor);
  90. void* ( *PyCapsule_GetPointer)(PyObject *capsule, const char *name);
  91. int( *PyObject_SetAttrString)(PyObject *o, const char *attr_name, PyObject *v);
  92. PyObject* ( *PyUnicode_FromWideChar)(const wchar_t *w, size_t size);
  93. PyObject* ( *PyBool_FromLong)(long v);
  94. size_t( *PyList_Size)(PyObject* list);
  95. PyObject* ( *PyList_GetItem)(PyObject *list, size_t index);
  96. PyObject* ( *PyFile_FromString)(char *filename, char *mode);
  97. FILE* ( *PyFile_AsFile)(PyObject *pyfile);
  98. PyObject* ( *PyUnicode_FromString)(const char *u);
  99. PyObject* ( *PyInstanceMethod_New)(PyObject *func);
  100. size_t( *PyUnicode_AsWideChar)(PyObject *unicode, wchar_t *w, size_t size);
  101. FILE* ( *_Py_fopen)(const char* filename, const char* mode);
  102. int( *Py_AddPendingCall)(int(*func)(void *), void *arg);
  103. PyGILState_STATE( *PyGILState_Ensure)();
  104. void( *PyGILState_Release)(PyGILState_STATE state);
  105. PyObject* ( *PyDescr_NewMethod)(PyObject* type, struct PyMethodDef *meth);
  106. HMODULE m_handlePython;
  107. PyThreadState* m_globalState;
  108. PythonInterpreter* m_globalInterpreter;
  109. bool m_pykdInit;
  110. };
  111. class PythonInterpreter
  112. {
  113. public:
  114. PythonInterpreter(PyModule* mod) :
  115. m_module(mod)
  116. {
  117. PyThreadState* state = mod->Py_NewInterpreter();
  118. m_module->PyThreadState_Swap(state);
  119. m_state = m_module->PyEval_SaveThread();
  120. }
  121. ~PythonInterpreter()
  122. {
  123. m_module->PyEval_RestoreThread(m_state);
  124. m_module->Py_EndInterpreter(m_state);
  125. }
  126. PyModule* m_module;
  127. PyThreadState* m_state;
  128. };
  129. class PythonSingleton
  130. {
  131. public:
  132. static PythonSingleton* get()
  133. {
  134. if (m_singleton.get() == 0)
  135. m_singleton.reset(new PythonSingleton());
  136. return m_singleton.get();
  137. }
  138. PythonInterpreter* currentInterpreter()
  139. {
  140. return m_currentInterpter;
  141. }
  142. PythonInterpreter* getInterpreter(int majorVersion, int minorVersion, bool global)
  143. {
  144. PyModule* module = 0;
  145. if (m_modules.find(std::make_pair(majorVersion, minorVersion)) == m_modules.end())
  146. {
  147. module = new PyModule(majorVersion, minorVersion);
  148. m_modules.insert(std::make_pair(std::make_pair(majorVersion, minorVersion), module));
  149. }
  150. else
  151. {
  152. module = m_modules[std::make_pair(majorVersion, minorVersion)];
  153. }
  154. if (global)
  155. {
  156. if (module->m_globalInterpreter == 0)
  157. {
  158. module->PyEval_RestoreThread(module->m_globalState);
  159. module->m_globalInterpreter = new PythonInterpreter(module);
  160. }
  161. m_currentInterpter = module->m_globalInterpreter;
  162. m_currentIsGlobal = true;
  163. }
  164. else
  165. {
  166. module->PyEval_RestoreThread(module->m_globalState);
  167. m_currentInterpter = new PythonInterpreter(module);
  168. m_currentIsGlobal = false;
  169. }
  170. m_currentInterpter->m_module->PyEval_RestoreThread(m_currentInterpter->m_state);
  171. return m_currentInterpter;
  172. }
  173. void releaseInterpretor(PythonInterpreter* interpret)
  174. {
  175. PyModule* module = m_currentInterpter->m_module;
  176. m_currentInterpter->m_state = module->PyEval_SaveThread();
  177. if (!m_currentIsGlobal)
  178. {
  179. delete m_currentInterpter;
  180. module->PyThreadState_Swap(module->m_globalState);
  181. module->m_globalState = module->PyEval_SaveThread();
  182. }
  183. m_currentInterpter = 0;
  184. }
  185. bool isInterpreterLoaded(int majorVersion, int minorVersion)
  186. {
  187. return m_modules.find(std::make_pair(majorVersion, minorVersion)) != m_modules.end();
  188. }
  189. void checkPykd()
  190. {
  191. m_currentInterpter->m_module->checkPykd();
  192. }
  193. void stopAllInterpreter()
  194. {
  195. for (auto m : m_modules)
  196. {
  197. m_currentInterpter = m.second->m_globalInterpreter;
  198. m.second->deactivate();
  199. }
  200. m_currentInterpter = 0;
  201. }
  202. private:
  203. static std::auto_ptr<PythonSingleton> m_singleton;
  204. std::map<std::pair<int,int>, PyModule*> m_modules;
  205. PythonInterpreter* m_currentInterpter;
  206. bool m_currentIsGlobal;
  207. };
  208. std::auto_ptr<PythonSingleton> PythonSingleton::m_singleton;
  209. HMODULE LoadPythonForKey(HKEY installPathKey, int majorVersion, int minorVersion)
  210. {
  211. HMODULE hmodule = NULL;
  212. char installPath[1000];
  213. DWORD installPathSize = sizeof(installPath);
  214. if (ERROR_SUCCESS == RegQueryValueExA(installPathKey, NULL, NULL, NULL, (LPBYTE)installPath, &installPathSize))
  215. {
  216. std::stringstream dllName;
  217. dllName << "python" << majorVersion << minorVersion << ".dll";
  218. std::stringstream imagePath;
  219. imagePath << installPath << dllName.str();
  220. hmodule = LoadLibraryExA(imagePath.str().c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  221. if (hmodule)
  222. return hmodule;
  223. hmodule = LoadLibraryA(dllName.str().c_str());
  224. if (hmodule)
  225. return hmodule;
  226. }
  227. return NULL;
  228. }
  229. HMODULE LoadPythonLibrary(int majorVersion, int minorVersion)
  230. {
  231. HKey pythonCoreKey;
  232. for (auto rootKey : std::list<HKEY>({ HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }))
  233. {
  234. if (ERROR_SUCCESS == RegOpenKeyA(rootKey, "SOFTWARE\\Python\\PythonCore", pythonCoreKey))
  235. {
  236. HKey installPathKey;
  237. std::stringstream installPathStr;
  238. if (majorVersion == 2 || (majorVersion == 3 && minorVersion <= 4))
  239. {
  240. installPathStr << majorVersion << '.' << minorVersion << "\\InstallPath";
  241. }
  242. else
  243. if (majorVersion == 3 && minorVersion >= 5)
  244. {
  245. #ifdef _M_X64
  246. installPathStr << majorVersion << '.' << minorVersion << "\\InstallPath";
  247. #else
  248. installPathStr << majorVersion << '.' << minorVersion << "-32" << "\\InstallPath";
  249. #endif
  250. }
  251. if (ERROR_SUCCESS == RegOpenKeyA(pythonCoreKey, installPathStr.str().c_str(), installPathKey))
  252. {
  253. HMODULE hmodule = LoadPythonForKey(installPathKey, majorVersion, minorVersion);
  254. if (hmodule)
  255. return hmodule;
  256. }
  257. }
  258. }
  259. return NULL;
  260. }
  261. std::list<InterpreterDesc> getInstalledInterpreter()
  262. {
  263. std::set<InterpreterDesc> interpretSet;
  264. for (auto rootKey : const std::list<HKEY>({ HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }))
  265. {
  266. HKey pythonCoreKey;
  267. if (ERROR_SUCCESS != RegOpenKeyA(rootKey, "SOFTWARE\\Python\\PythonCore", pythonCoreKey))
  268. continue;
  269. for (DWORD i = 0;; ++i)
  270. {
  271. char versionStr[20];
  272. if (ERROR_SUCCESS != RegEnumKeyA(pythonCoreKey, i, versionStr, sizeof(versionStr)))
  273. break;
  274. int majorVersion = -1, minorVersion = -1;
  275. sscanf_s(versionStr, "%d.%d", &majorVersion, &minorVersion);
  276. HKey installPathKey;
  277. std::string installPathStr(versionStr);
  278. installPathStr += "\\InstallPath";
  279. if (ERROR_SUCCESS != RegOpenKeyA(pythonCoreKey, installPathStr.c_str(), installPathKey))
  280. continue;
  281. HMODULE hmodule = LoadPythonForKey(installPathKey, majorVersion, minorVersion);
  282. if (hmodule)
  283. {
  284. char fullPath[1000];
  285. if (GetModuleFileNameA(hmodule, fullPath, sizeof(fullPath)))
  286. {
  287. interpretSet.insert({ majorVersion, minorVersion, fullPath });
  288. }
  289. FreeLibrary(hmodule);
  290. }
  291. }
  292. }
  293. std::list<InterpreterDesc> interpretLst;
  294. std::copy(interpretSet.begin(), interpretSet.end(), std::inserter(interpretLst, interpretLst.begin()));
  295. interpretLst.sort();
  296. return interpretLst;
  297. }
  298. PyModule::PyModule(int majorVesion, int minorVersion)
  299. {
  300. m_handlePython = LoadPythonLibrary(majorVesion, minorVersion);
  301. if (!m_handlePython)
  302. throw std::exception("failed to load python module");
  303. isPy3 = majorVesion == 3;
  304. *reinterpret_cast<FARPROC*>(&PyType_Type) = GetProcAddress(m_handlePython, "PyType_Type");
  305. *reinterpret_cast<FARPROC*>(&PyProperty_Type) = GetProcAddress(m_handlePython, "PyProperty_Type");
  306. *reinterpret_cast<FARPROC*>(&Py_None) = GetProcAddress(m_handlePython, "_Py_NoneStruct");
  307. PyExc_SystemExit = *reinterpret_cast<PyObject**>(GetProcAddress(m_handlePython, "PyExc_SystemExit"));
  308. *reinterpret_cast<FARPROC*>(&Py_Initialize) = GetProcAddress(m_handlePython, "Py_Initialize");
  309. *reinterpret_cast<FARPROC*>(&Py_Finalize) = GetProcAddress(m_handlePython, "Py_Finalize");
  310. *reinterpret_cast<FARPROC*>(&Py_NewInterpreter) = GetProcAddress(m_handlePython, "Py_NewInterpreter");
  311. *reinterpret_cast<FARPROC*>(&Py_EndInterpreter) = GetProcAddress(m_handlePython, "Py_EndInterpreter");
  312. *reinterpret_cast<FARPROC*>(&Py_DecRef) = GetProcAddress(m_handlePython, "Py_DecRef");
  313. *reinterpret_cast<FARPROC*>(&Py_IncRef) = GetProcAddress(m_handlePython, "Py_IncRef");
  314. *reinterpret_cast<FARPROC*>(&PyEval_GetGlobals) = GetProcAddress(m_handlePython, "PyEval_GetGlobals");
  315. *reinterpret_cast<FARPROC*>(&PyEval_InitThreads) = GetProcAddress(m_handlePython, "PyEval_InitThreads");
  316. *reinterpret_cast<FARPROC*>(&PyEval_SaveThread) = GetProcAddress(m_handlePython, "PyEval_SaveThread");
  317. *reinterpret_cast<FARPROC*>(&PyEval_RestoreThread) = GetProcAddress(m_handlePython, "PyEval_RestoreThread");
  318. *reinterpret_cast<FARPROC*>(&PyThreadState_Swap) = GetProcAddress(m_handlePython, "PyThreadState_Swap");
  319. *reinterpret_cast<FARPROC*>(&PyRun_String) = GetProcAddress(m_handlePython, "PyRun_String");
  320. *reinterpret_cast<FARPROC*>(&PyRun_SimpleString) = GetProcAddress(m_handlePython, "PyRun_SimpleString");
  321. *reinterpret_cast<FARPROC*>(&PyRun_File) = GetProcAddress(m_handlePython, "PyRun_File");
  322. *reinterpret_cast<FARPROC*>(&PyDict_New) = GetProcAddress(m_handlePython, "PyDict_New");
  323. *reinterpret_cast<FARPROC*>(&PyDict_SetItemString) = GetProcAddress(m_handlePython, "PyDict_SetItemString");
  324. *reinterpret_cast<FARPROC*>(&PyDict_GetItemString) = GetProcAddress(m_handlePython, "PyDict_GetItemString");
  325. *reinterpret_cast<FARPROC*>(&PyObject_Call) = GetProcAddress(m_handlePython, "PyObject_Call");
  326. *reinterpret_cast<FARPROC*>(&PyObject_GetAttr) = GetProcAddress(m_handlePython, "PyObject_GetAttr");
  327. *reinterpret_cast<FARPROC*>(&PyObject_GetAttrString) = GetProcAddress(m_handlePython, "PyObject_GetAttrString");
  328. *reinterpret_cast<FARPROC*>(&PyObject_SetAttr) = GetProcAddress(m_handlePython, "PyObject_SetAttr");
  329. *reinterpret_cast<FARPROC*>(&PyObject_CallObject) = GetProcAddress(m_handlePython, "PyObject_CallObject");
  330. *reinterpret_cast<FARPROC*>(&PyTuple_New) = GetProcAddress(m_handlePython, "PyTuple_New");
  331. *reinterpret_cast<FARPROC*>(&PyTuple_SetItem) = GetProcAddress(m_handlePython, "PyTuple_SetItem");
  332. *reinterpret_cast<FARPROC*>(&PyTuple_GetItem) = GetProcAddress(m_handlePython, "PyTuple_GetItem");
  333. *reinterpret_cast<FARPROC*>(&PyTuple_Size) = GetProcAddress(m_handlePython, "PyTuple_Size");
  334. *reinterpret_cast<FARPROC*>(&PyString_FromString) = GetProcAddress(m_handlePython, "PyString_FromString");
  335. *reinterpret_cast<FARPROC*>(&PyCFunction_NewEx) = GetProcAddress(m_handlePython, "PyCFunction_NewEx");
  336. *reinterpret_cast<FARPROC*>(&PySys_GetObject) = GetProcAddress(m_handlePython, "PySys_GetObject");
  337. *reinterpret_cast<FARPROC*>(&PySys_SetObject) = GetProcAddress(m_handlePython, "PySys_SetObject");
  338. *reinterpret_cast<FARPROC*>(&PySys_SetArgv) = !isPy3 ? GetProcAddress(m_handlePython, "PySys_SetArgv") : 0 ;
  339. *reinterpret_cast<FARPROC*>(&PySys_SetArgv_Py3) = isPy3 ? GetProcAddress(m_handlePython, "PySys_SetArgv") : 0;
  340. *reinterpret_cast<FARPROC*>(&PyString_FromString) = GetProcAddress(m_handlePython, "PyString_FromString");
  341. *reinterpret_cast<FARPROC*>(&PyString_AsString) = GetProcAddress(m_handlePython, "PyString_AsString");
  342. *reinterpret_cast<FARPROC*>(&PyErr_Fetch) = GetProcAddress(m_handlePython, "PyErr_Fetch");
  343. *reinterpret_cast<FARPROC*>(&PyErr_NormalizeException) = GetProcAddress(m_handlePython, "PyErr_NormalizeException");
  344. *reinterpret_cast<FARPROC*>(&PyErr_SetString) = GetProcAddress(m_handlePython, "PyErr_SetString");
  345. *reinterpret_cast<FARPROC*>(&PyErr_Clear) = GetProcAddress(m_handlePython, "PyErr_Clear");
  346. *reinterpret_cast<FARPROC*>(&PyImport_AddModule) = GetProcAddress(m_handlePython, "PyImport_AddModule");
  347. *reinterpret_cast<FARPROC*>(&PyImport_ImportModule) = GetProcAddress(m_handlePython, "PyImport_ImportModule");
  348. *reinterpret_cast<FARPROC*>(&PyClass_New) = GetProcAddress(m_handlePython, "PyClass_New");
  349. *reinterpret_cast<FARPROC*>(&PyInstance_New) = GetProcAddress(m_handlePython, "PyInstance_New");
  350. *reinterpret_cast<FARPROC*>(&PyMethod_New) = GetProcAddress(m_handlePython, "PyMethod_New");
  351. *reinterpret_cast<FARPROC*>(&PyCapsule_New) = GetProcAddress(m_handlePython, "PyCapsule_New");
  352. *reinterpret_cast<FARPROC*>(&PyCapsule_GetPointer) = GetProcAddress(m_handlePython, "PyCapsule_GetPointer");
  353. *reinterpret_cast<FARPROC*>(&PyObject_SetAttrString) = GetProcAddress(m_handlePython, "PyObject_SetAttrString");
  354. *reinterpret_cast<FARPROC*>(&PyUnicode_FromWideChar) = isPy3 ? GetProcAddress(m_handlePython, "PyUnicode_FromWideChar") :
  355. GetProcAddress(m_handlePython, "PyUnicodeUCS2_FromWideChar");
  356. *reinterpret_cast<FARPROC*>(&PyImport_Import) = GetProcAddress(m_handlePython, "PyImport_Import");
  357. *reinterpret_cast<FARPROC*>(&PyBool_FromLong) = GetProcAddress(m_handlePython, "PyBool_FromLong");
  358. *reinterpret_cast<FARPROC*>(&PyList_Size) = GetProcAddress(m_handlePython, "PyList_Size");
  359. *reinterpret_cast<FARPROC*>(&PyList_GetItem) = GetProcAddress(m_handlePython, "PyList_GetItem");
  360. *reinterpret_cast<FARPROC*>(&PyFile_FromString) = GetProcAddress(m_handlePython, "PyFile_FromString");
  361. *reinterpret_cast<FARPROC*>(&PyFile_AsFile) = GetProcAddress(m_handlePython, "PyFile_AsFile");
  362. *reinterpret_cast<FARPROC*>(&PyUnicode_FromString) = GetProcAddress(m_handlePython, "PyUnicode_FromString");
  363. *reinterpret_cast<FARPROC*>(&PyInstanceMethod_New) = GetProcAddress(m_handlePython, "PyInstanceMethod_New");
  364. *reinterpret_cast<FARPROC*>(&PyUnicode_AsWideChar) = GetProcAddress(m_handlePython, "PyUnicode_AsWideChar");
  365. *reinterpret_cast<FARPROC*>(&_Py_fopen) = GetProcAddress(m_handlePython, "_Py_fopen");
  366. *reinterpret_cast<FARPROC*>(&Py_AddPendingCall) = GetProcAddress(m_handlePython, "Py_AddPendingCall");
  367. *reinterpret_cast<FARPROC*>(&PyGILState_Ensure) = GetProcAddress(m_handlePython, "PyGILState_Ensure");
  368. *reinterpret_cast<FARPROC*>(&PyGILState_Release) = GetProcAddress(m_handlePython, "PyGILState_Release");
  369. *reinterpret_cast<FARPROC*>(&PyDescr_NewMethod) = GetProcAddress(m_handlePython, "PyDescr_NewMethod");
  370. Py_Initialize();
  371. PyEval_InitThreads();
  372. m_globalState = PyEval_SaveThread();
  373. }
  374. PyModule::~PyModule()
  375. {
  376. assert(0);
  377. //if (m_globalInterpreter)
  378. //{
  379. // delete m_globalInterpreter;
  380. // m_globalInterpreter = 0;
  381. //}
  382. //PyThreadState_Swap(m_globalState);
  383. //Py_Finalize();
  384. //FreeLibrary(m_handlePython);
  385. }
  386. void PyModule::deactivate()
  387. {
  388. if (m_globalInterpreter)
  389. {
  390. delete m_globalInterpreter;
  391. m_globalInterpreter = 0;
  392. }
  393. PyThreadState_Swap(m_globalState);
  394. if (m_pykdInit)
  395. {
  396. PyObject* mainName = isPy3 ? PyUnicode_FromString("__main__") : PyString_FromString("__main__");
  397. PyObject* mainMod = PyImport_Import(mainName);
  398. PyObject* globals = PyObject_GetAttrString(mainMod, "__dict__");
  399. PyObject* result = PyRun_String("__import__('pykd').deinitialize()\n", Py_file_input, globals, globals);
  400. if (mainName) Py_DecRef(mainName);
  401. if (mainMod) Py_DecRef(mainMod);
  402. if (globals) Py_DecRef(globals);
  403. if (result) Py_DecRef(result);
  404. m_pykdInit = false;
  405. }
  406. m_globalState = PyEval_SaveThread();
  407. }
  408. void PyModule::checkPykd()
  409. {
  410. PyObject *mainName = 0, *mainMod = 0, *globals = 0, *result = 0;
  411. do {
  412. if (m_pykdInit)
  413. break;
  414. mainName = isPy3 ? PyUnicode_FromString("__main__") : PyString_FromString("__main__");
  415. if (!mainName)
  416. break;
  417. mainMod = PyImport_Import(mainName);
  418. if (!mainMod)
  419. break;
  420. globals = PyObject_GetAttrString(mainMod, "__dict__");
  421. if (!globals)
  422. break;
  423. result = PyRun_String("__import__('pykd').initialize()\n", Py_file_input, globals, globals);
  424. if (!result)
  425. break;
  426. m_pykdInit = true;
  427. } while( false);
  428. if (mainName) Py_DecRef(mainName);
  429. if (mainMod) Py_DecRef(mainMod);
  430. if (globals) Py_DecRef(globals);
  431. if (result) Py_DecRef(result);
  432. }
  433. PythonInterpreter* activateInterpreter(bool global, int majorVersion, int minorVersion)
  434. {
  435. return PythonSingleton::get()->getInterpreter(majorVersion, minorVersion, global);
  436. }
  437. void releaseInterpretor(PythonInterpreter* interpret)
  438. {
  439. PythonSingleton::get()->releaseInterpretor(interpret);
  440. }
  441. bool isInterpreterLoaded(int majorVersion, int minorVersion)
  442. {
  443. return PythonSingleton::get()->isInterpreterLoaded(majorVersion, minorVersion);
  444. }
  445. void stopAllInterpreter()
  446. {
  447. PythonSingleton::get()->stopAllInterpreter();
  448. }
  449. void checkPykd()
  450. {
  451. PythonSingleton::get()->checkPykd();
  452. }
  453. void __stdcall Py_IncRef(PyObject* object)
  454. {
  455. PythonSingleton::get()->currentInterpreter()->m_module->Py_IncRef(object);
  456. }
  457. void __stdcall Py_DecRef(PyObject* object)
  458. {
  459. PythonSingleton::get()->currentInterpreter()->m_module->Py_DecRef(object);
  460. }
  461. PyObject* __stdcall PyString_FromString(const char *v)
  462. {
  463. return PythonSingleton::get()->currentInterpreter()->m_module->PyString_FromString(v);
  464. }
  465. PyObject* __stdcall PyDict_New()
  466. {
  467. return PythonSingleton::get()->currentInterpreter()->m_module->PyDict_New();
  468. }
  469. PyObject* __stdcall PyDict_GetItemString(PyObject *p, const char *key)
  470. {
  471. return PythonSingleton::get()->currentInterpreter()->m_module->PyDict_GetItemString(p, key);
  472. }
  473. int __stdcall PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
  474. {
  475. return PythonSingleton::get()->currentInterpreter()->m_module->PyDict_SetItemString(p, key, val);
  476. }
  477. PyObject* __stdcall PyCFunction_NewEx(PyMethodDef* pydef, PyObject *p1, PyObject *p2)
  478. {
  479. return PythonSingleton::get()->currentInterpreter()->m_module->PyCFunction_NewEx(pydef, p1, p2);
  480. }
  481. PyObject* __stdcall PyClass_New(PyObject* className, PyObject* classBases, PyObject* classDict)
  482. {
  483. return PythonSingleton::get()->currentInterpreter()->m_module->PyClass_New(className, classBases, classDict);
  484. }
  485. PyObject* __stdcall PyMethod_New(PyObject *func, PyObject *self, PyObject *classobj)
  486. {
  487. return PythonSingleton::get()->currentInterpreter()->m_module->PyMethod_New(func, self, classobj);
  488. }
  489. int __stdcall PySys_SetObject(char *name, PyObject *v)
  490. {
  491. return PythonSingleton::get()->currentInterpreter()->m_module->PySys_SetObject(name, v);
  492. }
  493. void __stdcall PySys_SetArgv(int argc, char **argv)
  494. {
  495. PythonSingleton::get()->currentInterpreter()->m_module->PySys_SetArgv(argc, argv);
  496. }
  497. void __stdcall PySys_SetArgv_Py3(int argc, wchar_t **argv)
  498. {
  499. PythonSingleton::get()->currentInterpreter()->m_module->PySys_SetArgv_Py3(argc, argv);
  500. }
  501. PyObject* __stdcall PySys_GetObject(char *name)
  502. {
  503. return PythonSingleton::get()->currentInterpreter()->m_module->PySys_GetObject(name);
  504. }
  505. PyObject* __stdcall PyInstance_New(PyObject *classobj, PyObject *arg, PyObject *kw)
  506. {
  507. return PythonSingleton::get()->currentInterpreter()->m_module->PyInstance_New(classobj, arg, kw);
  508. }
  509. int __stdcall PyRun_SimpleString(const char* str)
  510. {
  511. return PythonSingleton::get()->currentInterpreter()->m_module->PyRun_SimpleString(str);
  512. }
  513. PyObject* __stdcall PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
  514. {
  515. return PythonSingleton::get()->currentInterpreter()->m_module->PyRun_String(str, start, globals, locals);
  516. }
  517. PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
  518. {
  519. return PythonSingleton::get()->currentInterpreter()->m_module->PyCapsule_New(pointer, name, destructor);
  520. }
  521. void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
  522. {
  523. return PythonSingleton::get()->currentInterpreter()->m_module->PyCapsule_GetPointer(capsule, name);
  524. }
  525. int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
  526. {
  527. return PythonSingleton::get()->currentInterpreter()->m_module->PyObject_SetAttrString(o, attr_name, v);
  528. }
  529. PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
  530. {
  531. return PythonSingleton::get()->currentInterpreter()->m_module->PyObject_GetAttrString(o, attr_name);
  532. }
  533. PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
  534. {
  535. return PythonSingleton::get()->currentInterpreter()->m_module->PyObject_CallObject(callable_object, args);
  536. }
  537. PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
  538. {
  539. return PythonSingleton::get()->currentInterpreter()->m_module->PyObject_Call(callable_object, args, kw);
  540. }
  541. PyObject* __stdcall PyTuple_New(size_t len)
  542. {
  543. return PythonSingleton::get()->currentInterpreter()->m_module->PyTuple_New(len);
  544. }
  545. PyObject* __stdcall PyTuple_GetItem(PyObject *p, size_t pos)
  546. {
  547. return PythonSingleton::get()->currentInterpreter()->m_module->PyTuple_GetItem(p, pos);
  548. }
  549. int __stdcall PyTuple_SetItem(PyObject *p, size_t pos, PyObject *obj)
  550. {
  551. return PythonSingleton::get()->currentInterpreter()->m_module->PyTuple_SetItem(p, pos, obj);
  552. }
  553. size_t __stdcall PyTuple_Size(PyObject *p)
  554. {
  555. return PythonSingleton::get()->currentInterpreter()->m_module->PyTuple_Size(p);
  556. }
  557. char* PyString_AsString(PyObject *string)
  558. {
  559. return PythonSingleton::get()->currentInterpreter()->m_module->PyString_AsString(string);
  560. }
  561. PyObject* PyUnicode_FromWideChar(const wchar_t *w, size_t size)
  562. {
  563. return PythonSingleton::get()->currentInterpreter()->m_module->PyUnicode_FromWideChar(w, size);
  564. }
  565. PyObject* PyImport_Import(PyObject *name)
  566. {
  567. return PythonSingleton::get()->currentInterpreter()->m_module->PyImport_Import(name);
  568. }
  569. PyObject* PyBool_FromLong(long v)
  570. {
  571. return PythonSingleton::get()->currentInterpreter()->m_module->PyBool_FromLong(v);
  572. }
  573. PyObject* Py_None()
  574. {
  575. return PythonSingleton::get()->currentInterpreter()->m_module->Py_None;
  576. }
  577. PyObject* PyExc_SystemExit()
  578. {
  579. return PythonSingleton::get()->currentInterpreter()->m_module->PyExc_SystemExit;
  580. }
  581. PyObject* PyType_Type()
  582. {
  583. return PythonSingleton::get()->currentInterpreter()->m_module->PyType_Type;
  584. }
  585. PyObject* PyProperty_Type()
  586. {
  587. return PythonSingleton::get()->currentInterpreter()->m_module->PyProperty_Type;
  588. }
  589. void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
  590. {
  591. PythonSingleton::get()->currentInterpreter()->m_module->PyErr_Fetch(ptype, pvalue, ptraceback);
  592. }
  593. void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)
  594. {
  595. PythonSingleton::get()->currentInterpreter()->m_module->PyErr_NormalizeException(exc, val, tb);
  596. }
  597. void PyErr_Clear()
  598. {
  599. PythonSingleton::get()->currentInterpreter()->m_module->PyErr_Clear();
  600. }
  601. void PyErr_SetString(PyObject *type, const char *message)
  602. {
  603. PythonSingleton::get()->currentInterpreter()->m_module->PyErr_SetString(type, message);
  604. }
  605. size_t PyList_Size(PyObject* list)
  606. {
  607. return PythonSingleton::get()->currentInterpreter()->m_module->PyList_Size(list);
  608. }
  609. PyObject* PyList_GetItem(PyObject *list, size_t index)
  610. {
  611. return PythonSingleton::get()->currentInterpreter()->m_module->PyList_GetItem(list, index);
  612. }
  613. PyObject* PyFile_FromString(char *filename, char *mode)
  614. {
  615. return PythonSingleton::get()->currentInterpreter()->m_module->PyFile_FromString(filename, mode);
  616. }
  617. FILE* PyFile_AsFile(PyObject *pyfile)
  618. {
  619. return PythonSingleton::get()->currentInterpreter()->m_module->PyFile_AsFile(pyfile);
  620. }
  621. PyObject* PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals)
  622. {
  623. return PythonSingleton::get()->currentInterpreter()->m_module->PyRun_File(fp, filename, start, globals, locals);
  624. }
  625. PyObject* __stdcall PyUnicode_FromString(const char* str)
  626. {
  627. return PythonSingleton::get()->currentInterpreter()->m_module->PyUnicode_FromString(str);
  628. }
  629. PyObject* __stdcall PyInstanceMethod_New(PyObject *func)
  630. {
  631. return PythonSingleton::get()->currentInterpreter()->m_module->PyInstanceMethod_New(func);
  632. }
  633. size_t __stdcall PyUnicode_AsWideChar(PyObject *unicode, wchar_t *w, size_t size)
  634. {
  635. return PythonSingleton::get()->currentInterpreter()->m_module->PyUnicode_AsWideChar(unicode, w, size);
  636. }
  637. PyObject* __stdcall PyImport_ImportModule(const char *name)
  638. {
  639. return PythonSingleton::get()->currentInterpreter()->m_module->PyImport_ImportModule(name);
  640. }
  641. PyThreadState* __stdcall PyEval_SaveThread()
  642. {
  643. return PythonSingleton::get()->currentInterpreter()->m_module->PyEval_SaveThread();
  644. }
  645. void __stdcall PyEval_RestoreThread(PyThreadState *tstate)
  646. {
  647. PythonSingleton::get()->currentInterpreter()->m_module->PyEval_RestoreThread(tstate);
  648. }
  649. FILE* _Py_fopen(const char* filename, const char* mode)
  650. {
  651. return PythonSingleton::get()->currentInterpreter()->m_module->_Py_fopen(filename, mode);
  652. }
  653. int __stdcall Py_AddPendingCall(int(*func)(void *), void *arg)
  654. {
  655. return PythonSingleton::get()->currentInterpreter()->m_module->Py_AddPendingCall(func, arg);
  656. }
  657. PyGILState_STATE __stdcall PyGILState_Ensure()
  658. {
  659. return PythonSingleton::get()->currentInterpreter()->m_module->PyGILState_Ensure();
  660. }
  661. void __stdcall PyGILState_Release(PyGILState_STATE state)
  662. {
  663. PythonSingleton::get()->currentInterpreter()->m_module->PyGILState_Release(state);
  664. }
  665. PyObject* __stdcall PyDescr_NewMethod(PyObject* type, struct PyMethodDef *meth)
  666. {
  667. return PythonSingleton::get()->currentInterpreter()->m_module->PyDescr_NewMethod(type, meth);
  668. }
  669. bool IsPy3()
  670. {
  671. return PythonSingleton::get()->currentInterpreter()->m_module->isPy3;
  672. }