[PATCH] Port of dsdb, dsdb_dns and samdb Python modules

Douglas Bagnall 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[0] == 3
>  
>  if PY3:
>      integer_types = int,
> +    text_type = str
>  else:
>      integer_types = (int, long)
> +    text_type = unicode

Just a couple of days ago I found myself writing

   if PY3:
       json_str = str
   else:
       json_str = unicode

and wondering why samba.compat had no such thing.

(in http://git.catalyst.net.nz/gw?p=samba.git;a=blob;f=source4/torture/drs/python/samba_tool_drs_showrepl.py;h=90ee73824ce063ab257ed316b6787bbd611293fc;hb=89396842f8d18aef9081817eb6ebf668ec8be226#l25)

and this:

>              user_dn = res[0].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
>>> s.encode('utf-8')
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.

Douglas



More information about the samba-technical mailing list