SIGN-OFF s4:pyrpc: add pyrpc_string_array_{from,to}_PyList()
[metze/samba/wip.git] / source4 / librpc / rpc / pyrpc_util.c
index 29fd281f54daddac19925730c4777318a3a07bc2..1e7f5af113c9eac09aff2cca7c937c71c83a7c17 100644 (file)
@@ -339,6 +339,7 @@ bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpc
 
                PyDict_SetItemString(ifacetype->tp_dict, mds[i].name, 
                                     (PyObject *)ret);
+               Py_CLEAR(ret);
        }
 
        return true;
@@ -423,6 +424,113 @@ PyObject *PyString_FromStringOrNULL(const char *str)
        return PyStr_FromString(str);
 }
 
+const char *pyrpc_PyStr_AsString(TALLOC_CTX *mem_ctx, PyObject *value)
+{
+       const char *test_str = NULL;
+       const char *talloc_str = NULL;
+       PyObject *unicode = NULL;
+
+       if (PyUnicode_Check(value)) {
+               unicode = PyUnicode_AsEncodedString(value, "utf-8", "ignore");
+               if (unicode == NULL) {
+                       PyErr_NoMemory();
+                       return NULL;
+               }
+               test_str = PyBytes_AS_STRING(unicode);
+       } else if (PyBytes_Check(value)) {
+               test_str = PyBytes_AS_STRING(value);
+       } else {
+               PyErr_Format(PyExc_TypeError,
+                            "Expected string or unicode object, got %s",
+                            Py_TYPE(value)->tp_name);
+               return NULL;
+       }
+
+       talloc_str = talloc_strdup(mem_ctx, test_str);
+       if (unicode != NULL) {
+               Py_DECREF(unicode);
+       }
+       if (talloc_str == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       return talloc_str;
+}
+
+const char **pyrpc_string_array_from_PyList(TALLOC_CTX *mem_ctx, PyObject *list,
+                                           const char *debug_name)
+{
+       const char **ret = NULL;
+       Py_ssize_t i;
+
+       if (!PyList_Check(list)) {
+               PyErr_Format(PyExc_TypeError,
+                            "%s is not a list.",
+                            debug_name);
+               return NULL;
+       }
+
+       ret = talloc_zero_array(mem_ctx, const char *, PyList_Size(list)+1);
+       if (ret == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       for (i = 0; i < PyList_Size(list); i++) {
+               PyObject *item = PyList_GetItem(list, i);
+               ret[i] = pyrpc_PyStr_AsString(ret, item);
+               if (ret[i] == NULL) {
+                       talloc_free(ret);
+                       return NULL;
+               }
+       }
+
+       ret[i] = NULL;
+       return ret;
+}
+
+PyObject *pyrpc_string_array_to_PyList(const char **string_array,
+                                      const char *debug_name)
+{
+       PyObject *pylist = NULL;
+       Py_ssize_t i;
+       Py_ssize_t len;
+
+       if (string_array == NULL) {
+               PyErr_Format(PyExc_TypeError,
+                            "%s should be a string_array instead of NULL.",
+                            debug_name);
+               return NULL;
+       }
+
+       len = str_list_length(string_array);
+
+       pylist = PyList_New(len);
+       if (pylist == NULL) {
+               return NULL;
+       }
+
+       for (i = 0; i < len; i++) {
+               PyObject *item = NULL;
+               int ret;
+
+               item = PyStr_FromString(string_array[i]);
+               if (item == NULL) {
+                       Py_DECREF(pylist);
+                       return NULL;
+               }
+
+               ret = PyList_SetItem(pylist, i, item);
+               if (ret != 0) {
+                       Py_DECREF(pylist);
+                       return NULL;
+               }
+       }
+
+       return pylist;
+}
+
 PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
                             const void *in, const char *typename)
 {