From f4b14d3da07b643698dd4c3111ab6abebd4c0012 Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Sun, 14 Nov 2021 13:38:41 +0000 Subject: [PATCH] Fix backslash replacement issue --- cibuildwheel/util.py | 8 +++++++- unit_test/utils_test.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index a4e3058f..0e411351 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -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}}}") diff --git a/unit_test/utils_test.py b/unit_test/utils_test.py index cc501349..cdd7a2da 100644 --- a/unit_test/utils_test.py +++ b/unit_test/utils_test.py @@ -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")