Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed backslash literal bytestring for file.replace #62069

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()