diff --git a/doc/changelog.rst b/doc/changelog.rst index 5a5f1e19..d72e678f 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -6,6 +6,8 @@ Version 1.15.0 (unreleased) * Add support for Python 3.15 and drop 3.10 * Bump tornado to 6.6 (fixes ``BufferError`` on PyPy) * Bump genshi to 0.7.12 (fixes ``TypeError`` on Python 3.15) +* Fix benchmark selection checking Python version requirements against the + running interpreter instead of the target specified by ``--python`` Version 1.14.0 (2026-02-07) --------------------------- diff --git a/pyperformance/cli.py b/pyperformance/cli.py index cac15c33..cba81b52 100644 --- a/pyperformance/cli.py +++ b/pyperformance/cli.py @@ -185,7 +185,7 @@ def parse_args(): cmd.add_argument("changed_filename", metavar="changed_file.json") # list - cmd = subparsers.add_parser("list", help="List benchmarks of the running Python") + cmd = subparsers.add_parser("list", help="List benchmarks of the target Python") cmds.append(cmd) filter_opts(cmd) @@ -317,11 +317,11 @@ def _benchmarks_from_options(options): if not getattr(options, "benchmarks", None): return None manifest = _manifest_from_options(options) - return _select_benchmarks(options.benchmarks, manifest) + return _select_benchmarks(options.benchmarks, manifest, options.python) -def _select_benchmarks(raw, manifest): - from pyperformance import _benchmark_selections +def _select_benchmarks(raw, manifest, python): + from pyperformance import _benchmark_selections, _pythoninfo # Get the raw list of benchmarks. entries = raw.lower() @@ -342,13 +342,14 @@ def parse_entry(o, s): # Get the selections. selected = [] - this_python_version = ".".join(map(str, sys.version_info[:3])) + info = _pythoninfo.get_info(python) + target_python_version = ".".join(map(str, info.sys.version_info[:3])) for bench in _benchmark_selections.iter_selections(manifest, parsed_infos): if isinstance(bench, str): logging.warning(f"no benchmark named {bench!r}") continue - # Filter out any benchmarks that can't be run on the Python version we're running - if this_python_version in bench.python: + # Filter using the target interpreter, which may differ from the running Python. + if target_python_version in bench.python: selected.append(bench) return selected diff --git a/pyperformance/tests/test_commands.py b/pyperformance/tests/test_commands.py index f3924692..e6256e9b 100644 --- a/pyperformance/tests/test_commands.py +++ b/pyperformance/tests/test_commands.py @@ -1,11 +1,30 @@ import os import os.path import shutil +import sys import textwrap import unittest +from types import SimpleNamespace +from unittest import mock import pyperformance -from pyperformance import tests +from pyperformance import _pythoninfo, cli, tests + + +class BenchmarkSelectionTests(unittest.TestCase): + def test_target_python_version(self): + options = SimpleNamespace( + benchmarks="async_tree_eager", manifest=None, python="target-python" + ) + info = SimpleNamespace(sys=SimpleNamespace(version_info=(3, 16, 0))) + with ( + mock.patch.object(sys, "version_info", (3, 11, 0)), + mock.patch.object(_pythoninfo, "get_info", return_value=info) as get_info, + ): + benchmarks = cli._benchmarks_from_options(options) + + get_info.assert_called_once_with("target-python") + self.assertEqual([bench.name for bench in benchmarks], ["async_tree_eager"]) class FullStackTests(tests.Functional, unittest.TestCase):