comparison mercurial/templater.py @ 28343:a6c2310b3827

templater: factor out function that evaluates argument as integer We have more bare int()s that may raise ValueError or TypeError. This function will be used to fix them.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 14 Feb 2016 12:42:25 +0900
parents 9dc340f51e06
children ac371d4c007f
comparison
equal deleted inserted replaced
28342:bd05d38a1002 28343:a6c2310b3827
218 thing = func(context, mapping, data) 218 thing = func(context, mapping, data)
219 if isinstance(thing, types.GeneratorType): 219 if isinstance(thing, types.GeneratorType):
220 thing = stringify(thing) 220 thing = stringify(thing)
221 return thing 221 return thing
222 222
223 def evalinteger(context, mapping, arg, err):
224 try:
225 return int(stringify(arg[0](context, mapping, arg[1])))
226 except ValueError:
227 raise error.ParseError(err)
228
223 def runinteger(context, mapping, data): 229 def runinteger(context, mapping, data):
224 return int(data) 230 return int(data)
225 231
226 def runstring(context, mapping, data): 232 def runstring(context, mapping, data):
227 return data 233 return data
371 text = stringify(args[0][0](context, mapping, args[0][1])) 377 text = stringify(args[0][0](context, mapping, args[0][1]))
372 width = 76 378 width = 76
373 initindent = '' 379 initindent = ''
374 hangindent = '' 380 hangindent = ''
375 if 2 <= len(args) <= 4: 381 if 2 <= len(args) <= 4:
376 try: 382 width = evalinteger(context, mapping, args[1],
377 width = int(stringify(args[1][0](context, mapping, args[1][1]))) 383 # i18n: "fill" is a keyword
378 except ValueError: 384 _("fill expects an integer width"))
379 # i18n: "fill" is a keyword
380 raise error.ParseError(_("fill expects an integer width"))
381 try: 385 try:
382 initindent = stringify(args[2][0](context, mapping, args[2][1])) 386 initindent = stringify(args[2][0](context, mapping, args[2][1]))
383 hangindent = stringify(args[3][0](context, mapping, args[3][1])) 387 hangindent = stringify(args[3][0](context, mapping, args[3][1]))
384 except IndexError: 388 except IndexError:
385 pass 389 pass
708 if not (2 <= len(args) <= 3): 712 if not (2 <= len(args) <= 3):
709 # i18n: "word" is a keyword 713 # i18n: "word" is a keyword
710 raise error.ParseError(_("word expects two or three arguments, got %d") 714 raise error.ParseError(_("word expects two or three arguments, got %d")
711 % len(args)) 715 % len(args))
712 716
713 try: 717 num = evalinteger(context, mapping, args[0],
714 num = int(stringify(args[0][0](context, mapping, args[0][1]))) 718 # i18n: "word" is a keyword
715 except ValueError: 719 _("word expects an integer index"))
716 # i18n: "word" is a keyword
717 raise error.ParseError(_("word expects an integer index"))
718 text = stringify(args[1][0](context, mapping, args[1][1])) 720 text = stringify(args[1][0](context, mapping, args[1][1]))
719 if len(args) == 3: 721 if len(args) == 3:
720 splitter = stringify(args[2][0](context, mapping, args[2][1])) 722 splitter = stringify(args[2][0](context, mapping, args[2][1]))
721 else: 723 else:
722 splitter = None 724 splitter = None