mirror of
https://github.com/samba-team/samba.git
synced 2025-12-14 20:23:54 +03:00
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 <abartlet@samba.org> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user