From 88f567f2ff31f64bcc5e9ca0a4016e3ef3e72abc Mon Sep 17 00:00:00 2001 From: Aaron Boman Date: Sat, 8 Oct 2016 17:34:51 -0500 Subject: [PATCH] Prevent backreferences from breaking regex.transform(). --- src/util/regex.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/util/regex.py b/src/util/regex.py index 6348c6fb1..37eeb6d88 100644 --- a/src/util/regex.py +++ b/src/util/regex.py @@ -38,7 +38,16 @@ def replace(s, pattern, replacement): pattern (str): the search expression replacement (str): the string to replace each match with """ - return re.sub(pattern, replacement, s) + # the replacement string may contain invalid backreferences (like \1 or \g) + # which will cause python's regex to blow up. Since this should emulate + # the jam version exactly and the jam version didn't support + # backreferences, this version shouldn't either. re.sub + # allows replacement to be a callable; this is being used + # to simply return the replacement string and avoid the hassle + # of worrying about backreferences within the string. + def _replacement(matchobj): + return replacement + return re.sub(pattern, _replacement, s) @bjam_signature((['items', '*'], ['match'], ['replacement']))