[579] | 1 | /* |
---|
[1] | 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[453] | 4 | * Copyright (c) 2009-2014 GeoLabs SARL |
---|
[1] | 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "service_internal_python.h" |
---|
| 26 | |
---|
[579] | 27 | /** |
---|
| 28 | * The state for the zoo Python module |
---|
| 29 | */ |
---|
[392] | 30 | struct module_state { |
---|
[579] | 31 | PyObject *error; |
---|
[392] | 32 | }; |
---|
| 33 | |
---|
| 34 | #if PY_MAJOR_VERSION >= 3 |
---|
| 35 | #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) |
---|
[453] | 36 | #define PyInt_FromLong PyLong_FromLong |
---|
| 37 | #define PyInt_AsLong PyLong_AsLong |
---|
| 38 | #define PyString_FromString PyUnicode_FromString |
---|
| 39 | #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
---|
| 40 | #define PyString_Check PyUnicode_Check |
---|
| 41 | #define PyString_AsString _PyUnicode_AsString |
---|
| 42 | #define PyString_Size PyUnicode_GetSize |
---|
[392] | 43 | #else |
---|
| 44 | #define GETSTATE(m) (&_state) |
---|
| 45 | static struct module_state _state; |
---|
| 46 | #endif |
---|
| 47 | |
---|
[579] | 48 | /** |
---|
| 49 | * The exception for the zoo Python module |
---|
| 50 | */ |
---|
[368] | 51 | static PyObject* ZooError; |
---|
| 52 | |
---|
[579] | 53 | /** |
---|
| 54 | * Function definitions for the zoo Python Module |
---|
| 55 | * |
---|
| 56 | * Define the following functions available from a service loaded and running |
---|
| 57 | * from the ZOO-Kernel Python environment: |
---|
| 58 | * - "_" corresponding to the PythonTranslate function |
---|
[917] | 59 | * - "update_status" corresponding to the PythonUpdateStatus function |
---|
[579] | 60 | * @see PythonTranslate, PythonUpdateStatus |
---|
| 61 | */ |
---|
[368] | 62 | PyMethodDef zooMethods[] = { |
---|
[376] | 63 | {"_", PythonTranslate, METH_VARARGS, "Translate a string using the zoo-services textdomain."}, |
---|
[368] | 64 | {"update_status", PythonUpdateStatus, METH_VARARGS, "Update status percentage of a running process."}, |
---|
| 65 | {NULL, NULL, 0, NULL} /* tempt not the blade, all fear the sentinel */ |
---|
| 66 | }; |
---|
| 67 | |
---|
[392] | 68 | #if PY_MAJOR_VERSION >= 3 |
---|
| 69 | |
---|
| 70 | static int myextension_traverse(PyObject *m, visitproc visit, void *arg) { |
---|
| 71 | Py_VISIT(GETSTATE(m)->error); |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | static int myextension_clear(PyObject *m) { |
---|
| 76 | Py_CLEAR(GETSTATE(m)->error); |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | static struct PyModuleDef moduledef = { |
---|
| 81 | PyModuleDef_HEAD_INIT, |
---|
| 82 | "zoo", |
---|
| 83 | NULL, |
---|
| 84 | sizeof(struct module_state), |
---|
| 85 | zooMethods, |
---|
| 86 | NULL, |
---|
| 87 | myextension_traverse, |
---|
| 88 | myextension_clear, |
---|
| 89 | NULL |
---|
| 90 | }; |
---|
| 91 | #endif |
---|
| 92 | |
---|
[579] | 93 | /** |
---|
| 94 | * Function to create and initialize the zoo Python module |
---|
| 95 | * |
---|
| 96 | * @return the Python module (for Python versions < 3, nothing for version >=3) |
---|
| 97 | */ |
---|
[368] | 98 | PyMODINIT_FUNC init_zoo(){ |
---|
| 99 | PyObject *tmp,*d; |
---|
[392] | 100 | PyObject *module = |
---|
| 101 | #if PY_MAJOR_VERSION >= 3 |
---|
| 102 | PyModule_Create(&moduledef); |
---|
| 103 | #else |
---|
| 104 | Py_InitModule("zoo", zooMethods); |
---|
| 105 | #endif |
---|
| 106 | if (module == NULL){ |
---|
| 107 | #if PY_MAJOR_VERSION >= 3 |
---|
| 108 | return NULL; |
---|
| 109 | #else |
---|
[368] | 110 | return; |
---|
[392] | 111 | #endif |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | struct module_state *st = GETSTATE(module); |
---|
| 115 | |
---|
[368] | 116 | d = PyModule_GetDict(module); |
---|
| 117 | tmp = PyInt_FromLong(3); |
---|
| 118 | PyDict_SetItemString(d, "SERVICE_SUCCEEDED", tmp); |
---|
| 119 | Py_DECREF(tmp); |
---|
| 120 | |
---|
| 121 | tmp = PyInt_FromLong(4); |
---|
| 122 | PyDict_SetItemString(d, "SERVICE_FAILED", tmp); |
---|
| 123 | Py_DECREF(tmp); |
---|
| 124 | |
---|
[465] | 125 | tmp = PyString_FromString(ZOO_VERSION); |
---|
| 126 | PyDict_SetItemString(d, "VERSION", tmp); |
---|
| 127 | Py_DECREF(tmp); |
---|
| 128 | |
---|
[490] | 129 | ZooError = PyErr_NewException((char*)"zoo.error", NULL, NULL); |
---|
[368] | 130 | Py_INCREF(ZooError); |
---|
| 131 | PyModule_AddObject(module, "error", ZooError); |
---|
[392] | 132 | #if PY_MAJOR_VERSION >= 3 |
---|
| 133 | return module; |
---|
| 134 | #endif |
---|
[368] | 135 | } |
---|
| 136 | |
---|
[579] | 137 | /** |
---|
[580] | 138 | * Load a Python module then run the function corresponding to the service |
---|
[579] | 139 | * by passing the conf, inputs and outputs parameters by reference. |
---|
| 140 | * |
---|
| 141 | * @param main_conf the conf maps containing the main.cfg settings |
---|
| 142 | * @param request the map containing the HTTP request |
---|
| 143 | * @param s the service structure |
---|
| 144 | * @param real_inputs the maps containing the inputs |
---|
| 145 | * @param real_outputs the maps containing the outputs |
---|
| 146 | */ |
---|
[1] | 147 | int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){ |
---|
[917] | 148 | |
---|
[451] | 149 | char *pythonpath; |
---|
| 150 | char *python_path; |
---|
[917] | 151 | |
---|
| 152 | #ifdef WIN32 |
---|
[921] | 153 | const char* os_pathsep = ";"; |
---|
[917] | 154 | #else |
---|
[921] | 155 | const char* os_pathsep = ":"; |
---|
[917] | 156 | #endif |
---|
| 157 | |
---|
[1] | 158 | maps* m=*main_conf; |
---|
| 159 | maps* inputs=*real_inputs; |
---|
| 160 | maps* outputs=*real_outputs; |
---|
[392] | 161 | map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd"); |
---|
[917] | 162 | char *ntmp=tmp0->value; |
---|
[1] | 163 | map* tmp=NULL; |
---|
[451] | 164 | int hasToClean=0; |
---|
[917] | 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 | |
---|
[63] | 176 | #ifdef DEBUG |
---|
[9] | 177 | fprintf(stderr,"PYTHON SUPPORT \n"); |
---|
[63] | 178 | #endif |
---|
[1] | 179 | if(tmp!=NULL){ |
---|
[63] | 180 | #ifdef DEBUG |
---|
[9] | 181 | fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value)); |
---|
[63] | 182 | #endif |
---|
[917] | 183 | python_path=(char*)malloc((strlen(tmp->value)+1)*sizeof(char)); |
---|
[9] | 184 | sprintf(python_path,"%s",tmp->value); |
---|
[451] | 185 | hasToClean=1; |
---|
[1] | 186 | } |
---|
| 187 | else{ |
---|
[784] | 188 | map* cwdMap=getMapFromMaps(*main_conf,"main","servicePath"); |
---|
| 189 | if(cwdMap!=NULL) |
---|
| 190 | python_path=cwdMap->value; |
---|
| 191 | else |
---|
[917] | 192 | python_path=(char*)"."; |
---|
[1] | 193 | } |
---|
| 194 | tmp=NULL; |
---|
| 195 | tmp=getMap(request,"metapath"); |
---|
[392] | 196 | if(tmp!=NULL && strcmp(tmp->value,"")!=0){ |
---|
[917] | 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)); |
---|
[1] | 199 | #ifdef WIN32 |
---|
[917] | 200 | //sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path); |
---|
| 201 | sprintf(pythonpath, "%s/%s/;%s;%s", ntmp, tmp->value, python_path, libPath); |
---|
[1] | 202 | #else |
---|
[917] | 203 | //sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path); |
---|
| 204 | sprintf(pythonpath, "%s/%s/:%s:%s", ntmp, tmp->value, python_path, libPath); |
---|
[1] | 205 | #endif |
---|
[392] | 206 | } |
---|
| 207 | else{ |
---|
[917] | 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)); |
---|
[1] | 210 | #ifdef WIN32 |
---|
[917] | 211 | //sprintf(pythonpath,"%s;%s",ntmp,python_path); |
---|
| 212 | sprintf(pythonpath, "%s;%s;%s", ntmp, python_path, libPath); |
---|
[1] | 213 | #else |
---|
[917] | 214 | //sprintf(pythonpath,"%s:%s",ntmp,python_path); |
---|
| 215 | sprintf(pythonpath, "%s:%s:%s", ntmp, python_path, libPath); |
---|
| 216 | #endif |
---|
[392] | 217 | } |
---|
[67] | 218 | #ifdef DEBUG |
---|
[9] | 219 | fprintf(stderr,"PYTHONPATH=%s\n",pythonpath); |
---|
[67] | 220 | #endif |
---|
[917] | 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 |
---|
[1] | 230 | SetEnvironmentVariable("PYTHONPATH",pythonpath); |
---|
[364] | 231 | char* toto=(char*)malloc((strlen(pythonpath)+12)*sizeof(char)); |
---|
| 232 | sprintf(toto,"PYTHONPATH=%s",pythonpath); |
---|
[917] | 233 | _putenv(toto); |
---|
[451] | 234 | free(toto); |
---|
[917] | 235 | if (hasvalue(*main_conf, "env", "PYTHONHOME", &home)) { |
---|
| 236 | SetEnvironmentVariable("PYTHONHOME", home->value); |
---|
| 237 | } |
---|
[943] | 238 | char buffer[128]; |
---|
[917] | 239 | #endif |
---|
[451] | 240 | if(hasToClean>0) |
---|
| 241 | free(python_path); |
---|
[1] | 242 | free(pythonpath); |
---|
[9] | 243 | |
---|
[295] | 244 | PyThreadState *mainstate; |
---|
[917] | 245 | #if PY_MAJOR_VERSION >= 3 |
---|
| 246 | PyImport_AppendInittab("zoo", init_zoo); |
---|
[392] | 247 | #else |
---|
[295] | 248 | PyEval_InitThreads(); |
---|
[917] | 249 | #endif |
---|
| 250 | Py_Initialize(); |
---|
| 251 | #if PY_MAJOR_VERSION >= 3 |
---|
[392] | 252 | PyEval_InitThreads(); |
---|
[917] | 253 | PyImport_ImportModule("zoo"); |
---|
[392] | 254 | #else |
---|
[368] | 255 | init_zoo(); |
---|
[943] | 256 | #endif |
---|
[295] | 257 | mainstate = PyThreadState_Swap(NULL); |
---|
| 258 | PyEval_ReleaseLock(); |
---|
| 259 | PyGILState_STATE gstate; |
---|
| 260 | gstate = PyGILState_Ensure(); |
---|
[1] | 261 | PyObject *pName, *pModule, *pFunc; |
---|
| 262 | tmp=getMap(s->content,"serviceProvider"); |
---|
[505] | 263 | map* mp=getMap(request,"metapath"); |
---|
| 264 | if(tmp!=NULL){ |
---|
| 265 | if(mp!=NULL && strlen(mp->value)>0){ |
---|
| 266 | char *mps=zStrdup(mp->value); |
---|
| 267 | int i,len=strlen(mps); |
---|
| 268 | int j=0; |
---|
| 269 | for(i=0;i<len;i++){ |
---|
| 270 | if(mps[i]=='/'){ |
---|
| 271 | mps[i]='.'; |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | char *mn=(char*)malloc((strlen(mps)+strlen(tmp->value)+2)*sizeof(char)); |
---|
| 275 | sprintf(mn,"%s.%s",mps,tmp->value); |
---|
[917] | 276 | pName = PyString_FromString(mn); |
---|
[505] | 277 | free(mn); |
---|
| 278 | free(mps); |
---|
| 279 | } |
---|
| 280 | else{ |
---|
| 281 | pName = PyString_FromString(tmp->value); |
---|
| 282 | } |
---|
| 283 | } |
---|
[9] | 284 | else{ |
---|
[576] | 285 | errorException (m, "Unable to parse serviceProvider please check your zcfg file.", "NoApplicableCode", NULL); |
---|
[9] | 286 | exit(-1); |
---|
[943] | 287 | } |
---|
| 288 | |
---|
| 289 | pModule = PyImport_Import(pName); |
---|
[1] | 290 | int res=SERVICE_FAILED; |
---|
| 291 | if (pModule != NULL) { |
---|
| 292 | pFunc=PyObject_GetAttrString(pModule,s->name); |
---|
| 293 | if (pFunc && PyCallable_Check(pFunc)){ |
---|
[114] | 294 | PyObject *pValue; |
---|
[1] | 295 | PyDictObject* arg1=PyDict_FromMaps(m); |
---|
| 296 | PyDictObject* arg2=PyDict_FromMaps(inputs); |
---|
| 297 | PyDictObject* arg3=PyDict_FromMaps(outputs); |
---|
| 298 | PyObject *pArgs=PyTuple_New(3); |
---|
[114] | 299 | if (!pArgs) |
---|
| 300 | return -1; |
---|
[1] | 301 | PyTuple_SetItem(pArgs, 0, (PyObject *)arg1); |
---|
| 302 | PyTuple_SetItem(pArgs, 1, (PyObject *)arg2); |
---|
| 303 | PyTuple_SetItem(pArgs, 2, (PyObject *)arg3); |
---|
[943] | 304 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
[1] | 305 | if (pValue != NULL) { |
---|
| 306 | res=PyInt_AsLong(pValue); |
---|
[9] | 307 | freeMaps(real_outputs); |
---|
| 308 | free(*real_outputs); |
---|
[59] | 309 | freeMaps(main_conf); |
---|
| 310 | free(*main_conf); |
---|
[57] | 311 | *main_conf=mapsFromPyDict(arg1); |
---|
[917] | 312 | *real_outputs=mapsFromPyDict(arg3); |
---|
[1] | 313 | #ifdef DEBUG |
---|
| 314 | fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue)); |
---|
| 315 | dumpMaps(inputs); |
---|
[364] | 316 | dumpMaps(*real_outputs); |
---|
[1] | 317 | #endif |
---|
[917] | 318 | }else{ |
---|
[576] | 319 | PythonZooReport(m,tmp->value,0); |
---|
[295] | 320 | res=-1; |
---|
[1] | 321 | } |
---|
| 322 | } |
---|
| 323 | else{ |
---|
| 324 | char tmpS[1024]; |
---|
[295] | 325 | sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value); |
---|
[576] | 326 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
[295] | 327 | res=-1; |
---|
[1] | 328 | } |
---|
| 329 | } else{ |
---|
[576] | 330 | PythonZooReport(m,tmp->value,1); |
---|
[295] | 331 | res=-1; |
---|
[1] | 332 | } |
---|
[392] | 333 | #if PY_MAJOR_VERSION < 3 |
---|
[295] | 334 | PyGILState_Release(gstate); |
---|
[478] | 335 | PyEval_AcquireLock(); |
---|
[392] | 336 | #endif |
---|
[295] | 337 | PyThreadState_Swap(mainstate); |
---|
[943] | 338 | Py_Finalize(); |
---|
[1] | 339 | return res; |
---|
| 340 | } |
---|
| 341 | |
---|
[579] | 342 | /** |
---|
[580] | 343 | * Report Python error which may occur on loading the Python module or at |
---|
[579] | 344 | * runtime. |
---|
| 345 | * |
---|
| 346 | * @param m the conf maps containing the main.cfg settings |
---|
| 347 | * @param module the service name |
---|
| 348 | * @param load 1 if the Python module was not loaded yet |
---|
| 349 | */ |
---|
[576] | 350 | void PythonZooReport(maps* m,const char* module,int load){ |
---|
| 351 | PyObject *pName, *pModule, *pFunc; |
---|
| 352 | PyObject *ptype, *pvalue, *ptraceback,*pValue,*pArgs; |
---|
| 353 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
---|
[927] | 354 | #if PY_MAJOR_VERSION >= 3 |
---|
| 355 | const |
---|
| 356 | #endif |
---|
| 357 | char *pStrErrorMessage = PyString_AsString(pvalue); |
---|
[939] | 358 | const char *tmp0=_("Python module %s cannot be loaded. Message: %s\n"); |
---|
[576] | 359 | |
---|
| 360 | PyObject *trace=PyObject_Str(pvalue); |
---|
| 361 | char *pbt=NULL; |
---|
| 362 | if(PyString_Check(trace)){ |
---|
[917] | 363 | pbt=(char*)malloc((8+strlen(PyString_AsString(trace)))*sizeof(char)); |
---|
[576] | 364 | sprintf(pbt,"TRACE: %s",PyString_AsString(trace)); |
---|
| 365 | } |
---|
| 366 | else |
---|
| 367 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
[917] | 368 | |
---|
[576] | 369 | trace=NULL; |
---|
| 370 | |
---|
| 371 | trace=PyObject_Str(ptype); |
---|
| 372 | if(PyString_Check(trace)){ |
---|
| 373 | char *tpbt=zStrdup(pbt); |
---|
| 374 | if(pbt!=NULL) |
---|
| 375 | free(pbt); |
---|
| 376 | pbt=(char*)malloc((1+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 377 | sprintf(pbt,"%s\n%s",tpbt,PyString_AsString(trace)); |
---|
| 378 | free(tpbt); |
---|
| 379 | } |
---|
| 380 | else |
---|
| 381 | fprintf(stderr,"EMPTY TRACE ?"); |
---|
| 382 | |
---|
| 383 | if(ptraceback!=NULL){ |
---|
| 384 | char *tpbt=zStrdup(pbt); |
---|
| 385 | pName = PyString_FromString("traceback"); |
---|
| 386 | pModule = PyImport_Import(pName); |
---|
| 387 | pArgs = PyTuple_New(1); |
---|
| 388 | PyTuple_SetItem(pArgs, 0, ptraceback); |
---|
| 389 | pFunc = PyObject_GetAttrString(pModule,"format_tb"); |
---|
| 390 | pValue = PyObject_CallObject(pFunc, pArgs); |
---|
| 391 | trace=NULL; |
---|
| 392 | trace=PyObject_Str(pValue); |
---|
| 393 | if(PyString_Check(trace)){ |
---|
| 394 | if(pbt!=NULL) |
---|
| 395 | free(pbt); |
---|
[939] | 396 | const char* format=_("%s\nUnable to run your python process properly. Please check the following messages : %s"); |
---|
[790] | 397 | pbt=(char*)malloc((strlen(format)+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 398 | sprintf(pbt,format,tpbt,PyString_AsString(trace)); |
---|
[576] | 399 | } |
---|
| 400 | else{ |
---|
| 401 | if(pbt!=NULL) |
---|
| 402 | free(pbt); |
---|
[939] | 403 | const char* format=_("%s \n Unable to run your python process properly. Unable to provide any further information."); |
---|
[790] | 404 | pbt=(char*)malloc((strlen(format)+strlen(tpbt)+strlen(PyString_AsString(trace))+1)*sizeof(char)); |
---|
| 405 | sprintf(pbt,format,tpbt); |
---|
[576] | 406 | } |
---|
| 407 | free(tpbt); |
---|
| 408 | } |
---|
| 409 | if(load>0){ |
---|
| 410 | char *tmpS=(char*)malloc((strlen(tmp0)+strlen(module)+strlen(pbt)+1)*sizeof(char)); |
---|
| 411 | sprintf(tmpS,tmp0,module,pbt); |
---|
| 412 | errorException(m,tmpS,"NoApplicableCode",NULL); |
---|
| 413 | free(tmpS); |
---|
| 414 | }else |
---|
| 415 | errorException(m,pbt,"NoApplicableCode",NULL); |
---|
| 416 | free(pbt); |
---|
| 417 | } |
---|
| 418 | |
---|
[579] | 419 | /** |
---|
| 420 | * Convert a maps to a Python dictionary |
---|
| 421 | * |
---|
| 422 | * @param t the maps to convert |
---|
| 423 | * @return a new PyDictObject containing the converted maps |
---|
| 424 | * @see PyDict_FromMap |
---|
[781] | 425 | * @warning make sure to free resources returned by this function |
---|
[579] | 426 | */ |
---|
[1] | 427 | PyDictObject* PyDict_FromMaps(maps* t){ |
---|
| 428 | PyObject* res=PyDict_New( ); |
---|
| 429 | maps* tmp=t; |
---|
| 430 | while(tmp!=NULL){ |
---|
[295] | 431 | PyObject* value=(PyObject*)PyDict_FromMap(tmp->content); |
---|
[790] | 432 | if(tmp->child!=NULL){ |
---|
| 433 | PyObject* cname=PyString_FromString("child"); |
---|
| 434 | PyObject* childs=(PyObject*)PyDict_FromMaps(tmp->child); |
---|
| 435 | if(PyDict_SetItem(value,cname,childs)<0){ |
---|
| 436 | fprintf(stderr,"Unable to set map value ..."); |
---|
| 437 | return NULL; |
---|
| 438 | } |
---|
| 439 | Py_DECREF(cname); |
---|
| 440 | } |
---|
[453] | 441 | PyObject* name=PyString_FromString(tmp->name); |
---|
[295] | 442 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 443 | fprintf(stderr,"Unable to set map value ..."); |
---|
| 444 | return NULL; |
---|
[1] | 445 | } |
---|
[295] | 446 | Py_DECREF(name); |
---|
[1] | 447 | tmp=tmp->next; |
---|
[962] | 448 | } |
---|
[1] | 449 | return (PyDictObject*) res; |
---|
| 450 | } |
---|
| 451 | |
---|
[579] | 452 | /** |
---|
| 453 | * Convert a map to a Python dictionary |
---|
| 454 | * |
---|
| 455 | * @param t the map to convert |
---|
| 456 | * @return a new PyDictObject containing the converted maps |
---|
[781] | 457 | * @warning make sure to free resources returned by this function |
---|
[579] | 458 | */ |
---|
[1] | 459 | PyDictObject* PyDict_FromMap(map* t){ |
---|
| 460 | PyObject* res=PyDict_New( ); |
---|
| 461 | map* tmp=t; |
---|
[360] | 462 | int hasSize=0; |
---|
| 463 | map* isArray=getMap(tmp,"isArray"); |
---|
[58] | 464 | map* size=getMap(tmp,"size"); |
---|
[917] | 465 | map* useFile=getMap(tmp,"use_file"); |
---|
| 466 | map* cacheFile=getMap(tmp,"cache_file"); |
---|
[360] | 467 | map* tmap=getMapType(tmp); |
---|
[1] | 468 | while(tmp!=NULL){ |
---|
[453] | 469 | PyObject* name=PyString_FromString(tmp->name); |
---|
[967] | 470 | if(strcasecmp(tmp->name,"value")==0) { |
---|
[360] | 471 | if(isArray!=NULL){ |
---|
| 472 | map* len=getMap(tmp,"length"); |
---|
| 473 | int cnt=atoi(len->value); |
---|
| 474 | PyObject* value=PyList_New(cnt); |
---|
| 475 | PyObject* mvalue=PyList_New(cnt); |
---|
| 476 | PyObject* svalue=PyList_New(cnt); |
---|
[917] | 477 | PyObject* cvalue=PyList_New(cnt); |
---|
[360] | 478 | |
---|
| 479 | for(int i=0;i<cnt;i++){ |
---|
| 480 | |
---|
[917] | 481 | map* vMap=getMapArray(t,"value",i); |
---|
| 482 | map* uMap=getMapArray(t,"use_file",i); |
---|
| 483 | map* sMap=getMapArray(t,"size",i); |
---|
| 484 | map* cMap=getMapArray(t,"cache_file",i); |
---|
[360] | 485 | |
---|
| 486 | if(vMap!=NULL){ |
---|
| 487 | |
---|
| 488 | PyObject* lvalue; |
---|
| 489 | PyObject* lsvalue; |
---|
[917] | 490 | PyObject* lcvalue; |
---|
| 491 | if(sMap==NULL || uMap!=NULL){ |
---|
[360] | 492 | lvalue=PyString_FromString(vMap->value); |
---|
| 493 | } |
---|
[967] | 494 | else{ |
---|
| 495 | if(strlen(vMap->value)>0) |
---|
| 496 | lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value)); |
---|
| 497 | else |
---|
| 498 | lvalue=Py_None; |
---|
[917] | 499 | } |
---|
| 500 | if(sMap!=NULL){ |
---|
[360] | 501 | lsvalue=PyString_FromString(sMap->value); |
---|
| 502 | hasSize=1; |
---|
| 503 | } |
---|
[917] | 504 | else |
---|
| 505 | lsvalue=Py_None; |
---|
| 506 | if(uMap!=NULL){ |
---|
| 507 | lcvalue=PyString_FromString(cMap->value);; |
---|
| 508 | }else |
---|
| 509 | lcvalue=Py_None; |
---|
[360] | 510 | |
---|
| 511 | if(PyList_SetItem(value,i,lvalue)<0){ |
---|
| 512 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 513 | return NULL; |
---|
| 514 | } |
---|
| 515 | if(PyList_SetItem(svalue,i,lsvalue)<0){ |
---|
| 516 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 517 | return NULL; |
---|
| 518 | } |
---|
[917] | 519 | if(PyList_SetItem(cvalue,i,lcvalue)<0){ |
---|
| 520 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 521 | return NULL; |
---|
| 522 | } |
---|
[360] | 523 | } |
---|
| 524 | |
---|
[917] | 525 | PyObject* lmvalue; |
---|
[360] | 526 | map* mMap=getMapArray(tmp,tmap->name,i); |
---|
| 527 | if(mMap!=NULL){ |
---|
| 528 | lmvalue=PyString_FromString(mMap->value); |
---|
| 529 | }else |
---|
| 530 | lmvalue=Py_None; |
---|
| 531 | |
---|
| 532 | if(PyList_SetItem(mvalue,i,lmvalue)<0){ |
---|
| 533 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 534 | return NULL; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | if(PyDict_SetItem(res,name,value)<0){ |
---|
| 540 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 541 | return NULL; |
---|
| 542 | } |
---|
| 543 | if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){ |
---|
| 544 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 545 | return NULL; |
---|
| 546 | } |
---|
[917] | 547 | if(PyDict_SetItem(res,PyString_FromString("cache_file"),cvalue)<0){ |
---|
| 548 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 549 | return NULL; |
---|
| 550 | } |
---|
[360] | 551 | if(hasSize>0) |
---|
| 552 | if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){ |
---|
| 553 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 554 | return NULL; |
---|
| 555 | } |
---|
| 556 | } |
---|
[917] | 557 | else if(size!=NULL && useFile==NULL){ |
---|
[967] | 558 | PyObject* value; |
---|
| 559 | if(strlen(tmp->value)>0) |
---|
| 560 | value=PyString_FromStringAndSize(tmp->value,atoi(size->value)); |
---|
| 561 | else |
---|
| 562 | value=Py_None; |
---|
[59] | 563 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 564 | Py_DECREF(value); |
---|
[295] | 565 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 566 | return NULL; |
---|
[43] | 567 | } |
---|
[471] | 568 | Py_DECREF(value); |
---|
[58] | 569 | } |
---|
[59] | 570 | else{ |
---|
[453] | 571 | PyObject* value=PyString_FromString(tmp->value); |
---|
[59] | 572 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 573 | Py_DECREF(value); |
---|
[295] | 574 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 575 | return NULL; |
---|
[43] | 576 | } |
---|
[471] | 577 | Py_DECREF(value); |
---|
[59] | 578 | } |
---|
| 579 | } |
---|
| 580 | else{ |
---|
[967] | 581 | if(PyDict_GetItem(res,name)==NULL){ |
---|
[453] | 582 | PyObject* value=PyString_FromString(tmp->value); |
---|
[360] | 583 | if(PyDict_SetItem(res,name,value)<0){ |
---|
[471] | 584 | Py_DECREF(value); |
---|
[360] | 585 | fprintf(stderr,"Unable to set key value pair..."); |
---|
| 586 | return NULL; |
---|
| 587 | } |
---|
[471] | 588 | Py_DECREF(value); |
---|
[43] | 589 | } |
---|
[59] | 590 | } |
---|
| 591 | Py_DECREF(name); |
---|
[1] | 592 | tmp=tmp->next; |
---|
| 593 | } |
---|
| 594 | return (PyDictObject*) res; |
---|
| 595 | } |
---|
| 596 | |
---|
[579] | 597 | /** |
---|
| 598 | * Convert a Python dictionary to a maps |
---|
| 599 | * |
---|
| 600 | * @param t the PyDictObject to convert |
---|
| 601 | * @return a new maps containing the converted PyDictObject |
---|
[781] | 602 | * @warning make sure to free resources returned by this function |
---|
[579] | 603 | */ |
---|
[1] | 604 | maps* mapsFromPyDict(PyDictObject* t){ |
---|
| 605 | maps* res=NULL; |
---|
[682] | 606 | maps* cursor=NULL; |
---|
[1] | 607 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 608 | int nb=PyList_Size(list); |
---|
| 609 | int i; |
---|
[682] | 610 | PyObject* key; |
---|
| 611 | PyObject* value; |
---|
[1] | 612 | for(i=0;i<nb;i++){ |
---|
| 613 | #ifdef DEBUG |
---|
| 614 | fprintf(stderr,">> parsing maps %d\n",i); |
---|
| 615 | #endif |
---|
[682] | 616 | key=PyList_GetItem(list,i); |
---|
| 617 | value=PyDict_GetItem((PyObject*)t,key); |
---|
[1] | 618 | #ifdef DEBUG |
---|
| 619 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 620 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 621 | #endif |
---|
[790] | 622 | cursor=createMaps(PyString_AsString(key)); |
---|
[295] | 623 | cursor->content=mapFromPyDict((PyDictObject*)value); |
---|
[790] | 624 | PyObject* cname=PyString_FromString("child"); |
---|
| 625 | PyObject* childs=PyDict_GetItem((PyObject*)value,cname); |
---|
| 626 | if(childs!=NULL) |
---|
| 627 | cursor->child=mapsFromPyDict((PyDictObject*)childs); |
---|
| 628 | Py_DECREF(cname); |
---|
[1] | 629 | #ifdef DEBUG |
---|
[295] | 630 | dumpMap(cursor->content); |
---|
[1] | 631 | #endif |
---|
| 632 | cursor->next=NULL; |
---|
| 633 | if(res==NULL) |
---|
[59] | 634 | res=dupMaps(&cursor); |
---|
[1] | 635 | else |
---|
| 636 | addMapsToMaps(&res,cursor); |
---|
[59] | 637 | freeMap(&cursor->content); |
---|
| 638 | free(cursor->content); |
---|
| 639 | free(cursor); |
---|
[1] | 640 | #ifdef DEBUG |
---|
| 641 | dumpMaps(res); |
---|
| 642 | fprintf(stderr,">> parsed maps %d\n",i); |
---|
| 643 | #endif |
---|
| 644 | } |
---|
[505] | 645 | Py_DECREF(list); |
---|
[1] | 646 | return res; |
---|
| 647 | } |
---|
| 648 | |
---|
[579] | 649 | /** |
---|
[738] | 650 | * Convert a Python dictionary to a maps |
---|
| 651 | * |
---|
| 652 | * @param t the PyDictObject to convert |
---|
| 653 | * @return a new maps containing the converted PyDictObject |
---|
| 654 | * @warning make sure to free resources returned by this function |
---|
| 655 | */ |
---|
| 656 | maps* _mapsFromPyDict(PyDictObject* t){ |
---|
| 657 | |
---|
| 658 | PyObject* list = PyDict_Keys((PyObject*)t); // new ref |
---|
| 659 | int nb = PyList_Size(list); |
---|
| 660 | |
---|
| 661 | if (nb < 1) { |
---|
| 662 | Py_DECREF(list); |
---|
| 663 | return NULL; |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | maps* ptr = (maps*) malloc(MAPS_SIZE); |
---|
| 667 | maps* res = ptr; |
---|
| 668 | |
---|
| 669 | PyObject* key; |
---|
| 670 | PyObject* value; |
---|
| 671 | |
---|
| 672 | for(int i = 0; i < nb; i++) { |
---|
| 673 | |
---|
| 674 | key = PyList_GetItem(list,i); // borrowed ref |
---|
| 675 | value = PyDict_GetItem((PyObject*) t, key); // borrowed ref |
---|
| 676 | |
---|
| 677 | ptr->name = zStrdup(PyString_AsString(key)); |
---|
| 678 | ptr->content = mapFromPyDict((PyDictObject*) value); |
---|
| 679 | |
---|
| 680 | ptr->next = i < nb - 1 ? (maps*) malloc(MAPS_SIZE) : NULL; |
---|
| 681 | ptr = ptr->next; |
---|
| 682 | } |
---|
| 683 | Py_DECREF(list); |
---|
| 684 | |
---|
| 685 | return res; |
---|
| 686 | } // mapsFromPyDict |
---|
| 687 | |
---|
| 688 | /** |
---|
[579] | 689 | * Convert a Python dictionary to a map |
---|
| 690 | * |
---|
| 691 | * @param t the PyDictObject to convert |
---|
| 692 | * @return a new map containing the converted PyDictObject |
---|
[781] | 693 | * @warning make sure to free resources returned by this function |
---|
[579] | 694 | */ |
---|
[1] | 695 | map* mapFromPyDict(PyDictObject* t){ |
---|
| 696 | map* res=NULL; |
---|
| 697 | PyObject* list=PyDict_Keys((PyObject*)t); |
---|
| 698 | int nb=PyList_Size(list); |
---|
| 699 | int i; |
---|
[682] | 700 | PyObject* key; |
---|
| 701 | PyObject* value; |
---|
[1] | 702 | for(i=0;i<nb;i++){ |
---|
[682] | 703 | key=PyList_GetItem(list,i); |
---|
| 704 | value=PyDict_GetItem((PyObject*)t,key); |
---|
[1] | 705 | #ifdef DEBUG |
---|
| 706 | fprintf(stderr,">> DEBUG VALUES : %s => %s\n", |
---|
| 707 | PyString_AsString(key),PyString_AsString(value)); |
---|
| 708 | #endif |
---|
[790] | 709 | if(strncmp(PyString_AsString(key),"child",5)!=0){ |
---|
| 710 | if(strncmp(PyString_AsString(key),"value",5)==0){ |
---|
[927] | 711 | #if PY_MAJOR_VERSION >= 3 |
---|
| 712 | const |
---|
| 713 | #endif |
---|
| 714 | char *buffer=NULL; |
---|
[790] | 715 | Py_ssize_t size; |
---|
[392] | 716 | #if PY_MAJOR_VERSION >= 3 |
---|
[790] | 717 | if(PyBytes_Check(value)){ |
---|
| 718 | size=PyBytes_Size(value); |
---|
| 719 | buffer=PyBytes_AsString(value); |
---|
[682] | 720 | } |
---|
[790] | 721 | else |
---|
| 722 | if(PyUnicode_Check(value) && PyUnicode_READY(value) == 0){ |
---|
| 723 | buffer=PyUnicode_AsUTF8AndSize(value,&size); |
---|
| 724 | } |
---|
| 725 | else{ |
---|
[738] | 726 | #ifdef DEBUG |
---|
[790] | 727 | fprintf(stderr,"Unsupported return value."); |
---|
[738] | 728 | #endif |
---|
[790] | 729 | return NULL; |
---|
| 730 | } |
---|
[392] | 731 | #else |
---|
[790] | 732 | PyString_AsStringAndSize(value,&buffer,&size); |
---|
[738] | 733 | #endif |
---|
[790] | 734 | res = addToMapWithSize(res,PyString_AsString(key),buffer,size); |
---|
| 735 | }else{ |
---|
[927] | 736 | #if PY_MAJOR_VERSION >= 3 |
---|
| 737 | const |
---|
| 738 | #endif |
---|
| 739 | char* lkey=PyString_AsString(key); |
---|
| 740 | #if PY_MAJOR_VERSION >= 3 |
---|
| 741 | const |
---|
| 742 | #endif |
---|
| 743 | char* lvalue=PyString_AsString(value); |
---|
[790] | 744 | if(res!=NULL){ |
---|
| 745 | if(PyString_Size(value)>0) |
---|
| 746 | addToMap(res,lkey,lvalue); |
---|
| 747 | } |
---|
| 748 | else{ |
---|
| 749 | if(PyString_Size(value)>0) |
---|
| 750 | res=createMap(lkey,lvalue); |
---|
| 751 | } |
---|
[392] | 752 | } |
---|
[67] | 753 | } |
---|
[1] | 754 | } |
---|
[505] | 755 | Py_DECREF(list); |
---|
[1] | 756 | return res; |
---|
| 757 | } |
---|
[368] | 758 | |
---|
[579] | 759 | /** |
---|
[738] | 760 | * Convert a Python dictionary to a map |
---|
| 761 | * |
---|
| 762 | * @param t the PyDictObject to convert |
---|
| 763 | * @return a new map containing the converted PyDictObject |
---|
| 764 | * @warning make sure to free resources returned by this function |
---|
| 765 | */ |
---|
| 766 | map* _mapFromPyDict(PyDictObject* t) { |
---|
| 767 | |
---|
[928] | 768 | PyObject* list = PyDict_Keys((PyObject*) t); // new ref |
---|
| 769 | int nb = PyList_Size(list); |
---|
[738] | 770 | |
---|
[928] | 771 | if (nb < 1) { |
---|
| 772 | Py_DECREF(list); |
---|
| 773 | return NULL; |
---|
| 774 | } |
---|
[738] | 775 | |
---|
[928] | 776 | map* ptr = (map*) malloc(MAP_SIZE); |
---|
| 777 | map* res = ptr; |
---|
[738] | 778 | |
---|
[928] | 779 | PyObject* key; |
---|
| 780 | PyObject* value; |
---|
| 781 | #if PY_MAJOR_VERSION >= 3 |
---|
| 782 | const |
---|
| 783 | #endif |
---|
| 784 | char *buffer = NULL; |
---|
| 785 | Py_ssize_t size; |
---|
| 786 | for(int i = 0; i < nb; i++) { |
---|
[738] | 787 | |
---|
[928] | 788 | key = PyList_GetItem(list, i); // borrowed ref |
---|
| 789 | value = PyDict_GetItem((PyObject*) t, key); // borrowed ref |
---|
[738] | 790 | |
---|
[928] | 791 | ptr->name = zStrdup(PyString_AsString(key)); |
---|
| 792 | map* msize = NULL; |
---|
[738] | 793 | |
---|
[928] | 794 | #if PY_MAJOR_VERSION >= 3 |
---|
| 795 | if (PyBytes_Check(value)) { |
---|
| 796 | // value is byte array |
---|
| 797 | size = PyBytes_Size(value); |
---|
| 798 | buffer = PyBytes_AsString(value); // pointer to internal buffer |
---|
| 799 | char sz[32]; |
---|
| 800 | sprintf(sz, "%d", (int) size); |
---|
| 801 | msize = createMap("size", sz); |
---|
| 802 | } |
---|
| 803 | else if (PyUnicode_Check(value) && PyUnicode_READY(value) == 0) { |
---|
| 804 | // value is string object |
---|
| 805 | buffer = PyUnicode_AsUTF8AndSize(value, &size); |
---|
| 806 | size++; |
---|
| 807 | } |
---|
| 808 | else { |
---|
| 809 | printf("Type not recognized\n"); |
---|
| 810 | // error handling |
---|
| 811 | // ... |
---|
| 812 | } |
---|
| 813 | #else |
---|
| 814 | PyString_AsStringAndSize(value, &buffer, &size); |
---|
| 815 | size++; |
---|
| 816 | // to do: handle byte arrays |
---|
| 817 | #endif |
---|
[738] | 818 | |
---|
[928] | 819 | ptr->value = (char*) malloc(size); // check for NULL pointer |
---|
| 820 | memmove(ptr->value, buffer, size); |
---|
[738] | 821 | |
---|
[928] | 822 | if (msize != NULL) { |
---|
| 823 | ptr->next = msize; |
---|
| 824 | ptr = ptr->next; |
---|
| 825 | } |
---|
[738] | 826 | |
---|
[928] | 827 | ptr->next = i < nb - 1 ? (map*) malloc(MAP_SIZE) : NULL; |
---|
| 828 | ptr = ptr->next; |
---|
| 829 | } |
---|
| 830 | Py_DECREF(list); |
---|
[738] | 831 | |
---|
[928] | 832 | return res; |
---|
[738] | 833 | } // mapFromPyDict |
---|
| 834 | |
---|
| 835 | /** |
---|
[580] | 836 | * Use the ZOO-Services messages translation function from the Python |
---|
[579] | 837 | * environment |
---|
| 838 | * |
---|
| 839 | * @param self the Python object on which we can run the method |
---|
| 840 | * @param args the Python arguments given from the Python environment |
---|
[581] | 841 | * @return a new Python string containing the translated value |
---|
[579] | 842 | * @see _ss |
---|
| 843 | */ |
---|
[368] | 844 | PyObject* |
---|
[376] | 845 | PythonTranslate(PyObject* self, PyObject* args) |
---|
| 846 | { |
---|
| 847 | char *str; |
---|
| 848 | if (!PyArg_ParseTuple(args, "s", &str)){ |
---|
| 849 | #ifdef DEBUG |
---|
| 850 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 851 | #endif |
---|
| 852 | return NULL; |
---|
| 853 | } |
---|
| 854 | return PyString_FromString(_ss(str)); |
---|
| 855 | } |
---|
| 856 | |
---|
[579] | 857 | /** |
---|
| 858 | * Update the ongoing status of a running service from the Python environment |
---|
| 859 | * |
---|
| 860 | * @param self the Python object on which we can run the method |
---|
| 861 | * @param args the Python arguments given from the Python environment |
---|
| 862 | * @return None to the Python environment |
---|
| 863 | * @see _updateStatus |
---|
| 864 | */ |
---|
[376] | 865 | PyObject* |
---|
[368] | 866 | PythonUpdateStatus(PyObject* self, PyObject* args) |
---|
| 867 | { |
---|
| 868 | maps* conf; |
---|
| 869 | PyObject* confdict; |
---|
| 870 | int istatus; |
---|
[624] | 871 | char* status=NULL; |
---|
[368] | 872 | if (!PyArg_ParseTuple(args, "O!i", &PyDict_Type, &confdict, &istatus)){ |
---|
| 873 | #ifdef DEBUG |
---|
| 874 | fprintf(stderr,"Incorrect arguments to update status function"); |
---|
| 875 | #endif |
---|
[624] | 876 | Py_RETURN_NONE; |
---|
[368] | 877 | } |
---|
| 878 | if (istatus < 0 || istatus > 100){ |
---|
| 879 | PyErr_SetString(ZooError, "Status must be a percentage."); |
---|
[624] | 880 | Py_RETURN_NONE; |
---|
[368] | 881 | }else{ |
---|
| 882 | char tmpStatus[4]; |
---|
| 883 | snprintf(tmpStatus, 4, "%i", istatus); |
---|
[453] | 884 | status = zStrdup(tmpStatus); |
---|
[368] | 885 | } |
---|
[624] | 886 | // create a local copy and update the lenv map |
---|
[368] | 887 | conf = mapsFromPyDict((PyDictObject*)confdict); |
---|
[624] | 888 | if(status!=NULL){ |
---|
| 889 | setMapInMaps(conf,"lenv","status",status); |
---|
| 890 | free(status); |
---|
[368] | 891 | } |
---|
[624] | 892 | else |
---|
| 893 | setMapInMaps(conf,"lenv","status","15"); |
---|
| 894 | _updateStatus(conf); |
---|
[368] | 895 | freeMaps(&conf); |
---|
| 896 | free(conf); |
---|
| 897 | Py_RETURN_NONE; |
---|
| 898 | } |
---|