SIGN-OFF s4:pyrpc: add pyrpc_string_array_{from,to}_PyList()
authorArvid Requate <requate@univention.de>
Fri, 10 Mar 2017 16:41:46 +0000 (17:41 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 18 Feb 2019 11:47:34 +0000 (12:47 +0100)
Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>

TODO: Signed-off-by: Arvid Requate <requate@univention.de>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
source4/librpc/rpc/pyrpc_util.c
source4/librpc/rpc/pyrpc_util.h

index aa996c032b397e8cdb75408b8d599d2c126de697..1e7f5af113c9eac09aff2cca7c937c71c83a7c17 100644 (file)
@@ -458,6 +458,79 @@ const char *pyrpc_PyStr_AsString(TALLOC_CTX *mem_ctx, PyObject *value)
        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)
 {
index 6e70bf01b3d4fa2c5a70af666e0217ca458bc9a3..afc1ac8d0fa6f1ef3bfdf80f91e31ae314f97afd 100644 (file)
@@ -60,6 +60,11 @@ PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
 PyObject *PyString_FromStringOrNULL(const char *str);
 const char *pyrpc_PyStr_AsString(TALLOC_CTX *mem_ctx, PyObject *value);
 
+const char **pyrpc_string_array_from_PyList(TALLOC_CTX *mem_ctx, PyObject *list,
+                                           const char *debug_name);
+PyObject *pyrpc_string_array_to_PyList(const char **string_array,
+                                      const char *debug_name);
+
 PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
                             const void *in, const char *typename);
 void *pyrpc_export_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,