diff --git a/pyperformance/cli.py b/pyperformance/cli.py index cac15c33..3628b7a0 100644 --- a/pyperformance/cli.py +++ b/pyperformance/cli.py @@ -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,12 +342,13 @@ def parse_entry(o, s): # Get the selections. selected = [] - this_python_version = ".".join(map(str, sys.version_info[:3])) + info = _pythoninfo.get_info(python) + this_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 + # Filter using the target interpreter, which may differ from the running Python. if this_python_version in bench.python: selected.append(bench) diff --git a/pyperformance/tests/test_commands.py b/pyperformance/tests/test_commands.py index 5b44f015..80f63979 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):