MCPcopy Create free account
hub / github.com/mhammond/pywin32 / PyService_InitPython

Function PyService_InitPython

win32/src/PythonService.cpp:600–655  ·  view source on GitHub ↗

Couple of helpers for the service manager

Source from the content-addressed store, hash-verified

598
599// Couple of helpers for the service manager
600static void PyService_InitPython()
601{
602 // XXX - this assumes GIL held, so no races possible
603 static BOOL have_init = FALSE;
604 if (have_init)
605 return;
606 have_init = TRUE;
607 // Often for a service, __argv[0] will be just "ExeName", rather
608 // than "c:\path\to\ExeName.exe"
609 // This, however, shouldnt be a problem, as Python itself
610 // knows how to get the .EXE name when it needs.
611 int pyargc;
612#if (PY_VERSION_HEX < 0x03000000)
613 pyargc = 0;
614 char **pyargv = (char **)malloc(sizeof(char *) * __argc);
615 if (pyargv) {
616 for (;pyargc<__argc;pyargc++) {
617 pyargv[pyargc] = NarrowString(__wargv[pyargc]);
618 if (!pyargv[pyargc]) {
619 break;
620 }
621 }
622 }
623#else
624 WCHAR **pyargv = CommandLineToArgvW(GetCommandLineW(), &pyargc);
625#endif
626 if (pyargv)
627 Py_SetProgramName(pyargv[0]);
628
629#ifdef BUILD_FREEZE
630 PyInitFrozenExtensions();
631#endif
632 Py_Initialize();
633#ifdef BUILD_FREEZE
634 PyWinFreeze_ExeInit();
635#endif
636 // Ensure we are set for threading.
637 PyEval_InitThreads();
638 // Notes about argv: When debugging a service, the argv is currently
639 // the *full* args, including the "-debug servicename" args. This
640 // isn't ideal, but has been this way for a few builds, and a good
641 // fix isn't clear - should 'servicename' be presented in argv, even
642 // though it never is when running as a real service?
643 if (pyargv)
644 PySys_SetArgv(pyargc, pyargv);
645#if (PY_VERSION_HEX < 0x03000000)
646 initservicemanager();
647 // free the argv we created above
648 for (int i=0;i<pyargc;i++)
649 free(pyargv[i]);
650 free(pyargv);
651#else
652 PyInit_servicemanager();
653 LocalFree(pyargv);
654#endif
655}
656
657/*************************************************************************

Callers 1

LoadPythonServiceClassFunction · 0.85

Calls 1

NarrowStringFunction · 0.85

Tested by

no test coverage detected