Patchwork [1,of,7] py3: handle opts uniformly in commands.py

login
register
mail settings
Submitter Pulkit Goyal
Date May 4, 2017, 3:16 a.m.
Message ID <f15cb31ed5bd3d059401.1493867803@pulkit-goyal>
Download mbox | patch
Permalink /patch/20414/
State Accepted
Headers show

Comments

Pulkit Goyal - May 4, 2017, 3:16 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1493805306 -19800
#      Wed May 03 15:25:06 2017 +0530
# Node ID f15cb31ed5bd3d059401731b06930e0c344e0b4d
# Parent  c3dcec6cbc1bc3428f8d7b757a3621204a4e1742
py3: handle opts uniformly in commands.py

Since keyword arguments can't be bytes on Python 3, we converted then to
unicodes before passing into different command functions. We need to adopt a
certain pattern to convert opts back to bytes. Following are some of the
functions which are called from inside these command functions and should
always be feeded bytes to follow the right behaviour.

ui.fomattter()
scmutil.match()
patch.diffallopts()
hg.peer()
cmdutil.{show_changeset|copy|graphrevs|checkunsupportedflag}
server.{createservice|runservice}

There are few commands which are left out where opts is again passed to a
function as keyword arguments or converting opts back to bytes is kind of
not necessary. Those are cat, revert, help, unbundle and rollback. Following
patches will deal with them. This patch apart from these five commands,
convert opts back to bytes for rest of the commands.

This fixes a lot of things which are hidden like --git works now. Similarly
more flags of commands which run on Python 3 currently get fixed.
Yuya Nishihara - May 4, 2017, 4:35 a.m.
On Thu, 04 May 2017 08:46:43 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1493805306 -19800
> #      Wed May 03 15:25:06 2017 +0530
> # Node ID f15cb31ed5bd3d059401731b06930e0c344e0b4d
> # Parent  c3dcec6cbc1bc3428f8d7b757a3621204a4e1742
> py3: handle opts uniformly in commands.py

Queued the series, thanks.

I found a couple of nits. Can you fix them by a follow-up patch?

> --- a/mercurial/commands.py	Fri Apr 21 01:13:18 2017 +0530
> +++ b/mercurial/commands.py	Wed May 03 15:25:06 2017 +0530
> @@ -255,6 +255,7 @@
>      Returns 0 if all files are successfully added.
>      """
>  
> +    opts = pycompat.byteskwargs(opts)
>      m = scmutil.match(repo[None], pats, opts)
>      rejected = cmdutil.add(ui, repo, m, "", False, **opts)

Needs to convert opts back to a str-key dict.

>  def _dograft(ui, repo, *revs, **opts):
> +    opts = pycompat.byteskwargs(opts)
>      if revs and opts.get('rev'):
>          ui.warn(_('warning: inconsistent use of --rev might give unexpected '
>                    'revision ordering!\n'))

cmdutil.getcommiteditor(.., **opts)

> @@ -5210,6 +5253,7 @@
>      Returns 0 on success.
>      """
>  
> +    opts = pycompat.byteskwargs(opts)
>      ui.pager('tags')
>      fm = ui.formatter('tags', opts)
>      hexfunc = fm.hexfunc

cmdutil.getcommiteditor(.., **opts)

Patch

diff -r c3dcec6cbc1b -r f15cb31ed5bd mercurial/commands.py
--- a/mercurial/commands.py	Fri Apr 21 01:13:18 2017 +0530
+++ b/mercurial/commands.py	Wed May 03 15:25:06 2017 +0530
@@ -255,6 +255,7 @@ 
     Returns 0 if all files are successfully added.
     """
 
+    opts = pycompat.byteskwargs(opts)
     m = scmutil.match(repo[None], pats, opts)
     rejected = cmdutil.add(ui, repo, m, "", False, **opts)
     return rejected and 1 or 0
@@ -325,6 +326,7 @@ 
 
     Returns 0 if all files are successfully added.
     """
+    opts = pycompat.byteskwargs(opts)
     try:
         sim = float(opts.get('similarity') or 100)
     except ValueError:
