From 81377d92c8c9b8900aa09aed2c0ee43b75f8518b Mon Sep 17 00:00:00 2001 From: Arvid Requate Date: Fri, 10 Mar 2017 17:41:46 +0100 Subject: [PATCH] SIGN-OFF s4:pyrpc: add pyrpc_string_array_{from,to}_PyList() Pair-Programmed-With: Stefan Metzmacher TODO: Signed-off-by: Arvid Requate Signed-off-by: Stefan Metzmacher --- source4/librpc/rpc/pyrpc_util.c | 73 +++++++++++++++++++++++++++++++++ source4/librpc/rpc/pyrpc_util.h | 5 +++ 2 files changed, 78 insertions(+) diff --git a/source4/librpc/rpc/pyrpc_util.c b/source4/librpc/rpc/pyrpc_util.c index aa996c032b39..1e7f5af113c9 100644 --- a/source4/librpc/rpc/pyrpc_util.c +++ b/source4/librpc/rpc/pyrpc_util.c @@ -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) { diff --git a/source4/librpc/rpc/pyrpc_util.h b/source4/librpc/rpc/pyrpc_util.h index 6e70bf01b3d4..afc1ac8d0fa6 100644 --- a/source4/librpc/rpc/pyrpc_util.h +++ b/source4/librpc/rpc/pyrpc_util.h @@ -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, -- 2.34.1