[PATCH 1 of 3] py3: make a bytes version of getopt.getopt()

Yuya Nishihara yuya at tcha.org
Sun Dec 11 08:35:26 EST 2016


On Sat, 10 Dec 2016 05:10:25 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit at gmail.com>
> # Date 1480986396 -19800
> #      Tue Dec 06 06:36:36 2016 +0530
> # Node ID 4b57889d58056f95c58f2379836437a65950424c
> # Parent  a2b053b8d31aa01b1dcae2d3001b060ff59e8a68
> py3: make a bytes version of getopt.getopt()

Queued this, thanks.

> --- a/mercurial/pycompat.py	Tue Dec 06 11:44:49 2016 +0000
> +++ b/mercurial/pycompat.py	Tue Dec 06 06:36:36 2016 +0530
> @@ -10,6 +10,7 @@
>  
>  from __future__ import absolute_import
>  
> +import getopt
>  import os
>  import sys
>  
> @@ -87,6 +88,19 @@
>      setattr = _wrapattrfunc(builtins.setattr)
>      xrange = builtins.range
>  
> +    # getopt.getopt() on Python 3 deals with unicodes internally so we cannot
> +    # pass bytes there. Passing unicodes will result in unicodes as return
> +    # values which we need to convert again to bytes.
> +    def getoptb(args, shortlist, namelist):
> +        args = [a.decode('latin-1') for a in args]
> +        shortlist = shortlist.decode('latin-1')
> +        namelist = [a.decode('latin-1') for a in namelist]
> +        opts, args = getopt.getopt(args, shortlist, namelist)
> +        opts = [(a[0].enocde('latin-1'), a[1].enocde('latin-1'))
> +                            for a in opts]

Fixed typo in flight: s/enocde/encode/.


More information about the Mercurial-devel mailing list