Browse Source

ZOOKEEPER-600. TODO pondering about allocation behavior in zkpython may be removed (gustavo via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@892001 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 15 years ago
parent
commit
ca72b97623
2 changed files with 35 additions and 19 deletions
  1. 3 0
      CHANGES.txt
  2. 32 19
      src/contrib/zkpython/src/c/zookeeper.c

+ 3 - 0
CHANGES.txt

@@ -184,6 +184,9 @@ BUGFIXES:
 
   ZOOKEEPER-623. ClientBase in bookkeeper.util requires junit (fpj via breed)
 
+  ZOOKEEPER-600. TODO pondering about allocation behavior in zkpython may be
+  removed (gustavo via mahadev)
+
 IMPROVEMENTS:
   ZOOKEEPER-473. cleanup junit tests to eliminate false positives due to
   "socket reuse" and failure to close client (phunt via mahadev)

+ 32 - 19
src/contrib/zkpython/src/c/zookeeper.c

@@ -224,15 +224,29 @@ PyObject *build_stat( const struct Stat *stat )
 
 PyObject *build_string_vector(const struct String_vector *sv)
 {
-	if (!sv) {
-		return PyList_New(0);
-	}
-  PyObject *ret = PyList_New(sv->count);
-  int i;
-  for (i=0;i<sv->count;++i) 
+  PyObject *ret;
+  if (!sv)
     {
-      PyObject *s = PyString_FromString(sv->data[i]);
-      PyList_SetItem(ret, i, s);
+      ret = PyList_New(0);
+    }
+  else
+    {
+      ret = PyList_New(sv->count);
+      if (ret)
+        {
+          int i;
+          for (i=0;i<sv->count;++i) 
+            {
+              PyObject *s = PyString_FromString(sv->data[i]);
+              if (!s)
+                {
+                  Py_DECREF(ret);
+                  ret = NULL;
+                  break;
+                }
+              PyList_SetItem(ret, i, s);
+            }
+        }
     }
   return ret;
 }
@@ -407,8 +421,14 @@ void strings_completion_dispatch(int rc, const struct String_vector *strings, co
     return;
   PyObject *callback = pyw->callback;
   gstate = PyGILState_Ensure();
-  PyObject *arglist = Py_BuildValue("(i,i,O)", pyw->zhandle,rc, build_string_vector(strings));
-  if (PyObject_CallObject((PyObject*)callback, arglist) == NULL)
+  PyObject *pystrings = build_string_vector(strings);
+  if (pystrings)
+    {
+      PyObject *arglist = Py_BuildValue("(i,i,O)", pyw->zhandle, rc, pystrings);
+      if (arglist == NULL || PyObject_CallObject((PyObject*)callback, arglist) == NULL)
+        PyErr_Print();
+    }
+  else
     PyErr_Print();
   free_pywatcher(pyw);
   PyGILState_Release(gstate);
@@ -774,8 +794,6 @@ static PyObject *pyzoo_exists(PyObject *self, PyObject *args)
 
 static PyObject *pyzoo_get_children(PyObject *self, PyObject *args)
 {
-  // TO DO: Does Python copy the string or the reference? If it's the former
-  // we should free the String_vector
   int zkhid;
   char *path;
   PyObject *watcherfn = Py_None;
@@ -796,13 +814,8 @@ static PyObject *pyzoo_get_children(PyObject *self, PyObject *args)
       return NULL;
     }
 
-  PyObject *ret = PyList_New(strings.count);
-  int i;
-  for (i=0;i<strings.count;++i) 
-    {
-      PyObject *s = PyString_FromString(strings.data[i]);
-      PyList_SetItem(ret, i, s);
-    }
+  PyObject *ret = build_string_vector(&strings);
+  deallocate_String_vector(&strings);
   return ret;
 }