Skip to content

Commit

Permalink
fixes #61944 fixed backslash literal bytestring
Browse files Browse the repository at this point in the history
  • Loading branch information
emmadionne1 authored and Megan Wilhite committed May 19, 2022
1 parent c4593b8 commit 5e03515
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/61944.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed error when using backslash literal in file.replace
4 changes: 2 additions & 2 deletions salt/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ def replace(
else:
result, nrepl = re.subn(
cpattern,
repl.replace("\\", "\\\\") if backslash_literal else repl,
repl.replace(b"\\", b"\\\\") if backslash_literal else repl,
r_data,
count,
)
Expand Down Expand Up @@ -2624,7 +2624,7 @@ def replace(
r_data = mmap.mmap(r_file.fileno(), 0, access=mmap.ACCESS_READ)
result, nrepl = re.subn(
cpattern,
repl.replace("\\", "\\\\") if backslash_literal else repl,
repl.replace(b"\\", b"\\\\") if backslash_literal else repl,
r_data,
count,
)
Expand Down
5 changes: 5 additions & 0 deletions tests/pytests/functional/modules/file/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,8 @@ def test_replace_no_modify_time_update_on_no_change(file, multiline_file):
nmtime = os.stat(str(multiline_file)).st_mtime

assert mtime == nmtime


def test_backslash_literal(file, multiline_file):
file.replace(str(multiline_file), r"Etiam", "Emma", backslash_literal=True)
assert "Emma" in multiline_file.read_text()

0 comments on commit 5e03515

Please sign in to comment.