Use hash rather than backslash for escaping
This commit is contained in:
@@ -68,7 +68,7 @@ def format_safe(template: str, **kwargs: Any) -> str:
|
|||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
find_pattern = re.compile(
|
find_pattern = re.compile(
|
||||||
fr"""
|
fr"""
|
||||||
(?<!\\) # don't match if preceded by a backslash
|
(?<!\#) # don't match if preceded by a hash
|
||||||
{{ # literal open curly bracket
|
{{ # literal open curly bracket
|
||||||
{re.escape(key)} # the field name
|
{re.escape(key)} # the field name
|
||||||
}} # literal close curly bracket
|
}} # literal close curly bracket
|
||||||
@@ -85,7 +85,7 @@ def format_safe(template: str, **kwargs: Any) -> str:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# transform escaped sequences into their literal equivalents
|
# transform escaped sequences into their literal equivalents
|
||||||
result = result.replace(f"\\{{{key}}}", f"{{{key}}}")
|
result = result.replace(f"#{{{key}}}", f"{{{key}}}")
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ from cibuildwheel.util import format_safe, prepare_command
|
|||||||
|
|
||||||
def test_format_safe():
|
def test_format_safe():
|
||||||
assert format_safe("{wheel}", wheel="filename.whl") == "filename.whl"
|
assert format_safe("{wheel}", wheel="filename.whl") == "filename.whl"
|
||||||
assert format_safe("command \\{wheel}", wheel="filename.whl") == "command {wheel}"
|
assert format_safe("command #{wheel}", wheel="filename.whl") == "command {wheel}"
|
||||||
assert format_safe("{command \\{wheel}}", wheel="filename.whl") == "{command {wheel}}"
|
assert format_safe("{command #{wheel}}", wheel="filename.whl") == "{command {wheel}}"
|
||||||
|
|
||||||
# check unmatched brackets
|
# check unmatched brackets
|
||||||
assert format_safe("{command {wheel}", wheel="filename.whl") == "{command filename.whl"
|
assert format_safe("{command {wheel}", wheel="filename.whl") == "{command filename.whl"
|
||||||
@@ -15,6 +15,10 @@ def test_format_safe():
|
|||||||
== "find . -name * -exec ls -a {} \\;"
|
== "find . -name * -exec ls -a {} \\;"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert format_safe("{param} {param}", param="1") == "1 1"
|
||||||
|
assert format_safe("# {param} {param}", param="1") == "# 1 1"
|
||||||
|
assert format_safe("#{not_a_param} {param}", param="1") == "#{not_a_param} 1"
|
||||||
|
|
||||||
|
|
||||||
def test_prepare_command():
|
def test_prepare_command():
|
||||||
assert prepare_command("python -m {project}", project="project") == "python -m project"
|
assert prepare_command("python -m {project}", project="project") == "python -m project"
|
||||||
|
|||||||
Reference in New Issue
Block a user