From 4ab0e369d28e3febcc14de2e0801b0e577fc823d Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Fri, 3 May 2013 14:05:23 +0200 Subject: [PATCH 5/5] Add support for tuples and unicode pysss_nss_idmap.so This patch allows tuples as well as lists as input and adds support for Unicode objects as input and always returns the results as Unicode objects. --- src/python/pysss_nss_idmap.c | 64 ++++++++++++++++++++++++++++++----------- 1 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/python/pysss_nss_idmap.c b/src/python/pysss_nss_idmap.c index dd1fb4c..edd0c00 100644 --- a/src/python/pysss_nss_idmap.c +++ b/src/python/pysss_nss_idmap.c @@ -64,6 +64,19 @@ static int add_dict(PyObject *py_result, PyObject *key, PyObject *res_type, return ret; } +static char *py_string_or_unicode_as_string(PyObject *inp) +{ + PyObject *py_str = NULL; + + if (PyUnicode_Check(inp)) { + py_str = PyUnicode_AsUTF8String(inp); + } else { + py_str = inp; + } + + return PyString_AS_STRING(py_str); +} + static int do_getsidbyname(PyObject *py_result, PyObject *py_name) { int ret; @@ -71,7 +84,7 @@ static int do_getsidbyname(PyObject *py_result, PyObject *py_name) char *sid = NULL; enum sss_id_type id_type; - name = PyString_AS_STRING(py_name); + name = py_string_or_unicode_as_string(py_name); if (name == NULL) { return EINVAL; } @@ -79,7 +92,7 @@ static int do_getsidbyname(PyObject *py_result, PyObject *py_name) ret = sss_nss_getsidbyname(name, &sid, &id_type); if (ret == 0) { ret = add_dict(py_result, py_name, PyString_FromString(SSS_SID_KEY), - PyString_FromString(sid), PyInt_FromLong(id_type)); + PyUnicode_FromString(sid), PyInt_FromLong(id_type)); } free(sid); @@ -93,7 +106,7 @@ static int do_getnamebysid(PyObject *py_result, PyObject *py_sid) char *name = NULL; enum sss_id_type id_type; - sid = PyString_AS_STRING(py_sid); + sid = py_string_or_unicode_as_string(py_sid); if (sid == NULL) { return EINVAL; } @@ -101,7 +114,7 @@ static int do_getnamebysid(PyObject *py_result, PyObject *py_sid) ret = sss_nss_getnamebysid(sid, &name, &id_type); if (ret == 0) { ret = add_dict(py_result, py_sid, PyString_FromString(SSS_NAME_KEY), - PyString_FromString(name), PyInt_FromLong(id_type)); + PyUnicode_FromString(name), PyInt_FromLong(id_type)); } free(name); @@ -121,8 +134,8 @@ static int do_getsidbyid(PyObject *py_result, PyObject *py_id) id = PyInt_AS_LONG(py_id); } else if (PyLong_Check(py_id)) { id = PyLong_AsLong(py_id); - } else if (PyString_Check(py_id)) { - id_str = PyString_AS_STRING(py_id); + } else if (PyString_Check(py_id) || PyUnicode_Check(py_id)) { + id_str = py_string_or_unicode_as_string(py_id); errno = 0; id = strtol(id_str, &endptr, 10); if (errno != 0 || *endptr != '\0') { @@ -139,7 +152,7 @@ static int do_getsidbyid(PyObject *py_result, PyObject *py_id) ret = sss_nss_getsidbyid((uint32_t) id, &sid, &id_type); if (ret == 0) { ret = add_dict(py_result, py_id, PyString_FromString(SSS_SID_KEY), - PyString_FromString(sid), PyInt_FromLong(id_type)); + PyUnicode_FromString(sid), PyInt_FromLong(id_type)); } free(sid); @@ -153,7 +166,7 @@ static int do_getidbysid(PyObject *py_result, PyObject *py_sid) enum sss_id_type id_type; int ret; - sid = PyString_AS_STRING(py_sid); + sid = py_string_or_unicode_as_string(py_sid); if (sid == NULL) { return EINVAL; } @@ -190,22 +203,29 @@ static int do_lookup(enum lookup_type type, PyObject *py_result, return ENOSYS; } +typedef Py_ssize_t (*py_lt_size_fn_t)(PyObject *); +typedef PyObject *(*py_lt_get_item_fn_t)(PyObject *, Py_ssize_t); static PyObject *check_args(enum lookup_type type, PyObject *args) { PyObject *obj, *py_value; int ret; Py_ssize_t len, i; PyObject *py_result; + py_lt_size_fn_t lt_size = NULL; + py_lt_get_item_fn_t lt_get_item = NULL; + bool multival = false; if (!PyArg_ParseTuple(args, sss_py_const_p(char, "O"), &obj)) { PyErr_Format(PyExc_ValueError, "Unable to retrieve argument\n"); return NULL; } - if (!(PyList_Check(obj) || PyString_Check(obj) || + if (!(PyList_Check(obj) || PyTuple_Check(obj) || + PyString_Check(obj) || PyUnicode_Check(obj) || (type == SIDBYID && (PyInt_Check(obj) || PyLong_Check(obj))))) { PyErr_Format(PyExc_ValueError, - "Only string, long or list of them are accepted\n"); + "Only string, long or list or tuples of them " \ + "are accepted\n"); return NULL; } @@ -218,11 +238,21 @@ static PyObject *check_args(enum lookup_type type, PyObject *args) } if (PyList_Check(obj)) { - len = PyList_Size(obj); + lt_size = PyList_Size; + lt_get_item = PyList_GetItem; + multival = true; + } else if (PyTuple_Check(obj)) { + lt_size = PyTuple_Size; + lt_get_item = PyTuple_GetItem; + multival = true; + } + + if (multival) { + len = lt_size(obj); for(i=0; i < len; i++) { - py_value = PyList_GetItem(obj, i); + py_value = lt_get_item(obj, i); if ((py_value != NULL) && - (PyString_Check(py_value) || + (PyString_Check(py_value) || PyUnicode_Check(py_value) || (type == SIDBYID && (PyInt_Check(py_value) || PyLong_Check(py_value))))) { ret = do_lookup(type, py_result, py_value); @@ -256,7 +286,7 @@ static PyObject *check_args(enum lookup_type type, PyObject *args) } PyDoc_STRVAR(getsidbyname_doc, -"getsidbyname(name or list of names) -> dict(name => dict(results))\n\ +"getsidbyname(name or list/tuple of names) -> dict(name => dict(results))\n\ \n\ Returns a dictionary with a dictonary of results for each given name.\n\ The result dictonary contain the SID and the type of the object which can be\n\ @@ -275,7 +305,7 @@ static PyObject * py_getsidbyname(PyObject *module, PyObject *args) } PyDoc_STRVAR(getsidbyid_doc, -"getsidbyid(id or list of id) -> dict(id => dict(results))\n\ +"getsidbyid(id or list/tuple of id) -> dict(id => dict(results))\n\ \n\ Returns a dictionary with a dictonary of results for each given POSIX ID.\n\ The result dictonary contain the SID and the type of the object which can be\n\ @@ -288,7 +318,7 @@ static PyObject * py_getsidbyid(PyObject *module, PyObject *args) } PyDoc_STRVAR(getnamebysid_doc, -"getnamebysid(sid or list of sid) -> dict(sid => dict(results))\n\ +"getnamebysid(sid or list/tuple of sid) -> dict(sid => dict(results))\n\ \n\ Returns a dictionary with a dictonary of results for each given SID.\n\ The result dictonary contain the name and the type of the object which can be\n\ @@ -304,7 +334,7 @@ PyDoc_STRVAR(getidbysid_doc, "getidbysid(sid) -> POSIX ID\n\ \n\ Returns the POSIX ID of the object with the given SID." -"getidbysid(sid or list of sid) -> dict(sid => dict(results))\n\ +"getidbysid(sid or list/tuple of sid) -> dict(sid => dict(results))\n\ \n\ Returns a dictionary with a dictonary of results for each given SID.\n\ The result dictonary contain the POSIX ID and the type of the object which\n\ -- 1.7.7.6