Skip to content

Commit f766904

Browse files
authored
Merge pull request #1258 from henryiii/henryiii/chore/dev
chore: nicer tracebacks and dev improvements
2 parents 73a435b + c302a1d commit f766904

19 files changed

+95
-58
lines changed

bin/inspect_all_known_projects.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
#!/usr/bin/env python3
2+
3+
"""
4+
Check known projects for usage of requires-python.
5+
6+
Usage:
7+
8+
./bin/inspect_all_known_projects.py --online=$GITHUB_TOKEN
9+
10+
This will cache the results to all_known_setup.yaml; you can reprint
11+
the results without the `--online` setting.
12+
"""
13+
14+
215
from __future__ import annotations
316

417
import ast
@@ -7,7 +20,7 @@
720

821
import click
922
import yaml
10-
from ghapi.core import GhApi, HTTP404NotFoundError
23+
from github import Github, GithubException
1124
from rich import print
1225

1326
from cibuildwheel.projectfiles import Analyzer
@@ -47,33 +60,38 @@ def check_repo(name: str, contents: str) -> str:
4760

4861

4962
class MaybeRemote:
50-
def __init__(self, cached_file: Path | str, *, online: bool) -> None:
51-
self.online = online
52-
if self.online:
53-
self.contents: dict[str, dict[str, str | None]] = {
63+
github: Github | None
64+
contents: dict[str, dict[str, str | None]]
65+
66+
def __init__(self, cached_file: Path | str, *, online: str | None) -> None:
67+
if online is not None:
68+
self.github = Github(online)
69+
self.contents = {
5470
"setup.py": {},
5571
"setup.cfg": {},
5672
"pyproject.toml": {},
5773
}
5874
else:
75+
self.github = None
5976
with open(cached_file) as f:
6077
self.contents = yaml.safe_load(f)
6178

6279
def get(self, repo: str, filename: str) -> str | None:
63-
if self.online:
80+
if self.github:
6481
try:
65-
self.contents[filename][repo] = (
66-
GhApi(*repo.split("/")).get_content(filename).decode()
67-
)
68-
except HTTP404NotFoundError:
82+
gh_file = self.github.get_repo(repo).get_contents(filename)
83+
except GithubException:
6984
self.contents[filename][repo] = None
85+
else:
86+
assert not isinstance(gh_file, list)
87+
self.contents[filename][repo] = gh_file.decoded_content.decode(encoding="utf-8")
88+
7089
return self.contents[filename][repo]
7190
elif repo in self.contents[filename]:
7291
return self.contents[filename][repo]
7392
else:
74-
raise RuntimeError(
75-
f"Trying to access {repo}:{filename} and not in cache, rebuild cache"
76-
)
93+
msg = f"Trying to access {repo}:{filename} and not in cache, rebuild cache"
94+
raise RuntimeError(msg)
7795

7896
def save(self, filename: Path | str) -> None:
7997
with open(filename, "w") as f:
@@ -87,8 +105,8 @@ def on_each(self, repos: list[str]) -> Iterator[tuple[str, str, str | None]]:
87105

88106

89107
@click.command()
90-
@click.option("--online", is_flag=True, help="Remember to set GITHUB_TOKEN")
91-
def main(online: bool) -> None:
108+
@click.option("--online", help="Set to $GITHUB_TOKEN")
109+
def main(online: str | None) -> None:
92110
with open(DIR / "../docs/data/projects.yml") as f:
93111
known = yaml.safe_load(f)
94112

bin/run_example_ci_configs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def ci_service_for_config_file(config_file):
8181
if service.name == service_name:
8282
return service
8383

84-
raise ValueError(f"unknown ci service for config file {config_file}")
84+
msg = f"unknown ci service for config file {config_file}"
85+
raise ValueError(msg)
8586

8687

8788
@click.command()
@@ -100,7 +101,8 @@ def run_example_ci_configs(config_files=None):
100101
for config_file in config_files:
101102
service = ci_service_for_config_file(config_file)
102103
if service.name in configs_by_service:
103-
raise Exception("You cannot specify more than one config per CI service")
104+
msg = "You cannot specify more than one config per CI service"
105+
raise Exception(msg)
104106
configs_by_service[service.name] = config_file
105107

106108
if git_repo_has_changes():

bin/update_pythons.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ def update_version_windows(self, spec: Specifier) -> ConfigWinCP:
141141
releases = [r for r in releases if self.get_arch_file(r)]
142142

143143
if not releases:
144-
raise RuntimeError(f"PyPy Win {self.arch} not found for {spec}! {self.releases}")
144+
msg = f"PyPy Win {self.arch} not found for {spec}! {self.releases}"
145+
raise RuntimeError(msg)
145146

146147
version_arch = "win32" if self.arch == "32" else "win_amd64"
147148

@@ -159,13 +160,15 @@ def update_version_windows(self, spec: Specifier) -> ConfigWinCP:
159160

160161
def update_version_macos(self, spec: Specifier) -> ConfigMacOS:
161162
if self.arch != "64":
162-
raise RuntimeError("Other archs not supported yet on macOS")
163+
msg = "Other archs not supported yet on macOS"
164+
raise RuntimeError(msg)
163165

164166
releases = [r for r in self.releases if spec.contains(r["python_version"])]
165167
releases = sorted(releases, key=lambda r: r["pypy_version"]) # type: ignore[no-any-return]
166168

167169
if not releases:
168-
raise RuntimeError(f"PyPy macOS {self.arch} not found for {spec}!")
170+
msg = f"PyPy macOS {self.arch} not found for {spec}!"
171+
raise RuntimeError(msg)
169172

170173
release = releases[-1]
171174
version = release["python_version"]

cibuildwheel/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def main() -> None:
149149
try:
150150
(project_dir,) = temp_dir.iterdir()
151151
except ValueError:
152-
raise SystemExit("invalid sdist: didn't contain a single dir") from None
152+
msg = "invalid sdist: didn't contain a single dir"
153+
raise SystemExit(msg) from None
153154

154155
# This is now the new package dir
155156
args.package_dir = project_dir.resolve()

cibuildwheel/bashlex_eval.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def evaluate(
3232
command_node = bashlex.parsesingle(value)
3333

3434
if len(command_node.parts) != 1:
35-
raise ValueError(f'"{value}" has too many parts')
35+
msg = f"{value!r} has too many parts"
36+
raise ValueError(msg)
3637

3738
value_word_node = command_node.parts[0]
3839

@@ -54,7 +55,8 @@ def evaluate_node(node: bashlex.ast.node, context: NodeExecutionContext) -> str:
5455
elif node.kind == "parameter":
5556
return evaluate_parameter_node(node, context=context)
5657
else:
57-
raise ValueError(f'Unsupported bash construct: "{node.kind}"')
58+
msg = f"Unsupported bash construct: {node.kind!r}"
59+
raise ValueError(msg)
5860

5961

6062
def evaluate_word_node(node: bashlex.ast.node, context: NodeExecutionContext) -> str:
@@ -65,10 +67,8 @@ def evaluate_word_node(node: bashlex.ast.node, context: NodeExecutionContext) ->
6567
part_value = evaluate_node(part, context=context)
6668

6769
if part_string not in value:
68-
raise RuntimeError(
69-
f'bash parse failed. part "{part_string}" not found in "{value}". '
70-
f'Word was "{node.word}". Full input was "{context.input}"'
71-
)
70+
msg = f"bash parse failed. part {part_string!r} not found in {value!r}. Word was {node.word!r}. Full input was {context.input!r}"
71+
raise RuntimeError(msg)
7272

7373
value = value.replace(part_string, part_value, 1)
7474

@@ -95,9 +95,11 @@ def evaluate_nodes_as_compound_command(
9595
result += evaluate_command_node(node, context=context)
9696
elif node.kind == "operator":
9797
if node.op != ";":
98-
raise ValueError(f'Unsupported bash operator: "{node.op}"')
98+
msg = f"Unsupported bash operator: {node.op!r}"
99+
raise ValueError(msg)
99100
else:
100-
raise ValueError(f'Unsupported bash node in compound command: "{node.kind}"')
101+
msg = f"Unsupported bash node in compound command: {node.kind!r}"
102+
raise ValueError(msg)
101103

102104
return result
103105

cibuildwheel/functools_cached_property_38.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ def __set_name__(self, owner: type[Any], name: str) -> None:
2121
if self.attrname is None:
2222
self.attrname = name
2323
elif name != self.attrname:
24-
raise TypeError(
25-
"Cannot assign the same cached_property to two different names "
26-
f"({self.attrname!r} and {name!r})."
27-
)
24+
msg = f"Cannot assign the same cached_property to two different names ({self.attrname!r} and {name!r})."
25+
raise TypeError(msg)
2826

2927
@overload
3028
def __get__(self, instance: None, owner: type[Any] | None = ...) -> cached_property[_T]:
@@ -38,9 +36,8 @@ def __get__(self, instance: object | None, owner: type[Any] | None = None) -> An
3836
if instance is None:
3937
return self
4038
if self.attrname is None:
41-
raise TypeError(
42-
"Cannot use cached_property instance without calling __set_name__ on it."
43-
)
39+
msg = "Cannot use cached_property instance without calling __set_name__ on it."
40+
raise TypeError(msg)
4441
try:
4542
cache = instance.__dict__
4643
except AttributeError: # not all objects have __dict__ (e.g. class defines slots)

cibuildwheel/linux.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ def build(options: Options, tmp_path: Path) -> None: # pylint: disable=unused-a
367367
cwd = Path.cwd()
368368
abs_package_dir = options.globals.package_dir.resolve()
369369
if cwd != abs_package_dir and cwd not in abs_package_dir.parents:
370-
raise Exception("package_dir must be inside the working directory")
370+
msg = "package_dir must be inside the working directory"
371+
raise Exception(msg)
371372

372373
container_project_path = PurePosixPath("/project")
373374
container_package_dir = container_project_path / abs_package_dir.relative_to(cwd)

cibuildwheel/logger.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,16 @@ def build_description_from_identifier(identifier: str) -> str:
197197
elif python_interpreter == "pp":
198198
build_description += "PyPy"
199199
else:
200-
raise Exception("unknown python")
200+
msg = f"unknown python {python_interpreter!r}"
201+
raise Exception(msg)
201202

202203
build_description += f" {python_version[0]}.{python_version[1:]} "
203204

204205
try:
205206
build_description += PLATFORM_IDENTIFIER_DESCRIPTIONS[platform_identifier]
206207
except KeyError as e:
207-
raise Exception("unknown platform") from e
208+
msg = f"unknown platform {platform_identifier!r}"
209+
raise Exception(msg) from e
208210

209211
return build_description
210212

cibuildwheel/macos.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def setup_python(
146146
elif implementation_id.startswith("pp"):
147147
base_python = install_pypy(tmp, python_configuration.url)
148148
else:
149-
raise ValueError("Unknown Python implementation")
149+
msg = "Unknown Python implementation"
150+
raise ValueError(msg)
150151
assert base_python.exists()
151152

152153
log.step("Setting up build environment...")
@@ -466,7 +467,8 @@ def build(options: Options, tmp_path: Path) -> None:
466467
)
467468
)
468469
else:
469-
raise RuntimeError("unreachable")
470+
msg = "unreachable"
471+
raise RuntimeError(msg)
470472

471473
# skip this test
472474
continue

cibuildwheel/oci_container.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def __init__(
5858
engine: ContainerEngine = "docker",
5959
):
6060
if not image:
61-
raise ValueError("Must have a non-empty image to run.")
61+
msg = "Must have a non-empty image to run."
62+
raise ValueError(msg)
6263

6364
self.image = image
6465
self.simulate_32_bit = simulate_32_bit

0 commit comments

Comments
 (0)