comparison mercurial/templater.py @ 30116:1c01fa29630f

templater: handle division by zero in arithmetic For now, just turn it to an abort.
author Simon Farnsworth <simonfar@fb.com>
date Sun, 09 Oct 2016 08:09:20 -0700
parents 8e42dfde93d1
children 741e5d7f282d
comparison
equal deleted inserted replaced
30115:8e42dfde93d1 30116:1c01fa29630f
437 func, left, right = data 437 func, left, right = data
438 left = evalinteger(context, mapping, left, 438 left = evalinteger(context, mapping, left,
439 _('arithmetic only defined on integers')) 439 _('arithmetic only defined on integers'))
440 right = evalinteger(context, mapping, right, 440 right = evalinteger(context, mapping, right,
441 _('arithmetic only defined on integers')) 441 _('arithmetic only defined on integers'))
442 return func(left, right) 442 try:
443 return func(left, right)
444 except ZeroDivisionError:
445 raise error.Abort(_('division by zero is not defined'))
443 446
444 def buildfunc(exp, context): 447 def buildfunc(exp, context):
445 n = getsymbol(exp[1]) 448 n = getsymbol(exp[1])
446 args = [compileexp(x, context, exprmethods) for x in getlist(exp[2])] 449 args = [compileexp(x, context, exprmethods) for x in getlist(exp[2])]
447 if n in funcs: 450 if n in funcs:
739 """Calculate a mod b such that a / b + a mod b == a""" 742 """Calculate a mod b such that a / b + a mod b == a"""
740 if not len(args) == 2: 743 if not len(args) == 2:
741 # i18n: "mod" is a keyword 744 # i18n: "mod" is a keyword
742 raise error.ParseError(_("mod expects two arguments")) 745 raise error.ParseError(_("mod expects two arguments"))
743 746
744 left = evalinteger(context, mapping, args[0], 747 func = lambda a, b: a % b
745 _('arithmetic only defined on integers')) 748 return runarithmetic(context, mapping, (func, args[0], args[1]))
746 right = evalinteger(context, mapping, args[1],
747 _('arithmetic only defined on integers'))
748
749 return left % right
750 749
751 @templatefunc('relpath(path)') 750 @templatefunc('relpath(path)')
752 def relpath(context, mapping, args): 751 def relpath(context, mapping, args):
753 """Convert a repository-absolute path into a filesystem path relative to 752 """Convert a repository-absolute path into a filesystem path relative to
754 the current working directory.""" 753 the current working directory."""