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

fix: pass through unexpected format strings #884

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions cibuildwheel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from enum import Enum
from pathlib import Path
from time import sleep
from typing import Dict, Iterator, List, Optional
from typing import Dict, Iterator, List, Optional, TypeVar

import bracex
import certifi
Expand Down Expand Up @@ -48,14 +48,36 @@
)


T = TypeVar("T", bound="LimitedExpandStr")


class LimitedExpandStr:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't honestly say I understand what's happening in this class... I'd have expected that

def __missing__(self, key):
    return "{" + key + "}"

would have done the job. what extra is this class doing? (might be nice to add this as a docstring, too)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because valid Bash syntax will trip this up otherwise. This makes it slightly less brittle. Colons and especially brackets are valid in Bash, but would trip up our expansion if we didn't have this little class.

def __init__(self, inner: PathOrStr) -> None:
self.inner = str(inner)

def __format__(self, fmt: str) -> str:
return str(self.__class__(self.inner + (f":{fmt}" if fmt else "")))

def __getitem__(self: T, item: int) -> T:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __getitem__(self: T, item: int) -> T:
def __getitem__(self: T, item: Any) -> T:

return self.__class__(f"{self.inner}[{item}]")

def __str__(self) -> str:
return f"{{{self.inner}}}"
joerick marked this conversation as resolved.
Show resolved Hide resolved


class SafeDict(Dict[str, PathOrStr]):
def __missing__(self, key: str) -> LimitedExpandStr:
Copy link
Contributor Author

@henryiii henryiii Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __missing__(self, key: str) -> LimitedExpandStr:
def __missing__(self, key: PathOrStr) -> LimitedExpandStr:

Will commit this after review, just to possibly save a CI run.

return LimitedExpandStr(key)


def prepare_command(command: str, **kwargs: PathOrStr) -> str:
"""
Preprocesses a command by expanding variables like {python}.

For example, used in the test_command option to specify the path to the
project's root.
project's root. Unmatched syntax will mostly be allowed through.
"""
return command.format(python="python", pip="pip", **kwargs)
return command.format_map(SafeDict(python="python", pip="pip", **kwargs))


def get_build_verbosity_extra_flags(level: int) -> List[str]:
Expand Down
12 changes: 12 additions & 0 deletions unit_test/utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from cibuildwheel.util import prepare_command


def test_prepare_command():
assert prepare_command("python -m {project}", project="project") == "python -m project"
assert prepare_command("python -m {something}", project="project") == "python -m {something}"
assert (
prepare_command(
"{a,b}{b:.2e}{{c}}{d%s}{e:3}{f[0]}", a=42, b=3.14159 # type:ignore[arg-type]
)
== "{a,b}3.14e+00{c}{d%s}{e:3}{f[0]}"
)