CallPython.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "StdAfx.h"
  2. #include "CallPython.h"
  3. #include "CharEncoding.h"
  4. CCallPython::CCallPython()
  5. {
  6. _state = PyGILState_Ensure();
  7. }
  8. CCallPython::~CCallPython()
  9. {
  10. PyGILState_Release(_state);
  11. }
  12. void CCallPython::Init()
  13. {
  14. Py_Initialize();
  15. if (!Py_IsInitialized())
  16. return;
  17. // 不用多线程,不用开启下面功能;
  18. // 初始化线程支持
  19. //PyEval_InitThreads();
  20. // 启动子线程前执行,为了释放PyEval_InitThreads获得的全局锁,否则子线程可能无法获取到全局锁。
  21. //PyEval_ReleaseThread(PyThreadState_Get());
  22. }
  23. void CCallPython::Free()
  24. {
  25. // 保证子线程调用都结束后
  26. PyGILState_Ensure();
  27. Py_Finalize();
  28. }
  29. void PySetItem(PyObject* args, _variant_t var, int nIndex)
  30. {
  31. if (args == NULL || nIndex < 0)
  32. return;
  33. PyObject* pValue;
  34. switch (var.vt)
  35. {
  36. case VT_BSTR:
  37. {
  38. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("u", var.bstrVal));
  39. break;
  40. }
  41. case VT_LPSTR:
  42. {
  43. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("s", var.bstrVal));
  44. break;
  45. }
  46. case VT_I1:
  47. {
  48. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("b", var.cVal));
  49. break;
  50. }
  51. case VT_I2:
  52. {
  53. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("h", var.iVal));
  54. break;
  55. }
  56. case VT_I4:
  57. {
  58. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("i", var.lVal));
  59. break;
  60. }
  61. case VT_I8:
  62. {
  63. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("k", var.lVal));
  64. break;
  65. }
  66. case VT_INT:
  67. {
  68. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("i", var.intVal));
  69. break;
  70. }
  71. case VT_UI1:
  72. {
  73. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("B", var.cVal));
  74. break;
  75. }
  76. case VT_UI2:
  77. {
  78. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("H", var.iVal));
  79. break;
  80. }
  81. case VT_UI4:
  82. {
  83. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("I", var.lVal));
  84. break;
  85. }
  86. case VT_UI8:
  87. {
  88. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("K", var.lVal));
  89. break;
  90. }
  91. case VT_UINT:
  92. {
  93. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("I", var.intVal));
  94. break;
  95. }
  96. case VT_R4:
  97. {
  98. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("f", var.fltVal));
  99. break;
  100. }
  101. case VT_R8:
  102. {
  103. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("f", var.dblVal));
  104. break;
  105. }
  106. case VT_BOOL:
  107. {
  108. PyTuple_SetItem(args, nIndex, pValue = Py_BuildValue("b", (var.boolVal == false) ? 0 : 1));
  109. break;
  110. }
  111. default:
  112. {
  113. TRACE("UnSupport type for Python: %d\n", var.vt);
  114. break;
  115. }
  116. if (pValue)
  117. Py_DECREF(pValue);
  118. }
  119. }
  120. PyObject* CCallPython::CallPython(std::string strPyPath, std::string strPyFuncName, int argc, ...)
  121. {
  122. if (!Py_IsInitialized())
  123. Py_Initialize();
  124. if (!PathFileExists(strPyPath.c_str()) || !strPyFuncName.size())
  125. return NULL;
  126. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  127. TCHAR szDir[_MAX_DIR] = { 0 };
  128. TCHAR szExt[_MAX_EXT] = { 0 };
  129. TCHAR szFilename[_MAX_FNAME] = { 0 };
  130. TCHAR szScriptDir[MAX_PATH] = { 0 };
  131. _tsplitpath_s(strPyPath.c_str(), szDrive, szDir, szFilename, szExt);
  132. _stprintf_s(szScriptDir, _T("%s%s"), szDrive, szDir);
  133. int len = _tcslen(szScriptDir);
  134. for (int i = 0; i < len; i++) {
  135. if (szScriptDir[i] == '\\')
  136. szScriptDir[i] = '/';
  137. }
  138. if (szScriptDir[0] == '\0')
  139. _tcscpy_s(szScriptDir, _T("./"));
  140. TCHAR szExecuteDir[MAX_PATH] = { 0 };
  141. _stprintf_s(szExecuteDir, _T("sys.path.append('%s')"), szScriptDir);
  142. PyRun_SimpleString("import sys");
  143. //PyRun_SimpleString("sys.path.append('./')"); // 无用,加载不了当前其他Py模块;
  144. //PyRun_SimpleString("reload(sys)");
  145. PyRun_SimpleString(szExecuteDir);
  146. //_stprintf_s(szExecuteDir, _T("reload('%s')"), szScriptDir);
  147. //PyRun_SimpleString(szExecuteDir);
  148. PyObject* args = NULL;
  149. PyObject* pRet = NULL;
  150. PyObject* pFunc = NULL;
  151. PyObject* pModule = PyImport_ImportModule(szFilename);
  152. if (!pModule)
  153. goto over;
  154. pFunc = PyObject_GetAttrString(pModule, strPyFuncName.c_str());
  155. if (!pFunc || !PyCallable_Check(pFunc))
  156. goto over;
  157. if (argc != 0) {
  158. va_list ap;
  159. va_start(ap, argc);
  160. args = PyTuple_New(argc);
  161. if (!args) {
  162. va_end(ap);
  163. goto over;
  164. }
  165. for (int i = 0; i < argc; i++) {
  166. PySetItem(args, va_arg(ap, _variant_t), i);
  167. }
  168. va_end(ap);
  169. pRet = PyObject_CallObject(pFunc, args);
  170. }
  171. else
  172. pRet = PyEval_CallObject(pFunc, NULL);
  173. over:
  174. if (args)
  175. Py_DECREF(args);
  176. if (pFunc)
  177. Py_DECREF(pFunc);
  178. if (pModule)
  179. Py_DECREF(pModule);
  180. return pRet;
  181. }
  182. int CCallPython::GetIntegerValue(PyObject* pValue)
  183. {
  184. int value = 0;
  185. if (pValue) {
  186. PyArg_Parse(pValue, "i", &value);
  187. }
  188. return value;
  189. }
  190. std::string CCallPython::GetUnicodeString(PyObject* pValue)
  191. {
  192. std::string strValue;
  193. if (pValue) {
  194. char* pszValue;
  195. PyArg_Parse(pValue, "s", &pszValue);
  196. CharEncoding::UNICODE2ASCII((LPWCH)pszValue, strValue);
  197. }
  198. return strValue;
  199. }
  200. std::string CCallPython::GetUTF8String(PyObject* pValue)
  201. {
  202. std::string strValue;
  203. if (pValue) {
  204. char* pszValue;
  205. PyArg_Parse(pValue, "s", &pszValue);
  206. CharEncoding::UTF82ASCII(pszValue, strValue);
  207. }
  208. return strValue;
  209. }