From fd81759e2ed44cac3bc67243a39256f953969103 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 21 Jul 2023 14:31:30 +1200 Subject: [PATCH] python: Move PyList_AsStringList to common code so we can reuse BUG: https://bugzilla.samba.org/show_bug.cgi?id=15289 Signed-off-by: Andrew Bartlett Reviewed-by: Douglas Bagnall --- python/modules.c | 35 +++++++++++++++++++++++++++++++++++ python/modules.h | 7 +++++++ python/pyglue.c | 1 + source4/auth/pyauth.c | 34 ---------------------------------- source4/auth/wscript_build | 4 +++- 5 files changed, 46 insertions(+), 35 deletions(-) diff --git a/python/modules.c b/python/modules.c index d8b330b6b28..83dc91c08a1 100644 --- a/python/modules.c +++ b/python/modules.c @@ -71,3 +71,38 @@ error: Py_XDECREF(mod_sys); return false; } + +const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, + const char *paramname) +{ + const char **ret; + Py_ssize_t i; + if (!PyList_Check(list)) { + PyErr_Format(PyExc_TypeError, "%s is not a list", paramname); + return NULL; + } + ret = talloc_array(NULL, const char *, PyList_Size(list)+1); + if (ret == NULL) { + PyErr_NoMemory(); + return NULL; + } + + for (i = 0; i < PyList_Size(list); i++) { + const char *value; + Py_ssize_t size; + PyObject *item = PyList_GetItem(list, i); + if (!PyUnicode_Check(item)) { + PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); + return NULL; + } + value = PyUnicode_AsUTF8AndSize(item, &size); + if (value == NULL) { + talloc_free(ret); + return NULL; + } + ret[i] = talloc_strndup(ret, value, size); + } + ret[i] = NULL; + return ret; +} + diff --git a/python/modules.h b/python/modules.h index 75108d77907..331adde0230 100644 --- a/python/modules.h +++ b/python/modules.h @@ -20,7 +20,14 @@ #ifndef __SAMBA_PYTHON_MODULES_H__ #define __SAMBA_PYTHON_MODULES_H__ +#include + bool py_update_path(void); /* discard signature of 'func' in favour of 'target_sig' */ #define PY_DISCARD_FUNC_SIG(target_sig, func) (target_sig)(void(*)(void))func + +const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, + const char *paramname); + #endif /* __SAMBA_PYTHON_MODULES_H__ */ + diff --git a/python/pyglue.c b/python/pyglue.c index 90c3d6e2895..6b9814072e7 100644 --- a/python/pyglue.c +++ b/python/pyglue.c @@ -20,6 +20,7 @@ #include #include "python/py3compat.h" #include "includes.h" +#include "python/modules.h" #include "version.h" #include "param/pyparam.h" #include "lib/socket/netif.h" diff --git a/source4/auth/pyauth.c b/source4/auth/pyauth.c index ec6065d7d0a..ee36b5e5efa 100644 --- a/source4/auth/pyauth.c +++ b/source4/auth/pyauth.c @@ -350,40 +350,6 @@ static PyObject *py_session_info_set_unix(PyObject *module, } -static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, - const char *paramname) -{ - const char **ret; - Py_ssize_t i; - if (!PyList_Check(list)) { - PyErr_Format(PyExc_TypeError, "%s is not a list", paramname); - return NULL; - } - ret = talloc_array(NULL, const char *, PyList_Size(list)+1); - if (ret == NULL) { - PyErr_NoMemory(); - return NULL; - } - - for (i = 0; i < PyList_Size(list); i++) { - const char *value; - Py_ssize_t size; - PyObject *item = PyList_GetItem(list, i); - if (!PyUnicode_Check(item)) { - PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); - return NULL; - } - value = PyUnicode_AsUTF8AndSize(item, &size); - if (value == NULL) { - talloc_free(ret); - return NULL; - } - ret[i] = talloc_strndup(ret, value, size); - } - ret[i] = NULL; - return ret; -} - static PyObject *PyAuthContext_FromContext(struct auth4_context *auth_context) { return pytalloc_reference(&PyAuthContext, auth_context); diff --git a/source4/auth/wscript_build b/source4/auth/wscript_build index 9b94143ba7c..57bb9f751c6 100644 --- a/source4/auth/wscript_build +++ b/source4/auth/wscript_build @@ -85,10 +85,12 @@ pytalloc_util = bld.pyembed_libname('pytalloc-util') pyparam_util = bld.pyembed_libname('pyparam_util') pyldb_util = bld.pyembed_libname('pyldb-util') pycredentials = 'pycredentials' +libpython = bld.pyembed_libname('LIBPYTHON') + bld.SAMBA_PYTHON('pyauth', source='pyauth.c', public_deps='auth_system_session', - deps='samdb %s %s %s %s auth4' % (pytalloc_util, pyparam_util, pyldb_util, pycredentials), + deps=f'samdb {pytalloc_util} {pyparam_util} {pyldb_util} {pycredentials} {libpython} auth4', realname='samba/auth.so' )