Fix backslash replacement issue

This commit is contained in:
Joe Rickerby
2021-11-14 13:38:41 +00:00
parent 97e3fbbd73
commit f4b14d3da0
2 changed files with 15 additions and 1 deletions
+7 -1
View File
@@ -76,7 +76,13 @@ def format_safe(template: str, **kwargs: Any) -> str:
re.VERBOSE,
)
result = re.sub(find_pattern, str(value), result)
# we use a lambda for repl to prevent re.sub interpreting backslashes
# in repl as escape sequences
result = re.sub(
pattern=find_pattern,
repl=lambda _: str(value),
string=result,
)
# transform escaped sequences into their literal equivalents
result = result.replace(f"\\{{{key}}}", f"{{{key}}}")
+8
View File
@@ -29,6 +29,14 @@ def test_prepare_command():
== "python -m {something.abc[4]:3f}"
)
# test backslashes in the replacement
assert (
prepare_command(
"command {wheel} \\Users\\Temp\\output_dir", wheel="\\Temporary Files\\cibw"
)
== "command \\Temporary Files\\cibw \\Users\\Temp\\output_dir"
)
# test some unusual syntax that used to trip up the str.format approach
assert (
prepare_command("{a}{a,b}{b:.2e}{c}{d%s}{e:3}{f[0]}", a="42", b="3.14159")