changeset 30801:67ee7874e53b

revset: factor out getinteger() helper We have 4 revset functions that take integer arguments, and they handle their arguments in slightly different ways. This patch unifies them: - getstring() in place of getsymbol(), which is more consistent with the handling of integer revisions (both 1 and '1' are valid) - say "expects" instead of "requires" for type errors We don't need to catch TypeError since getstring() must return a string.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 09 Jan 2017 17:39:44 +0900
parents cd23879cbac7
children 5eb3e4568c94
files mercurial/revset.py
diffstat 1 files changed, 20 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revset.py	Mon Jan 09 16:16:26 2017 +0900
+++ b/mercurial/revset.py	Mon Jan 09 17:39:44 2017 +0900
@@ -312,6 +312,12 @@
         return x[1]
     raise error.ParseError(err)
 
+def getinteger(x, err):
+    try:
+        return int(getstring(x, err))
+    except ValueError:
+        raise error.ParseError(err)
+
 def getlist(x):
     if not x:
         return []
@@ -539,10 +545,7 @@
     Changesets that are the Nth ancestor (first parents only) of a changeset
     in set.
     """
-    try:
-        n = int(n[1])
-    except (TypeError, ValueError):
-        raise error.ParseError(_("~ expects a number"))
+    n = getinteger(n, _("~ expects a number"))
     ps = set()
     cl = repo.changelog
     for r in getset(repo, fullreposet(repo), x):
@@ -1098,10 +1101,8 @@
             raise error.ParseError(_("followlines expects exactly one file"))
         fname = files[0]
 
-    try:
-        fromline, toline = [int(getsymbol(a)) for a in args['lines']]
-    except ValueError:
-        raise error.ParseError(_("line range bounds must be integers"))
+    fromline, toline = [getinteger(a, _("line range bounds must be integers"))
+                        for a in args['lines']]
     if toline - fromline < 0:
         raise error.ParseError(_("line range must be positive"))
     if fromline < 1:
@@ -1273,19 +1274,15 @@
     if 'set' not in args:
         # i18n: "limit" is a keyword
         raise error.ParseError(_("limit requires one to three arguments"))
-    try:
-        lim, ofs = 1, 0
-        if 'n' in args:
-            # i18n: "limit" is a keyword
-            lim = int(getstring(args['n'], _("limit requires a number")))
-        if 'offset' in args:
-            # i18n: "limit" is a keyword
-            ofs = int(getstring(args['offset'], _("limit requires a number")))
-        if ofs < 0:
-            raise error.ParseError(_("negative offset"))
-    except (TypeError, ValueError):
+    lim, ofs = 1, 0
+    if 'n' in args:
         # i18n: "limit" is a keyword
-        raise error.ParseError(_("limit expects a number"))
+        lim = getinteger(args['n'], _("limit expects a number"))
+    if 'offset' in args:
+        # i18n: "limit" is a keyword
+        ofs = getinteger(args['offset'], _("limit expects a number"))
+    if ofs < 0:
+        raise error.ParseError(_("negative offset"))
     os = getset(repo, fullreposet(repo), args['set'])
     result = []
     it = iter(os)
@@ -1308,14 +1305,10 @@
     """
     # i18n: "last" is a keyword
     l = getargs(x, 1, 2, _("last requires one or two arguments"))
-    try:
-        lim = 1
-        if len(l) == 2:
-            # i18n: "last" is a keyword
-            lim = int(getstring(l[1], _("last requires a number")))
-    except (TypeError, ValueError):
+    lim = 1
+    if len(l) == 2:
         # i18n: "last" is a keyword
-        raise error.ParseError(_("last expects a number"))
+        lim = getinteger(l[1], _("last expects a number"))
     os = getset(repo, fullreposet(repo), l[0])
     os.reverse()
     result = []