[pywin32-checkins] pywin32/win32/src PythonService.cpp, 1.24.2.1, 1.24.2.2
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Roger U. <ru...@us...> - 2008-08-30 22:33:25
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16690 Modified Files: Tag: py3k PythonService.cpp Log Message: Get service framework working for py3k Index: PythonService.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/PythonService.cpp,v retrieving revision 1.24.2.1 retrieving revision 1.24.2.2 diff -C2 -d -r1.24.2.1 -r1.24.2.2 *** PythonService.cpp 29 Aug 2008 04:59:26 -0000 1.24.2.1 --- PythonService.cpp 30 Aug 2008 22:33:34 -0000 1.24.2.2 *************** *** 32,36 **** PYSERVICE_EXPORT BOOL PythonService_PrepareToHostMultiple(const TCHAR *service_name, PyObject *klass); PYSERVICE_EXPORT BOOL PythonService_StartServiceCtrlDispatcher(); ! PYSERVICE_EXPORT int PythonService_main(int argc, char **argv); TCHAR g_szEventSourceName[MAX_PATH] = _T("Python Service"); --- 32,36 ---- PYSERVICE_EXPORT BOOL PythonService_PrepareToHostMultiple(const TCHAR *service_name, PyObject *klass); PYSERVICE_EXPORT BOOL PythonService_StartServiceCtrlDispatcher(); ! PYSERVICE_EXPORT int PythonService_main(int argc, TCHAR **argv); TCHAR g_szEventSourceName[MAX_PATH] = _T("Python Service"); *************** *** 121,129 **** static PY_SERVICE_TABLE_ENTRY *FindPythonServiceEntry(LPCTSTR svcName); ! static PyObject *LoadPythonServiceClass(char *svcInitString); static PyObject *LoadPythonServiceInstance(PyObject *, DWORD dwArgc, LPTSTR *lpszArgv ); ! static BOOL LocatePythonServiceClassString( TCHAR *svcName, char *buf, int cchBuf); --- 121,129 ---- static PY_SERVICE_TABLE_ENTRY *FindPythonServiceEntry(LPCTSTR svcName); ! static PyObject *LoadPythonServiceClass(TCHAR *svcInitString); static PyObject *LoadPythonServiceInstance(PyObject *, DWORD dwArgc, LPTSTR *lpszArgv ); ! static BOOL LocatePythonServiceClassString( TCHAR *svcName, TCHAR *buf, int cchBuf); *************** *** 542,546 **** ! #define ADD_CONSTANT(tok) PyModule_AddIntConstant(dict, #tok, tok) extern "C" __declspec(dllexport) --- 542,546 ---- ! #define ADD_CONSTANT(tok) if (PyModule_AddIntConstant(module, #tok, tok) == -1) RETURN_ERROR; extern "C" __declspec(dllexport) *************** *** 637,645 **** // This, however, shouldnt be a problem, as Python itself // knows how to get the .EXE name when it needs. #if (PY_VERSION_HEX < 0x03000000) ! Py_SetProgramName(__argv[0]); #else ! Py_SetProgramName(__wargv[0]); ! #endif; #ifdef BUILD_FREEZE --- 637,649 ---- // This, however, shouldnt be a problem, as Python itself // knows how to get the .EXE name when it needs. + int pyargc; #if (PY_VERSION_HEX < 0x03000000) ! pyargc = __argc; ! char **pyargv = __argv; #else ! WCHAR **pyargv; ! pyargv = CommandLineToArgvW(GetCommandLineW(), &pyargc); ! #endif ! Py_SetProgramName(pyargv[0]); #ifdef BUILD_FREEZE *************** *** 652,662 **** // Ensure we are set for threading. PyEval_InitThreads(); ! #if (PY_VERSION_HEX < 0x03000000) - PySys_SetArgv(__argc, __argv); initservicemanager(); #else - PySys_SetArgv(__argc, __wargv); PyInit_servicemanager(); #endif } --- 656,665 ---- // Ensure we are set for threading. PyEval_InitThreads(); ! PySys_SetArgv(pyargc, pyargv); #if (PY_VERSION_HEX < 0x03000000) initservicemanager(); #else PyInit_servicemanager(); + LocalFree(pyargv); #endif } *************** *** 865,870 **** pe = PythonServiceTable; if (!pe->klass) { ! char svcInitBuf[256]; ! LocatePythonServiceClassString(lpszArgv[0], svcInitBuf, sizeof(svcInitBuf)); pe->klass = LoadPythonServiceClass(svcInitBuf); } --- 868,873 ---- pe = PythonServiceTable; if (!pe->klass) { ! TCHAR svcInitBuf[256]; ! LocatePythonServiceClassString(lpszArgv[0], svcInitBuf, sizeof(svcInitBuf)/sizeof(svcInitBuf[0])); pe->klass = LoadPythonServiceClass(svcInitBuf); } *************** *** 1072,1076 **** // the service has stopped, so exit. // ! int PythonService_main(int argc, char **argv) { // Note that we don't know the service name we are hosting yet! --- 1075,1079 ---- // the service has stopped, so exit. // ! int PythonService_main(int argc, TCHAR **argv) { // Note that we don't know the service name we are hosting yet! *************** *** 1111,1116 **** { #ifndef BUILD_FREEZE ! if ( _stricmp( "register", argv[1]+1 ) == 0 || ! _stricmp( "install", argv[1]+1 ) == 0 ) { // Get out of here. --- 1114,1119 ---- { #ifndef BUILD_FREEZE ! if ( _tcsicmp( _T("register"), argv[1]+1 ) == 0 || ! _tcsicmp( _T("install"), argv[1]+1 ) == 0 ) { // Get out of here. *************** *** 1118,1122 **** } #endif ! if ( _stricmp( "debug", argv[1]+1 ) == 0 ) { /* Debugging the service. If this EXE has a service name embedded in it, use it, otherwise insist one is passed on the --- 1121,1125 ---- } #endif ! if ( _tcsicmp( _T("debug"), argv[1]+1 ) == 0 ) { /* Debugging the service. If this EXE has a service name embedded in it, use it, otherwise insist one is passed on the *************** *** 1179,1191 **** // Given the string in form [path\]module.ClassName, return // an instance of the class ! PyObject *LoadPythonServiceClass(char *svcInitString) { ! char valueBuf[512]; // Initialize Python PyService_InitPython(); ! strncpy(valueBuf, svcInitString, sizeof(valueBuf)); // Find the last "\\" ! char *sep = strrchr(valueBuf, '\\'); ! char *fname; if (sep) { *sep = '\0'; --- 1182,1194 ---- // Given the string in form [path\]module.ClassName, return // an instance of the class ! PyObject *LoadPythonServiceClass(TCHAR *svcInitString) { ! TCHAR valueBuf[512]; // Initialize Python PyService_InitPython(); ! _tcsncpy(valueBuf, svcInitString, sizeof(valueBuf)/sizeof(valueBuf[0])); // Find the last "\\" ! TCHAR *sep = _tcsrchr(valueBuf, _T('\\')); ! TCHAR *fname; if (sep) { *sep = '\0'; *************** *** 1197,1201 **** return NULL; } ! PyObject *obNew = PyString_FromString(valueBuf); if (obNew==NULL) { ReportPythonError(PYS_E_NO_MEMORY_FOR_SYS_PATH); --- 1200,1204 ---- return NULL; } ! PyObject *obNew = PyWinObject_FromTCHAR(valueBuf); if (obNew==NULL) { ReportPythonError(PYS_E_NO_MEMORY_FOR_SYS_PATH); *************** *** 1208,1212 **** } // Find the last "." in the name, and assume it is a module name. ! char *classNamePos = strrchr(fname, '.'); if (classNamePos==NULL) { ReportError(PYS_E_CANT_LOCATE_MODULE_NAME); --- 1211,1215 ---- } // Find the last "." in the name, and assume it is a module name. ! TCHAR *classNamePos = _tcsrchr(fname, _T('.')); if (classNamePos==NULL) { ReportError(PYS_E_CANT_LOCATE_MODULE_NAME); *************** *** 1217,1226 **** // PyImport_ImportModule("foo.bar") will return 'foo', not bar. *classNamePos++ = '\0'; ! module = PyImport_ImportModule(fname); if (module==NULL) { ReportPythonError(E_PYS_NO_MODULE); return NULL; } ! PyObject *pyclass = PyObject_GetAttrString(module, classNamePos); Py_DECREF(module); if (pyclass==NULL) { --- 1220,1233 ---- // PyImport_ImportModule("foo.bar") will return 'foo', not bar. *classNamePos++ = '\0'; ! PyObject *obname=PyWinObject_FromTCHAR(fname); ! module = PyImport_Import(obname); ! Py_DECREF(obname); if (module==NULL) { ReportPythonError(E_PYS_NO_MODULE); return NULL; } ! PyObject *obclassName=PyWinObject_FromTCHAR(classNamePos); ! PyObject *pyclass = PyObject_GetAttr(module, obclassName); ! Py_DECREF(obclassName); Py_DECREF(module); if (pyclass==NULL) { *************** *** 1270,1281 **** } ! BOOL LocatePythonServiceClassString( TCHAR *svcName, char *buf, int cchBuf) { ! char keyName[1024]; // If not error loading, and not an empty string // (NOTE: Embedding a resource to specify the service name is // deprecated) ! if (LoadStringA(GetModuleHandle(NULL), RESOURCE_SERVICE_NAME, buf, cchBuf)>1) // Get out of here now! return TRUE; --- 1277,1288 ---- } ! BOOL LocatePythonServiceClassString( TCHAR *svcName, TCHAR *buf, int cchBuf) { ! TCHAR keyName[1024]; // If not error loading, and not an empty string // (NOTE: Embedding a resource to specify the service name is // deprecated) ! if (LoadString(GetModuleHandle(NULL), RESOURCE_SERVICE_NAME, buf, cchBuf)>1) // Get out of here now! return TRUE; *************** *** 1283,1294 **** HKEY key = NULL; BOOL ok = TRUE; ! wsprintfA(keyName, "System\\CurrentControlSet\\Services\\%S\\PythonClass", svcName); ! if (RegOpenKeyA(HKEY_LOCAL_MACHINE, keyName, &key) != ERROR_SUCCESS) { ReportAPIError(PYS_E_API_CANT_LOCATE_PYTHON_CLASS); return FALSE; } DWORD dataType; ! DWORD valueBufSize = cchBuf; ! if ((RegQueryValueExA(key, "", 0, &dataType, (LPBYTE)buf, &valueBufSize)!=ERROR_SUCCESS) || (dataType != REG_SZ)) { ReportAPIError(PYS_E_API_CANT_LOCATE_PYTHON_CLASS); --- 1290,1303 ---- HKEY key = NULL; BOOL ok = TRUE; ! _sntprintf(keyName, sizeof(keyName)/sizeof(keyName[0]), ! _T("System\\CurrentControlSet\\Services\\%s\\PythonClass"), ! svcName); ! if (RegOpenKey(HKEY_LOCAL_MACHINE, keyName, &key) != ERROR_SUCCESS) { ReportAPIError(PYS_E_API_CANT_LOCATE_PYTHON_CLASS); return FALSE; } DWORD dataType; ! DWORD valueBufSize = cchBuf * sizeof(TCHAR); ! if ((RegQueryValueEx(key, NULL, 0, &dataType, (LPBYTE)buf, &valueBufSize)!=ERROR_SUCCESS) || (dataType != REG_SZ)) { ReportAPIError(PYS_E_API_CANT_LOCATE_PYTHON_CLASS); *************** *** 1384,1389 **** #define GPEM_ERROR(what) {errorMsg = "<Error getting traceback - " ## what ## ">";goto done;} ! static char *GetPythonTraceback(PyObject *exc_tb) { char *result = NULL; char *errorMsg = NULL; --- 1393,1400 ---- #define GPEM_ERROR(what) {errorMsg = "<Error getting traceback - " ## what ## ">";goto done;} ! static char *GetPythonTraceback(PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb) { + // Sleep (30000); // Time enough to attach the debugger (barely) + PyErr_Clear(); char *result = NULL; char *errorMsg = NULL; *************** *** 1397,1401 **** --- 1408,1418 ---- /* Import the modules we need - cStringIO and traceback */ + #if (PY_VERSION_HEX < 0x03000000) modStringIO = PyImport_ImportModule("cStringIO"); + #else + // In py3k, cStringIO is in "io" + modStringIO = PyImport_ImportModule("io"); + #endif + if (modStringIO==NULL) GPEM_ERROR("cant import cStringIO"); modTB = PyImport_ImportModule("traceback"); *************** *** 1409,1423 **** /* Get the traceback.print_exception function, and call it. */ ! obFuncTB = PyObject_GetAttrString(modTB, "print_tb"); ! if (obFuncTB==NULL) GPEM_ERROR("cant find traceback.print_tb"); ! argsTB = Py_BuildValue("OOO", ! exc_tb ? exc_tb : Py_None, ! Py_None, ! obStringIO); ! if (argsTB==NULL) GPEM_ERROR("cant make print_tb arguments"); obResult = PyObject_CallObject(obFuncTB, argsTB); ! if (obResult==NULL) GPEM_ERROR("traceback.print_tb() failed"); ! /* Now call the getvalue() method in the StringIO instance */ Py_DECREF(obFuncStringIO); --- 1426,1459 ---- /* Get the traceback.print_exception function, and call it. */ ! obFuncTB = PyObject_GetAttrString(modTB, "print_exception"); ! if (obFuncTB==NULL) GPEM_ERROR("cant find traceback.print_exception"); ! argsTB = Py_BuildValue("OOOOO" ! #if (PY_VERSION_HEX >= 0x03000000) ! "i" ! // Py3k has added an undocumented 'chain' argument which defaults to True ! // and causes all kinds of exceptions while trying to print a goddam exception ! #endif ! , ! exc_type ? exc_type : Py_None, ! exc_value ? exc_value : Py_None, ! exc_tb ? exc_tb : Py_None, ! Py_None, // limit ! obStringIO ! #if (PY_VERSION_HEX >= 0x03000000) ! ,0 // Goddam undocumented 'chain' param, which defaults to True ! #endif ! ); ! if (argsTB==NULL) GPEM_ERROR("cant make print_exception arguments"); obResult = PyObject_CallObject(obFuncTB, argsTB); ! if (obResult==NULL){ ! // Chain parameter when True causes traceback.print_exception to fail, leaving no ! // way to see what the original problem is, or even what error print_exc raises ! // PyObject *t, *v, *tb; ! // PyErr_Fetch(&t, &v, &tb); ! // PyUnicodeObject *uo=(PyUnicodeObject *)v; ! // DebugBreak(); ! GPEM_ERROR("traceback.print_exception() failed"); ! } /* Now call the getvalue() method in the StringIO instance */ Py_DECREF(obFuncStringIO); *************** *** 1429,1435 **** /* And it should be a string all ready to go - duplicate it. */ ! if (!PyString_Check(obResult)) GPEM_ERROR("getvalue() did not return a string"); ! result = strdup(PyString_AsString(obResult)); done: if (result==NULL && errorMsg != NULL) --- 1465,1477 ---- /* And it should be a string all ready to go - duplicate it. */ ! if (PyString_Check(obResult)) ! result = strdup(PyString_AsString(obResult)); ! #if (PY_VERSION_HEX >= 0x03000000) ! else if (PyUnicode_Check(obResult)) ! result = strdup(_PyUnicode_AsString(obResult)); ! #endif ! else GPEM_ERROR("getvalue() did not return a string"); ! done: if (result==NULL && errorMsg != NULL) *************** *** 1448,1458 **** { if (PyErr_Occurred()) { ! LPTSTR inserts[4]; ! inserts[3] = NULL; // terminate array PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); ! WCHAR *szTracebackUse = L"<No memory!>"; // default. ! WCHAR *szTraceback = NULL; // to be freed. ! char *szmbTraceback = GetPythonTraceback(traceback); if (szmbTraceback) { int tb_len = strlen(szmbTraceback) + 1; --- 1490,1499 ---- { if (PyErr_Occurred()) { ! LPTSTR inserts[4] = {NULL, NULL, NULL, NULL}; PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); ! TCHAR *szTracebackUse = L"<No memory!>"; // default. ! TCHAR *szTraceback = NULL; // to be freed. ! char *szmbTraceback = GetPythonTraceback(type, value, traceback); if (szmbTraceback) { int tb_len = strlen(szmbTraceback) + 1; *************** *** 1467,1480 **** } inserts[0] = szTracebackUse; - PyObject *obStr = PyObject_Str(type); - PyWinObject_AsWCHAR(obStr, inserts+1); - Py_XDECREF(obStr); - obStr = PyObject_Str(value); - PyWinObject_AsWCHAR(PyObject_Str(obStr), inserts+2); - Py_XDECREF(obStr); ReportError(code, (LPCTSTR *)inserts); if (szTraceback) free(szTraceback); ! PyWinObject_FreeWCHAR(inserts[1]); ! PyWinObject_FreeWCHAR(inserts[2]); if (bServiceDebug) { // If debugging, restore for traceback print, PyErr_Restore(type, value, traceback); --- 1508,1514 ---- } inserts[0] = szTracebackUse; ReportError(code, (LPCTSTR *)inserts); if (szTraceback) free(szTraceback); ! if (bServiceDebug) { // If debugging, restore for traceback print, PyErr_Restore(type, value, traceback); *************** *** 1608,1612 **** // Our EXE entry point. ! int main(int argc, char **argv) { PyObject *module, *f; --- 1642,1646 ---- // Our EXE entry point. ! int _tmain(int argc, TCHAR **argv) { PyObject *module, *f; *************** *** 1616,1619 **** --- 1650,1654 ---- Py_Initialize(); PyEval_InitThreads(); + module = PyImport_ImportModule("site"); // ??? remove when site bug is fixed ??? module = PyImport_ImportModule("servicemanager"); if (!module) goto failed; *************** *** 1621,1630 **** Py_DECREF(module); if (!f) goto failed; ! if (!PyString_Check(f)) { ! PyErr_SetString(PyExc_TypeError, "servicemanager.__file__ is not a string!"); goto failed; } ! // now get the handle to the DLL, and call the main function. ! hmod = GetModuleHandleA(PyString_AsString(f)); Py_DECREF(f); if (!hmod) { --- 1656,1670 ---- Py_DECREF(module); if (!f) goto failed; ! ! // now get the handle to the DLL, and call the main function. ! if (PyString_Check(f)) ! hmod = GetModuleHandleA(PyString_AsString(f)); ! else if (PyUnicode_Check(f)) ! hmod = GetModuleHandleW(PyUnicode_AsUnicode(f)); ! else{ ! PyErr_SetString(PyExc_TypeError, "servicemanager.__file__ is not a string or unicode !"); goto failed; } ! Py_DECREF(f); if (!hmod) { *************** *** 1642,1647 **** PyEval_ReleaseThread(threadState); ! typedef int (* FNPythonService_main)(int argc, char **argv); ! return ((FNPythonService_main)proc)(argc, argv); failed: fprintf(stderr, "PythonService was unable to locate the service manager. " --- 1682,1687 ---- PyEval_ReleaseThread(threadState); ! typedef int (* FNPythonService_main)(int argc, TCHAR **argv); ! return (*(FNPythonService_main)proc)(argc, argv); failed: fprintf(stderr, "PythonService was unable to locate the service manager. " |