Commit b1c5024
authored
Resolve BENCH_DIR at call time in the benchmark runner's main() (#2563)
discover_benchmarks() goes out of its way to avoid def-time binding, and
says so:
# Resolve the default inside the call so tests (and embedders) can
# monkeypatch ``BENCH_DIR`` at the module level - Python binds default
# args at def-time, so a literal default would ignore later patches.
if bench_dir is None:
bench_dir = BENCH_DIR
main() then reintroduces exactly that binding:
def main(
*,
bench_dir: Path = BENCH_DIR,
default_output: Path = DEFAULT_OUTPUT,
...
registry = discover_benchmarks(bench_dir=bench_dir, ...)
Because main() always passes a non-None bench_dir down, the sentinel branch
in discover_benchmarks() can never be taken on this path, and patching
runner.main.BENCH_DIR - the documented mechanism - has no effect on main().
Same for DEFAULT_OUTPUT.
run_pyperf.py calls main() with no arguments, so this is the production
path. The existing tests patch BENCH_DIR and call discover_benchmarks()
directly, which is why the gap is invisible today.
Apply the same sentinel to both parameters. Explicit arguments keep working
unchanged, so the embedder API is unaffected.
Adds test_main_honors_a_monkeypatched_bench_dir, which patches BENCH_DIR to a
tmp dir holding one bench_*.py and drives main() with --list. It fails before
this change (main() lists the repo's real benchmarks instead).1 parent b8a255d commit b1c5024
2 files changed
Lines changed: 30 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
235 | | - | |
236 | | - | |
| 235 | + | |
| 236 | + | |
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
240 | 247 | | |
241 | 248 | | |
242 | 249 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
0 commit comments