From 188e7910dd67eec642bc2e7c0bd5247915e32cf4 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sun, 29 Dec 2013 21:50:56 +0800 Subject: [PATCH] add pywinalfred test project --- PyWinAlfred.Test/PyWinAlfred.Test.vcxproj | 155 ++++++++++++++++++ .../PyWinAlfred.Test.vcxproj.filters | 22 +++ PyWinAlfred.Test/Source.cpp | 124 ++++++++++++++ PyWinAlfred/Main.cpp | 4 + WinAlfred.sln | 18 ++ 5 files changed, 323 insertions(+) create mode 100644 PyWinAlfred.Test/PyWinAlfred.Test.vcxproj create mode 100644 PyWinAlfred.Test/PyWinAlfred.Test.vcxproj.filters create mode 100644 PyWinAlfred.Test/Source.cpp diff --git a/PyWinAlfred.Test/PyWinAlfred.Test.vcxproj b/PyWinAlfred.Test/PyWinAlfred.Test.vcxproj new file mode 100644 index 0000000000..008dae7f0a --- /dev/null +++ b/PyWinAlfred.Test/PyWinAlfred.Test.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {05D72D92-4010-4F92-A147-906930241573} + Win32Proj + PyWinAlfredTest + + + + Application + true + v110 + Unicode + + + Application + true + v110 + Unicode + + + Application + false + v110 + true + Unicode + + + Application + false + v110 + true + Unicode + + + + + + + + + + + + + + + + + + + true + C:\Python27\include;$(IncludePath) + C:\Python27\libs;$(LibraryPath) + + + true + C:\Python27\include;$(IncludePath) + C:\Python27\libs;$(LibraryPath) + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/PyWinAlfred.Test/PyWinAlfred.Test.vcxproj.filters b/PyWinAlfred.Test/PyWinAlfred.Test.vcxproj.filters new file mode 100644 index 0000000000..395e675b59 --- /dev/null +++ b/PyWinAlfred.Test/PyWinAlfred.Test.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/PyWinAlfred.Test/Source.cpp b/PyWinAlfred.Test/Source.cpp new file mode 100644 index 0000000000..4f70cb6a41 --- /dev/null +++ b/PyWinAlfred.Test/Source.cpp @@ -0,0 +1,124 @@ +#include +#include +#include "Python.h" +#include +#include + +char* GetErrorMessage() +{ + char *pStrErrorMessage = NULL; + + if(PyErr_Occurred()){ + PyObject *ptype, *pvalue, *ptraceback; + PyErr_Fetch(&ptype, &pvalue, &ptraceback); + pStrErrorMessage = PyString_AsString(pvalue); + } + + return pStrErrorMessage; +} + + +char* Exec(char* directory, char* file, char* method, char* para) +{ + PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance; + char *error; + + //PyThreadState* global_state = PyThreadState_Get(); + //PyThreadState* ts = Py_NewInterpreter(); + //PyThreadState_Swap(ts); + // Initialise the Python interpreter + + // Create GIL/enable threads + + PyGILState_STATE gstate = PyGILState_Ensure(); + // // Get the default thread state + // PyThreadState* state = PyThreadState_Get(); + // // Once in each thread + //PyThreadState* stateForNewThread = PyThreadState_New(state->interp); + //PyEval_RestoreThread(stateForNewThread); + + // Build the name object + PyObject *sys = PyImport_ImportModule("sys"); + PyObject *path = PyObject_GetAttrString(sys, "path"); + PyList_Append(path, PyString_FromString(directory)); + + pName = PyString_FromString(file); + error = GetErrorMessage(); + if(error != NULL){ + char* err =new char[5000](); + sprintf(error, "%s:%s","PYTHONERROR",error); + return err; + } + + pModule = PyImport_Import(pName); + error = GetErrorMessage(); + if(error != NULL){ + char* err =new char[5000](); + sprintf(err, "%s:%s","PYTHONERROR",error); + return err; + } + + pDict = PyModule_GetDict(pModule); + error = GetErrorMessage(); + if(error != NULL){ + char* err =new char[5000](); + sprintf(err, "%s:%s","PYTHONERROR",error); + return err; + } + + pClass = PyDict_GetItemString(pDict,"PyWinAlfred"); + error = GetErrorMessage(); + if(error != NULL){ + char* err =new char[5000](); + sprintf(err, "%s:%s","PYTHONERROR",error); + return err; + } + + pInstance = PyObject_CallObject(pClass, NULL); + error = GetErrorMessage(); + if(error != NULL){ + char* err =new char[5000](); + sprintf(err, "%s:%s","PYTHONERROR",error); + return err; + } + + // Call a method of the class with two parameters + pValue = PyObject_CallMethod(pInstance,method, "(s)",para); + error = GetErrorMessage(); + if(error != NULL){ + char* err =new char[5000](); + sprintf(err, "%s:%s","PYTHONERROR",error); + return err; + } + + char * str_ret = PyString_AsString(pValue); + + //PyEval_SaveThread(); + + // Finish the Python Interpreter + + //PyErr_Clear(); + //Py_EndInterpreter(ts); + //PyThreadState_Swap(global_state); + + + PyGILState_Release(gstate); + return str_ret; +} + +int main(int argc, char *argv[]) +{ + char* directory = "d:\\Personal\\WinAlfred\\WinAlfred\\bin\\Debug\\Plugins\\p4pperson\\"; + char* file = "main"; + char* method = "query"; + char* para = "p q"; + Py_Initialize(); + PyEval_InitThreads(); + for(int i=0;i++;i<10){ + auto future = std::async(Exec,directory,file,method,para); + printf("%s",future.get()); + } + Py_Finalize(); + getchar(); + return 0; +} \ No newline at end of file diff --git a/PyWinAlfred/Main.cpp b/PyWinAlfred/Main.cpp index 75306ab75e..fd61844adc 100644 --- a/PyWinAlfred/Main.cpp +++ b/PyWinAlfred/Main.cpp @@ -3,8 +3,11 @@ #include #include +int i = 0; + extern "C" __declspec(dllexport) void InitPythonEnv() { + i++; Py_Initialize(); PyEval_InitThreads(); } @@ -27,6 +30,7 @@ char* Exec(char* directory, char* file, char* method, char* para) { PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance; char *error; + i++; PyThreadState* global_state = PyThreadState_Get(); PyThreadState* ts = Py_NewInterpreter(); diff --git a/WinAlfred.sln b/WinAlfred.sln index 1b27e33db8..6c6a89568d 100644 --- a/WinAlfred.sln +++ b/WinAlfred.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred", "WinAlfred\WinA EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PyWinAlfred", "PyWinAlfred\PyWinAlfred.vcxproj", "{D03FD663-38A8-4C1A-8431-EB44F93E7EBA}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PyWinAlfred.Test", "PyWinAlfred.Test\PyWinAlfred.Test.vcxproj", "{05D72D92-4010-4F92-A147-906930241573}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -104,6 +106,22 @@ Global {D03FD663-38A8-4C1A-8431-EB44F93E7EBA}.Release|x64.Build.0 = Release|x64 {D03FD663-38A8-4C1A-8431-EB44F93E7EBA}.Release|x86.ActiveCfg = Release|Win32 {D03FD663-38A8-4C1A-8431-EB44F93E7EBA}.Release|x86.Build.0 = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Debug|Any CPU.ActiveCfg = Debug|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Debug|Mixed Platforms.ActiveCfg = Debug|x64 + {05D72D92-4010-4F92-A147-906930241573}.Debug|Mixed Platforms.Build.0 = Debug|x64 + {05D72D92-4010-4F92-A147-906930241573}.Debug|Win32.ActiveCfg = Debug|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Debug|Win32.Build.0 = Debug|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Debug|x64.ActiveCfg = Debug|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Debug|x86.ActiveCfg = Debug|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Debug|x86.Build.0 = Debug|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|Any CPU.ActiveCfg = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|Mixed Platforms.ActiveCfg = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|Mixed Platforms.Build.0 = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|Win32.ActiveCfg = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|Win32.Build.0 = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|x64.ActiveCfg = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|x86.ActiveCfg = Release|Win32 + {05D72D92-4010-4F92-A147-906930241573}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE