Patchwork templater: handle division by zero in arithmetic

login
register
mail settings
Submitter Simon Farnsworth
Date Oct. 9, 2016, 3:11 p.m.
Message ID <ad830281f2cd1a6fd224.1476025867@devvm022.lla2.facebook.com>
Download mbox | patch
Permalink /patch/17014/
State Accepted
Headers show

Comments

Simon Farnsworth - Oct. 9, 2016, 3:11 p.m.
# HG changeset patch
# User Simon Farnsworth <simonfar@fb.com>
# Date 1476025760 25200
#      Sun Oct 09 08:09:20 2016 -0700
# Node ID ad830281f2cd1a6fd2249813a8a1ceddf3d0d2e8
# Parent  8e42dfde93d10e099040e9b57c70b7774235d883
templater: handle division by zero in arithmetic

For now, just turn it to an abort.
Yuya Nishihara - Oct. 9, 2016, 3:25 p.m.
On Sun, 9 Oct 2016 08:11:07 -0700, Simon Farnsworth wrote:
> # HG changeset patch
> # User Simon Farnsworth <simonfar@fb.com>
> # Date 1476025760 25200
> #      Sun Oct 09 08:09:20 2016 -0700
> # Node ID ad830281f2cd1a6fd2249813a8a1ceddf3d0d2e8
> # Parent  8e42dfde93d10e099040e9b57c70b7774235d883
> templater: handle division by zero in arithmetic

Queued, thanks.
timeless - Oct. 21, 2016, 2:03 p.m.
Should we have a test for this?

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -439,7 +439,10 @@ 
                        _('arithmetic only defined on integers'))
     right = evalinteger(context, mapping, right,
                         _('arithmetic only defined on integers'))
-    return func(left, right)
+    try:
+        return func(left, right)
+    except ZeroDivisionError:
+        raise error.Abort(_('division by zero is not defined'))
 
 def buildfunc(exp, context):
     n = getsymbol(exp[1])
@@ -741,12 +744,8 @@ 
         # i18n: "mod" is a keyword
         raise error.ParseError(_("mod expects two arguments"))
 
-    left = evalinteger(context, mapping, args[0],
-                       _('arithmetic only defined on integers'))
-    right = evalinteger(context, mapping, args[1],
-                        _('arithmetic only defined on integers'))
-
-    return left % right
+    func = lambda a, b: a % b
+    return runarithmetic(context, mapping, (func, args[0], args[1]))
 
 @templatefunc('relpath(path)')
 def relpath(context, mapping, args):