pyldb: p3k readiness: allow single unicode string in msg element
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Tue, 10 Jan 2017 23:18:15 +0000 (12:18 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 10 Mar 2017 06:31:09 +0000 (07:31 +0100)
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/pyldb.c

index bea837f210e0bf04ca6d57c4dc1c1794bfde472d..b65e25525ad0aa6158b90ac1fa31b57d43715d3b 100644 (file)
@@ -3047,7 +3047,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb
 
        if (py_elements != NULL) {
                Py_ssize_t i;
-               if (PyBytes_Check(py_elements)) {
+               if (PyBytes_Check(py_elements) || PyStr_Check(py_elements)) {
                        char *_msg = NULL;
                        el->num_values = 1;
                        el->values = talloc_array(el, struct ldb_val, 1);
@@ -3056,12 +3056,17 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb
                                PyErr_NoMemory();
                                return NULL;
                        }
-                       result = PyBytes_AsStringAndSize(py_elements, &_msg, &size);
+                       if (PyBytes_Check(py_elements)) {
+                               result = PyBytes_AsStringAndSize(py_elements, &_msg, &size);
+                               msg = _msg;
+                       } else {
+                               msg = PyStr_AsUTF8AndSize(py_elements, &size);
+                               result = (msg == NULL) ? -1 : 0;
+                       }
                        if (result != 0) {
                                talloc_free(mem_ctx);
                                return NULL;
                        }
-                       msg = _msg;
                        el->values[0].data = talloc_memdup(el->values, 
                                (const uint8_t *)msg, size + 1);
                        el->values[0].length = size;