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

Function LoadPythonServiceClass

win32/src/PythonService.cpp:1185–1241  ·  view source on GitHub ↗

Given the string in form [path\]module.ClassName, return an instance of the class

Source from the content-addressed store, hash-verified

1183// Given the string in form [path\]module.ClassName, return
1184// an instance of the class
1185PyObject *LoadPythonServiceClass(TCHAR *svcInitString)
1186{
1187 TCHAR valueBuf[512];
1188 // Initialize Python
1189 PyService_InitPython();
1190 _tcsncpy(valueBuf, svcInitString, sizeof(valueBuf)/sizeof(valueBuf[0]));
1191 // Find the last "\\"
1192 TCHAR *sep = _tcsrchr(valueBuf, _T('\\'));
1193 TCHAR *fname;
1194 if (sep) {
1195 *sep = '\0';
1196 fname = sep+1;
1197 // Stick the Path on the Python sys.path.
1198 PyObject *obPath = PySys_GetObject("path");
1199 if (obPath==NULL) {
1200 ReportPythonError(PYS_E_NO_SYS_PATH);
1201 return NULL;
1202 }
1203 PyObject *obNew = PyWinObject_FromTCHAR(valueBuf);
1204 if (obNew==NULL) {
1205 ReportPythonError(PYS_E_NO_MEMORY_FOR_SYS_PATH);
1206 return NULL;
1207 }
1208 // Insert the Path at the beginning to avoid locating a
1209 // different module of the same name earlier on the path.
1210 PyList_Insert(obPath, 0, obNew);
1211 Py_DECREF(obNew);
1212 } else {
1213 fname = valueBuf;
1214 }
1215 // Find the last "." in the name, and assume it is a module name.
1216 TCHAR *classNamePos = _tcsrchr(fname, _T('.'));
1217 if (classNamePos==NULL) {
1218 ReportError(PYS_E_CANT_LOCATE_MODULE_NAME);
1219 return NULL;
1220 }
1221 PyObject *module;
1222 // XXX - does this work for packages? I fear that like Python,
1223 // PyImport_ImportModule("foo.bar") will return 'foo', not bar.
1224 *classNamePos++ = '\0';
1225 PyObject *obname=PyWinObject_FromTCHAR(fname);
1226 module = PyImport_Import(obname);
1227 Py_DECREF(obname);
1228 if (module==NULL) {
1229 ReportPythonError(E_PYS_NO_MODULE);
1230 return NULL;
1231 }
1232 PyObject *obclassName=PyWinObject_FromTCHAR(classNamePos);
1233 PyObject *pyclass = PyObject_GetAttr(module, obclassName);
1234 Py_DECREF(obclassName);
1235 Py_DECREF(module);
1236 if (pyclass==NULL) {
1237 ReportPythonError(E_PYS_NO_CLASS);
1238 return NULL;
1239 }
1240 return pyclass;
1241}
1242

Callers 1

service_mainFunction · 0.85

Calls 4

PyService_InitPythonFunction · 0.85
ReportPythonErrorFunction · 0.85
ReportErrorFunction · 0.85
PyWinObject_FromTCHARFunction · 0.70

Tested by

no test coverage detected