[PATCH] Port of dsdb, dsdb_dns and samdb Python modules
douglas.bagnall at catalyst.net.nz
Sat Feb 10 10:38:18 UTC 2018
On 10/02/18 09:02, Lumir Balhar via samba-technical wrote:
> I was working on that in parallel with you and I think I have a final solution without PY3 condition. Moreover, it is compatible with Python ≥ 2.6.
> New patchset attached. What do you think about it?
I definitely approve of this bit:
> --- a/python/samba/compat.py
> +++ b/python/samba/compat.py
> @@ -23,5 +23,7 @@ PY3 = sys.version_info == 3
> if PY3:
> integer_types = int,
> + text_type = str
> integer_types = (int, long)
> + text_type = unicode
Just a couple of days ago I found myself writing
json_str = str
json_str = unicode
and wondering why samba.compat had no such thing.
> user_dn = res.dn
> - pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')
> + pw = text_type(b'"' + password.encode('utf-8') + b'"', 'utf-8').encode('utf-16-le')
> setpw = """
is certainly better than before.
BUT, and this is perhaps getting off topic, I can't see how this ever
worked when password is a py2 str containing actual non-ascii
characters. I mean:
>>> s = 'ā' # utf-8 for LATIN SMALL LETTER A WITH MACRON
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
which is the error you get with samba-tool user setpassword.
Now, with Python 3, password would be a unicode string from the start,
and unless samba-tool does some special byte-ifying to maintain
bug-for-bug compatibility, samba-tool user setpassword --password
<utf8> will work.
It's a benign example of how hard it is to maintain identical
semantics across the Python versions.
More information about the samba-technical