Changeset 917 for trunk/zoo-project/zoo-kernel/service_internal_python.c
- Timestamp:
- May 7, 2019, 2:17:08 PM (6 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
False
/branches/prototype-v0 merged eligible
-
Property
svn:mergeinfo
set to
False
-
trunk/zoo-project/zoo-kernel/service_internal_python.c
r790 r917 57 57 * from the ZOO-Kernel Python environment: 58 58 * - "_" corresponding to the PythonTranslate function 59 * - "upd te_status" corresponding to the PythonUpdateStatus function59 * - "update_status" corresponding to the PythonUpdateStatus function 60 60 * @see PythonTranslate, PythonUpdateStatus 61 61 */ … … 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"); … … 163 181 fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); 164 182 #endif 165 python_path=(char*)malloc((strlen(tmp->value) )*sizeof(char));183 python_path=(char*)malloc((strlen(tmp->value)+1)*sizeof(char)); 166 184 sprintf(python_path,"%s",tmp->value); 167 185 hasToClean=1; … … 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)); 202 232 sprintf(toto,"PYTHONPATH=%s",pythonpath); 203 putenv(toto);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)); … … 281 315 dumpMaps(*real_outputs); 282 316 #endif 283 }else{ 317 }else{ 284 318 PythonZooReport(m,tmp->value,0); 285 319 res=-1; … … 301 335 #endif 302 336 PyThreadState_Swap(mainstate); 303 Py_Finalize(); 337 Py_Finalize(); 304 338 return res; 305 339 } … … 323 357 char *pbt=NULL; 324 358 if(PyString_Check(trace)){ 325 pbt=(char*)malloc(( 7+strlen(PyString_AsString(trace))+1)*sizeof(char));359 pbt=(char*)malloc((8+strlen(PyString_AsString(trace)))*sizeof(char)); 326 360 sprintf(pbt,"TRACE: %s",PyString_AsString(trace)); 327 361 } 328 362 else 329 363 fprintf(stderr,"EMPTY TRACE ?"); 330 364 331 365 trace=NULL; 332 366 … … 425 459 map* isArray=getMap(tmp,"isArray"); 426 460 map* size=getMap(tmp,"size"); 461 map* useFile=getMap(tmp,"use_file"); 462 map* cacheFile=getMap(tmp,"cache_file"); 427 463 map* tmap=getMapType(tmp); 428 464 while(tmp!=NULL){ … … 435 471 PyObject* mvalue=PyList_New(cnt); 436 472 PyObject* svalue=PyList_New(cnt); 473 PyObject* cvalue=PyList_New(cnt); 437 474 438 475 for(int i=0;i<cnt;i++){ 439 476 440 map* vMap=getMapArray(tmp,"value",i); 441 map* sMap=getMapArray(tmp,"size",i); 477 map* vMap=getMapArray(t,"value",i); 478 map* uMap=getMapArray(t,"use_file",i); 479 map* sMap=getMapArray(t,"size",i); 480 map* cMap=getMapArray(t,"cache_file",i); 442 481 443 482 if(vMap!=NULL){ … … 445 484 PyObject* lvalue; 446 485 PyObject* lsvalue; 447 if(sMap==NULL){ 486 PyObject* lcvalue; 487 if(sMap==NULL || uMap!=NULL){ 448 488 lvalue=PyString_FromString(vMap->value); 449 lsvalue=Py_None;450 489 } 451 490 else{ 452 491 lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); 492 } 493 if(sMap!=NULL){ 453 494 lsvalue=PyString_FromString(sMap->value); 454 495 hasSize=1; 455 496 } 497 else 498 lsvalue=Py_None; 499 if(uMap!=NULL){ 500 lcvalue=PyString_FromString(cMap->value);; 501 }else 502 lcvalue=Py_None; 456 503 457 504 if(PyList_SetItem(value,i,lvalue)<0){ … … 463 510 return NULL; 464 511 } 512 if(PyList_SetItem(cvalue,i,lcvalue)<0){ 513 fprintf(stderr,"Unable to set key value pair..."); 514 return NULL; 515 } 465 516 } 466 517 518 PyObject* lmvalue; 467 519 map* mMap=getMapArray(tmp,tmap->name,i); 468 PyObject* lmvalue;469 520 if(mMap!=NULL){ 470 521 lmvalue=PyString_FromString(mMap->value); … … 487 538 return NULL; 488 539 } 540 if(PyDict_SetItem(res,PyString_FromString("cache_file"),cvalue)<0){ 541 fprintf(stderr,"Unable to set key value pair..."); 542 return NULL; 543 } 489 544 if(hasSize>0) 490 545 if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ … … 493 548 } 494 549 } 495 else if(size!=NULL ){550 else if(size!=NULL && useFile==NULL){ 496 551 PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); 497 552 if(PyDict_SetItem(res,name,value)<0){
Note: See TracChangeset
for help on using the changeset viewer.