- Timestamp:
- Feb 25, 2019, 1:57:11 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/prototype-v0/zoo-project/zoo-kernel/service_internal_python.c
r890 r896 146 146 */ 147 147 int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ 148 148 149 char *pythonpath; 149 150 char *python_path; 151 152 #ifdef WIN32 153 char* os_pathsep = ";"; 154 #else 155 char* os_pathsep = ":"; 156 #endif 157 150 158 maps* m=*main_conf; 151 159 maps* inputs=*real_inputs; 152 160 maps* outputs=*real_outputs; 153 161 map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); 154 char *ntmp=tmp0->value; 162 char *ntmp=tmp0->value; 155 163 map* tmp=NULL; 156 164 int hasToClean=0; 157 tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); 165 tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH"); 166 167 map* kvp = NULL; 168 char* libPath = NULL; 169 if (hasvalue(*main_conf, "main", "libPath", &kvp)) { 170 libPath = kvp->value; 171 } 172 else { 173 libPath = ""; 174 } 175 158 176 #ifdef DEBUG 159 177 fprintf(stderr,"PYTHON SUPPORT \n"); … … 172 190 python_path=cwdMap->value; 173 191 else 174 python_path=(char*)"."; 192 python_path=(char*)"."; 175 193 } 176 194 tmp=NULL; 177 195 tmp=getMap(request,"metapath"); 178 196 if(tmp!=NULL && strcmp(tmp->value,"")!=0){ 179 pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char)); 197 //pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char)); 198 pythonpath = (char*)malloc((5 + strlen(python_path) + strlen(ntmp) + strlen(tmp->value) + strlen(libPath)) * sizeof(char)); 180 199 #ifdef WIN32 181 sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); 200 //sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); 201 sprintf(pythonpath, "%s/%s/;%s;%s", ntmp, tmp->value, python_path, libPath); 182 202 #else 183 sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); 203 //sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); 204 sprintf(pythonpath, "%s/%s/:%s:%s", ntmp, tmp->value, python_path, libPath); 184 205 #endif 185 206 } 186 207 else{ 187 pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char)); 208 //pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char)); 209 pythonpath = (char*)malloc((3 + strlen(python_path) + strlen(ntmp) + strlen(libPath)) * sizeof(char)); 188 210 #ifdef WIN32 189 sprintf(pythonpath,"%s;%s",ntmp,python_path); 211 //sprintf(pythonpath,"%s;%s",ntmp,python_path); 212 sprintf(pythonpath, "%s;%s;%s", ntmp, python_path, libPath); 190 213 #else 191 sprintf(pythonpath,"%s:%s",ntmp,python_path); 192 #endif 214 //sprintf(pythonpath,"%s:%s",ntmp,python_path); 215 sprintf(pythonpath, "%s:%s:%s", ntmp, python_path, libPath); 216 #endif 193 217 } 194 218 #ifdef DEBUG 195 219 fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); 196 220 #endif 197 #ifndef WIN32 198 setenv("PYTHONPATH",pythonpath,1); 199 #else 221 map* home = NULL; 222 // knut: also set PYTHONHOME environment variable so that Python can load standard modules 223 #ifndef WIN32 224 setenv("PYTHONPATH",pythonpath,1); 225 //= getMapFromMaps(*main_conf, "env", "PYTHONHOME"); 226 if (hasvalue(*main_conf, "env", "PYTHONHOME", &home)) { 227 setenv("PYTHONHOME", home->value, 1); // overwrite 228 } 229 #else 200 230 SetEnvironmentVariable("PYTHONPATH",pythonpath); 201 231 char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char)); … … 203 233 _putenv(toto); 204 234 free(toto); 205 #endif 235 if (hasvalue(*main_conf, "env", "PYTHONHOME", &home)) { 236 SetEnvironmentVariable("PYTHONHOME", home->value); 237 } 238 char buffer[128]; 239 #endif 206 240 if(hasToClean>0) 207 241 free(python_path); … … 209 243 210 244 PyThreadState *mainstate; 211 #if PY_MAJOR_VERSION >= 3 212 PyImport_AppendInittab("zoo", init_zoo); 245 #if PY_MAJOR_VERSION >= 3 246 PyImport_AppendInittab("zoo", init_zoo); 213 247 #else 214 248 PyEval_InitThreads(); 215 #endif 216 Py_Initialize(); 217 #if PY_MAJOR_VERSION >= 3 249 #endif 250 Py_Initialize(); 251 #if PY_MAJOR_VERSION >= 3 218 252 PyEval_InitThreads(); 219 PyImport_ImportModule("zoo"); 253 PyImport_ImportModule("zoo"); 220 254 #else 221 255 init_zoo(); 222 #endif 256 #endif 223 257 mainstate = PyThreadState_Swap(NULL); 224 258 PyEval_ReleaseLock(); … … 240 274 char *mn=(char*)malloc((strlen(mps)+strlen(tmp->value)+2)*sizeof(char)); 241 275 sprintf(mn,"%s.%s",mps,tmp->value); 242 pName = PyString_FromString(mn); 276 pName = PyString_FromString(mn); 243 277 free(mn); 244 278 free(mps); … … 251 285 errorException (m, "Unable to parse serviceProvider please check your zcfg file.", "NoApplicableCode", NULL); 252 286 exit(-1); 253 } 254 pModule = PyImport_Import(pName); 287 } 288 pModule = PyImport_Import(pName); 255 289 int res=SERVICE_FAILED; 256 290 if (pModule != NULL) { … … 267 301 PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); 268 302 PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); 269 pValue = PyObject_CallObject(pFunc, pArgs); 303 pValue = PyObject_CallObject(pFunc, pArgs); 270 304 if (pValue != NULL) { 271 305 res=PyInt_AsLong(pValue); … … 275 309 free(*main_conf); 276 310 *main_conf=mapsFromPyDict(arg1); 277 *real_outputs=mapsFromPyDict(arg3); 311 *real_outputs=mapsFromPyDict(arg3); 278 312 #ifdef DEBUG 279 313 fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); … … 301 335 #endif 302 336 PyThreadState_Swap(mainstate); 303 Py_Finalize(); 337 Py_Finalize(); 304 338 return res; 305 339 }
Note: See TracChangeset
for help on using the changeset viewer.