Skip to content

Commit b5d42b4

Browse files
committed
Fix re.subn DeprecationWarning in all three call sites in file.replace
Apply count=count keyword argument fix to the two remaining re.subn calls that the original commit missed (lines 2676 and 2892 in master). Add regression test and changelog entry.
1 parent 3b1e41f commit b5d42b4

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

changelog/69497.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `DeprecationWarning: 'count' is passed as positional argument` emitted by all three `re.subn` calls in `salt.modules.file.replace` on Python 3.12+.

salt/modules/file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ def replace(
26772677
cpattern,
26782678
repl_str.replace("\\", "\\\\") if backslash_literal else repl_str,
26792679
orig_contents,
2680-
count,
2680+
count=count,
26812681
)
26822682

26832683
found = nrepl > 0
@@ -2893,7 +2893,7 @@ def replace(
28932893
cpattern,
28942894
repl.replace(b"\\", b"\\\\") if backslash_literal else repl,
28952895
r_data,
2896-
count,
2896+
count=count,
28972897
)
28982898
try:
28992899
w_file.write(salt.utils.stringutils.to_str(result))

tests/pytests/functional/modules/file/test_replace.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,17 @@ def test_replace_no_modify_time_update_on_no_change(file, multiline_file):
211211
def test_backslash_literal(file, multiline_file):
212212
file.replace(str(multiline_file), r"Etiam", "Emma", backslash_literal=True)
213213
assert "Emma" in multiline_file.read_text()
214+
215+
216+
def test_replace_count_no_deprecation_warning(file, multiline_file):
217+
"""
218+
Regression test for https://github.com/saltstack/salt/pull/69497.
219+
file.replace must not emit a DeprecationWarning for passing 'count'
220+
as a positional argument to re.subn.
221+
"""
222+
import warnings
223+
224+
with warnings.catch_warnings():
225+
warnings.simplefilter("error", DeprecationWarning)
226+
file.replace(str(multiline_file), r"Etiam", "Salticus", count=1)
227+
assert "Salticus" in multiline_file.read_text()

0 commit comments

Comments
 (0)