@@ -368,6 +370,8 @@ 
 
     Returns 0 on success.
     """
+
+    opts = pycompat.byteskwargs(opts)
     if not pats:
         raise error.Abort(_('at least one filename or pattern is required'))
 
@@ -532,6 +536,7 @@ 
     Returns 0 on success.
     '''
 
+    opts = pycompat.byteskwargs(opts)
     ctx = scmutil.revsingle(repo, opts.get('rev'))
     if not ctx:
         raise error.Abort(_('no working directory: please specify a revision'))
@@ -627,6 +632,8 @@ 
         release(lock, wlock)
 
 def _dobackout(ui, repo, node=None, rev=None, **opts):
+
+    opts = pycompat.byteskwargs(opts)
     if opts.get('commit') and opts.get('no_commit'):
         raise error.Abort(_("cannot use --commit with --no-commit"))
     if opts.get('merge') and opts.get('no_commit'):
@@ -1002,6 +1009,8 @@ 
 
           hg book -f @
     '''
+
+    opts = pycompat.byteskwargs(opts)
     force = opts.get('force')
     rev = opts.get('rev')
     delete = opts.get('delete')
@@ -1178,6 +1187,7 @@ 
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     if label:
         label = label.strip()
 
@@ -1226,6 +1236,7 @@ 
     Returns 0.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ui.pager('branches')
     fm = ui.formatter('branches', opts)
     hexfunc = fm.hexfunc
@@ -1309,6 +1320,7 @@ 
 
     Returns 0 on success, 1 if no changes found.
     """
+    opts = pycompat.byteskwargs(opts)
     revs = None
     if 'rev' in opts:
         revstrings = opts['rev']
@@ -1549,6 +1561,7 @@ 
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     if opts.get('noupdate') and opts.get('updaterev'):
         raise error.Abort(_("cannot specify both --noupdate and --updaterev"))
 
@@ -1639,16 +1652,16 @@ 
         release(lock, wlock)
 
 def _docommit(ui, repo, *pats, **opts):
-    opts = pycompat.byteskwargs(opts)
-    if opts.get('interactive'):
-        opts.pop('interactive')
+    if opts.get(r'interactive'):
+        opts.pop(r'interactive')
         ret = cmdutil.dorecord(ui, repo, commit, None, False,
                                cmdutil.recordfilter, *pats,
-                               **pycompat.strkwargs(opts))
+                               **opts)
         # ret can be 0 (no changes to record) or the value returned by
         # commit(), 1 if nothing changed or None on success.
         return 1 if ret == 0 else ret
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('subrepos'):
         if opts.get('amend'):
             raise error.Abort(_('cannot amend with --subrepos'))
@@ -1769,6 +1782,7 @@ 
 
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('edit') or opts.get('local') or opts.get('global'):
         if opts.get('local') and opts.get('global'):
             raise error.Abort(_("can't use --local and --global together"))
@@ -1871,6 +1885,7 @@ 
 
     Returns 0 on success, 1 if errors are encountered.
     """
+    opts = pycompat.byteskwargs(opts)
     with repo.wlock(False):
         return cmdutil.copy(ui, repo, pats, opts)
 
@@ -1938,6 +1953,7 @@ 
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     revs = opts.get('rev')
     change = opts.get('change')
     stat = opts.get('stat')
@@ -2094,7 +2110,9 @@ 
     Returns 0 if a match is found, 1 otherwise.
 
     """
-    ctx = scmutil.revsingle(repo, opts.get(r'rev'), None)
+
+    opts = pycompat.byteskwargs(opts)
+    ctx = scmutil.revsingle(repo, opts.get('rev'), None)
 
     end = '\n'
     if opts.get('print0'):
@@ -2136,6 +2154,7 @@ 
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if not pats:
         raise error.Abort(_('no files specified'))
 
@@ -2220,6 +2239,7 @@ 
         return _dograft(ui, repo, *revs, **opts)
 
 def _dograft(ui, repo, *revs, **opts):
+    opts = pycompat.byteskwargs(opts)
     if revs and opts.get('rev'):
         ui.warn(_('warning: inconsistent use of --rev might give unexpected '
                   'revision ordering!\n'))
@@ -2437,6 +2457,8 @@ 
 
     Returns 0 if a match is found, 1 otherwise.
     """
+
+    opts = pycompat.byteskwargs(opts)
     reflags = re.M
     if opts.get('ignore_case'):
         reflags |= re.I
@@ -2683,6 +2705,7 @@ 
     Returns 0 if matching heads are found, 1 if not.
     """
 
+    opts = pycompat.byteskwargs(opts)
     start = None
     if 'rev' in opts:
         start = scmutil.revsingle(repo, opts['rev'], None).node()
@@ -2808,6 +2831,7 @@ 
     Returns 0 if successful.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if not repo and not source:
         raise error.Abort(_("there is no Mercurial repository here "
                            "(.hg not found)"))
