diff mercurial/templater.py @ 26188:662ea52d5dca

templater: catch regexp error at sub() function This patch splits re.sub() into re.compile() and sub() so that it can distinguish which argument causes re.error.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 07 Sep 2015 21:58:17 +0900
parents 51f6940d3b4f
children fb6c08a9b40a
line wrap: on
line diff
--- a/mercurial/templater.py	Tue Sep 08 23:00:44 2015 +0900
+++ b/mercurial/templater.py	Mon Sep 07 21:58:17 2015 +0900
@@ -660,7 +660,16 @@
     pat = stringify(args[0][0](context, mapping, args[0][1]))
     rpl = stringify(args[1][0](context, mapping, args[1][1]))
     src = stringify(args[2][0](context, mapping, args[2][1]))
-    yield re.sub(pat, rpl, src)
+    try:
+        patre = re.compile(pat)
+    except re.error:
+        # i18n: "sub" is a keyword
+        raise error.ParseError(_("sub got an invalid pattern: %s") % pat)
+    try:
+        yield patre.sub(rpl, src)
+    except re.error:
+        # i18n: "sub" is a keyword
+        raise error.ParseError(_("sub got an invalid replacement: %s") % rpl)
 
 def startswith(context, mapping, args):
     """:startswith(pattern, text): Returns the value from the "text" argument