Issue 1519638: Now unmatched groups are replaced with empty strings in re.sub()

and re.subn().
This commit is contained in:
Serhiy Storchaka 2014-10-10 11:06:31 +03:00
parent 365e28238f
commit 7438e4b56f
5 changed files with 26 additions and 8 deletions

View file

@ -880,14 +880,12 @@ def parse_template(source, pattern):
def expand_template(template, match):
g = match.group
sep = match.string[:0]
empty = match.string[:0]
groups, literals = template
literals = literals[:]
try:
for index, group in groups:
literals[index] = s = g(group)
if s is None:
raise error("unmatched group")
literals[index] = g(group) or empty
except IndexError:
raise error("invalid group reference")
return sep.join(literals)
return empty.join(literals)