mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
#6509: fix re.sub to work properly when the pattern, the string, and the replacement were all bytes. Patch by Antoine Pitrou.
This commit is contained in:
parent
64fb18e192
commit
b92ed7cf36
3 changed files with 28 additions and 1 deletions
|
@ -786,12 +786,18 @@ def parse_template(source, pattern):
|
|||
groups = []
|
||||
groupsappend = groups.append
|
||||
literals = [None] * len(p)
|
||||
if isinstance(source, str):
|
||||
encode = lambda x: x
|
||||
else:
|
||||
# The tokenizer implicitly decodes bytes objects as latin-1, we must
|
||||
# therefore re-encode the final representation.
|
||||
encode = lambda x: x.encode('latin1')
|
||||
for c, s in p:
|
||||
if c is MARK:
|
||||
groupsappend((i, s))
|
||||
# literal[i] is already None
|
||||
else:
|
||||
literals[i] = s
|
||||
literals[i] = encode(s)
|
||||
i = i + 1
|
||||
return groups, literals
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue