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

Douglas Bagnall douglas.bagnall at catalyst.net.nz
Fri Feb 9 19:11:04 UTC 2018


On 10/02/18 07:01, Andrew Bartlett via samba-technical wrote:
> Yes, I was hoping you could clarify here (and probably in the commit
> message) why there is no common way to do this:

Warning: your continued attention to this is attracting bikeshedders.
 
> -            pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')
> +
> +            if PY3:
> +                pw = str('"' + password + '"').encode('utf-16-le')
> +            else:
> +                pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')
> 
> The 'if PY3:' stuff just bothers me because we will live with it
> forever, and if we must do it I would prefer it in a helper function
> somewhere. 

I don't think this situation is very common or something we want to keep.

By my unpacking of the snowball:

-            pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')

there are 3 cases before the .encode('utf-16-le'):

1. password is unicode (somehow): it goes through a .encode().decode() cycle to unicode.
2. password is ascii str: it goes through .encode() NOOP, .decode() to unicode.
3. password is non-ascii (perhaps utf-8) str: encode ERROR

I think something like this is wanted:

+            if not PY3 and isinstance(password, str):
+                password = unicode(password, 'utf-8')
+            pw = (u"%s" % password).encode('utf-16-le')

cheers,
Douglas



More information about the samba-technical mailing list