LDB python3 strings

Andrew Bartlett abartlet at samba.org
Tue May 1 21:28:16 UTC 2018

G'Day Noel,

Thanks so much for continuing the python3 work.  This is really
important and I'm so glad to be able to pass on the baton here.

One thing that came up in a discussion in the Catalyst office regarding
this work is worth raising more broadly.

It is exceedingly common in Samba's use of ldb to use:

username = str(res[0]["samAccountName"])

This works because of 

static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
        struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);

        if (el->num_values == 1)
                return PyStr_FromStringAndSize((char *)el->values[0].data, el->values[0].length);

However equally common is:

username = str(res[0]["samAccountName"][0])

This works because in python2 it just returns the string.  However in
python3 I'm told it will return "b'username'" (no so helpful).

As all strings in LDAP are UTF8 (I'm willing to assert that for sanity)
I think we need the MessageElement to contain not byte buffers, but a
subclass of byte buffers that have a string function that converts
automatically produces a utf8 string for str().

Do you think you could have a look at that?  Otherwise, converting
samba-tool and our other ldb-calling code is going to get very tricky.


Andrew Bartlett

Andrew Bartlett
Authentication Developer, Samba Team         https://samba.org
Samba Development and Support, Catalyst IT   

More information about the samba-technical mailing list