@@ -3031,6 +3055,7 @@ 
     Returns 0 on success, 1 on partial success (see --partial).
     """
 
+    opts = pycompat.byteskwargs(opts)
     if not patch1:
         raise error.Abort(_('need at least one patch to import'))
 
@@ -3235,6 +3260,7 @@ 
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     hg.peer(ui, opts, ui.expandpath(dest), create=True)
 
 @command('locate',
@@ -3265,6 +3291,7 @@ 
 
     Returns 0 if a match is found, 1 otherwise.
     """
+    opts = pycompat.byteskwargs(opts)
     if opts.get('print0'):
         end = '\0'
     else:
@@ -3471,6 +3498,8 @@ 
 
     Returns 0 on success.
     """
+
+    opts = pycompat.byteskwargs(opts)
     fm = ui.formatter('manifest', opts)
 
     if opts.get('all'):
@@ -3549,6 +3578,7 @@ 
     Returns 0 on success, 1 if there are unresolved files.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('rev') and node:
         raise error.Abort(_("please specify just one revision"))
     if not node:
@@ -3628,6 +3658,7 @@ 
 
     Returns 0 if there are outgoing changes, 1 otherwise.
     """
+    opts = pycompat.byteskwargs(opts)
     if opts.get('graph'):
         cmdutil.checkunsupportedgraphflags([], opts)
         o, other = hg._outgoing(ui, repo, dest, opts)
@@ -3685,6 +3716,7 @@ 
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ctx = scmutil.revsingle(repo, opts.get('rev'), None)
 
     if file_:
@@ -3747,6 +3779,8 @@ 
 
     Returns 0 on success.
     """
+
+    opts = pycompat.byteskwargs(opts)
     ui.pager('paths')
     if search:
         pathitems = [(name, path) for name, path in ui.paths.iteritems()
@@ -3809,6 +3843,7 @@ 
 
     (For more information about the phases concept, see :hg:`help phases`.)
     """
+    opts = pycompat.byteskwargs(opts)
     # search for a unique phase argument
     targetphase = None
     for idx, name in enumerate(phases.phasenames):
@@ -3941,6 +3976,7 @@ 
     Returns 0 on success, 1 if an update had unresolved files.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if ui.configbool('commands', 'update.requiredest') and opts.get('update'):
         msg = _('update destination required by configuration')
         hint = _('use hg pull followed by hg update DEST')
@@ -4071,6 +4107,7 @@ 
     Returns 0 if push was successful, 1 if nothing to push.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts.get('bookmark'):
         ui.setconfig('bookmarks', 'pushing', opts['bookmark'], 'push')
         for b in opts['bookmark']:
@@ -4196,6 +4233,7 @@ 
     Returns 0 on success, 1 if any warnings encountered.
     """
 
+    opts = pycompat.byteskwargs(opts)
     after, force = opts.get('after'), opts.get('force')
     if not pats and not after:
         raise error.Abort(_('no files specified'))
@@ -4225,6 +4263,7 @@ 
 
     Returns 0 on success, 1 if errors are encountered.
     """
+    opts = pycompat.byteskwargs(opts)
     with repo.wlock(False):
         return cmdutil.copy(ui, repo, pats, opts, rename=True)
 
@@ -4279,6 +4318,7 @@ 
     Returns 0 on success, 1 if any files fail a resolve attempt.
     """
 
+    opts = pycompat.byteskwargs(opts)
     flaglist = 'all mark unmark list no_status'.split()
     all, mark, unmark, show, nostatus = \
         [opts.get(o) for o in flaglist]
@@ -4650,6 +4690,7 @@ 
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     if opts["stdio"] and opts["cmdserver"]:
         raise error.Abort(_("cannot use --stdio with --cmdserver"))
 
@@ -4815,6 +4856,7 @@ 
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ui.pager('summary')
     ctx = repo[None]
     parents = ctx.parents()
@@ -5123,6 +5165,7 @@ 
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     wlock = lock = None
     try:
         wlock = repo.wlock()
@@ -5210,6 +5253,7 @@ 
     Returns 0 on success.
     """
 
+    opts = pycompat.byteskwargs(opts)
     ui.pager('tags')
     fm = ui.formatter('tags', opts)
     hexfunc = fm.hexfunc
@@ -5254,6 +5298,7 @@ 
 
     Returns 0 on success.
     """
+    opts = pycompat.byteskwargs(opts)
     displayer = cmdutil.show_changeset(ui, repo, opts)
     displayer.show(repo['tip'])
     displayer.close()
@@ -5428,6 +5473,8 @@ 
 @command('version', [] + formatteropts, norepo=True)
 def version_(ui, **opts):
     """output version and copyright information"""
+
+    opts = pycompat.byteskwargs(opts)
     if ui.verbose:
         ui.pager('version')
     fm = ui.formatter("version", opts)