CString BZScriptPython::run(CBZScriptView* sview, const char * cmdstr) { cbzsv = sview; // referring IDAPython PythonEvalOrExec.. PyCompilerFlags cf = {0}; PyObject *py_code = Py_CompileStringFlags(cmdstr, "<string>", Py_eval_input, &cf); if(py_code == NULL || PyErr_Occurred()) { // Not an expression? PyErr_Clear(); // Run as a string PyRun_SimpleString(cmdstr); return _T(""); } PyObject *module = PyImport_AddModule("__main__"); PyObject *py_globals = (module == NULL) ? NULL : PyModule_GetDict(module); //PYW_GIL_ENSURE; #ifdef BZPYTHON2 PyObject *py_result = PyEval_EvalCode((PyCodeObject*)py_code, py_globals, py_globals); #endif #ifdef BZPYTHON3 PyObject *py_result = PyEval_EvalCode(py_code, py_globals, py_globals); #endif //PYW_GIL_RELEASE; Py_DECREF(py_code); if(py_result == NULL || PyErr_Occurred()) { PyErr_Print(); return _T(""); } if(py_result != Py_None) { PyObject *pystr = PyObject_Str(py_result); Py_DECREF(py_result); #ifdef BZPYTHON2 char *cstr = PyString_AsString(pystr); int cstrlen = strlen(cstr); CStringA csresult(cstr, cstrlen); #endif #ifdef BZPYTHON3 // PyUnicode_AsUnicode->Py_UNICODE could be UCS4... only want wchar_t; use PyUnicode_AsWideChar(String). wchar_t *cstr = PyUnicode_AsWideCharString(pystr, NULL); int cstrlen = lstrlenW(cstr); CStringW csresult(cstr, cstrlen); PyMem_Free(cstr); #endif Py_DECREF(pystr); CString csnative(csresult); CString ostr; csnative.Replace(_T("\n"), _T("\r\n")); ostr.Format(_T("%s\r\n"), csnative); return ostr; } Py_DECREF(py_result); return _T("");//ostr; }
PyObject * PyCompileString(const char *str, const char *filename, int start) { return Py_CompileStringFlags(str, filename, start, NULL); }