Index: /trunk/zoo-kernel/service_internal_python.c
===================================================================
--- /trunk/zoo-kernel/service_internal_python.c	(revision 294)
+++ /trunk/zoo-kernel/service_internal_python.c	(revision 295)
@@ -74,5 +74,11 @@
   free(pythonpath);
 
+  PyThreadState *mainstate;
+  PyEval_InitThreads();
   Py_Initialize();
+  mainstate = PyThreadState_Swap(NULL);
+  PyEval_ReleaseLock();
+  PyGILState_STATE gstate;
+  gstate = PyGILState_Ensure();
   PyObject *pName, *pModule, *pFunc;
   tmp=getMap(s->content,"serviceProvider");
@@ -119,11 +125,4 @@
 	dumpMaps(outputs);
 #endif
-	Py_DECREF(arg1);
-	Py_DECREF(arg2);
-	Py_DECREF(arg3);
-      	Py_DECREF(pArgs);
-	Py_DECREF(pValue);
-	Py_XDECREF(pFunc);
-	Py_DECREF(pModule);
       }else{	  
 	PyObject *ptype,*pvalue, *ptraceback;
@@ -159,27 +158,13 @@
 	addToMap(err,"code","NoApplicableCode");
 	printExceptionReportResponse(m,err);
-	Py_DECREF(arg1);
-	Py_DECREF(arg2);
-	Py_DECREF(arg3);
-	Py_XDECREF(pFunc);
-      	Py_DECREF(pArgs);
-	Py_DECREF(pModule);
-	Py_DECREF(ptraceback);
-	Py_DECREF(ptype);
-	Py_DECREF(pValue);
-#if not(defined(macintosh)) && not(defined(__MACH__) && defined(__APPLE__))
-	Py_Finalize();
-#endif
-	exit(-1);
+	res=-1;
       }
     }
     else{
       char tmpS[1024];
-      sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value);
+      sprintf(tmpS, "Cannot find the %s function in the %s file.\n", s->name, tmp->value);
       map* tmps=createMap("text",tmpS);
       printExceptionReportResponse(m,tmps);
-      Py_XDECREF(pFunc);
-      Py_DECREF(pModule);
-      exit(-1);
+      res=-1;
     }
   } else{
@@ -190,9 +175,12 @@
     if (PyErr_Occurred())
       PyErr_Print();
-    exit(-1);
+    PyErr_Clear();
+    res=-1;
+    //exit(-1);
   } 
-#if not(defined(macintosh)) && not(defined(__MACH__) && defined(__APPLE__))
+  PyGILState_Release(gstate);
+  PyEval_AcquireLock();
+  PyThreadState_Swap(mainstate);
   Py_Finalize();
-#endif
   return res;
 }
@@ -202,10 +190,11 @@
   maps* tmp=t;
   while(tmp!=NULL){
-    PyObject* subc=(PyObject*)PyDict_FromMap(tmp->content);
-    if(PyDict_SetItem(res,PyString_FromString(tmp->name),subc)<0){
-      fprintf(stderr,"Unable to parse params...");
-      exit(1);
-    }
-    Py_DECREF(subc);
+    PyObject* value=(PyObject*)PyDict_FromMap(tmp->content);
+    PyObject* name=PyString_FromString(tmp->name);
+    if(PyDict_SetItem(res,name,value)<0){
+      fprintf(stderr,"Unable to set map value ...");
+      return NULL;
+    }
+    Py_DECREF(name);
     tmp=tmp->next;
   }  
@@ -223,18 +212,14 @@
 	PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value));
 	if(PyDict_SetItem(res,name,value)<0){
-	  fprintf(stderr,"Unable to parse params...");
-	  Py_DECREF(value);
-	  exit(1);
+	  fprintf(stderr,"Unable to set key value pair...");
+	  return NULL;
 	}
-	Py_DECREF(value);
       }
       else{
 	PyObject* value=PyString_FromString(tmp->value);
 	if(PyDict_SetItem(res,name,value)<0){
-	  fprintf(stderr,"Unable to parse params...");
-	  Py_DECREF(value);
-	  exit(1);
+	  fprintf(stderr,"Unable to set key value pair...");
+	  return NULL;
 	}
-	Py_DECREF(value);
       }
     }
@@ -242,9 +227,7 @@
       PyObject* value=PyString_FromString(tmp->value);
       if(PyDict_SetItem(res,name,value)<0){
-	fprintf(stderr,"Unable to parse params...");
-	Py_DECREF(value);
-	exit(1);
-      }
-      Py_DECREF(value);
+	fprintf(stderr,"Unable to set key value pair...");
+	return NULL;
+      }
     }
     Py_DECREF(name);
@@ -272,8 +255,8 @@
     cursor=(maps*)malloc(MAPS_SIZE);
     cursor->name=PyString_AsString(key);
-#ifdef DEBUG
-    dumpMap(mapFromPyDict((PyDictObject*)value));
-#endif
     cursor->content=mapFromPyDict((PyDictObject*)value);
+#ifdef DEBUG
+    dumpMap(cursor->content);
+#endif
     cursor->next=NULL;
     if(res==NULL)
@@ -284,6 +267,4 @@
     free(cursor->content);
     free(cursor);
-    Py_DECREF(value);
-    Py_DECREF(key);
 #ifdef DEBUG
     dumpMaps(res);
@@ -291,5 +272,4 @@
 #endif
   }
-  Py_DECREF(list);
   return res;
 }
@@ -330,7 +310,5 @@
 	res=createMap(PyString_AsString(key),PyString_AsString(value));
     }
-    Py_DECREF(key);
-  }
-  Py_DECREF(list);
+  }
   return res;
 }
