Skip to content

Commit b7517a7

Browse files
committed
Patch our old asv to work on CPython 3.12, too (for now)
1 parent a7c39a7 commit b7517a7

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

mx.graalpython/mx_graalpython_python_benchmarks.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,41 @@ def create_asv_benchmark_selection(benchmarks, skipped=()):
207207
return '^(?!' + '|'.join(negative_lookaheads) + ')(' + regex + ')'
208208

209209

210+
def patch_asv_for_cpython_312(workdir, vm_venv):
211+
pattern = join(workdir, vm_venv, "lib", "python*", "site-packages", "asv", "plugins", "virtualenv.py")
212+
candidates = glob.glob(pattern)
213+
if not candidates:
214+
mx.abort(f"Could not find ASV virtualenv plugin to patch: {pattern}")
215+
216+
if len(candidates) != 1:
217+
mx.abort(f"Found multiple ASV virtualenv plugins to patch: {candidates}")
218+
219+
virtualenv_py = candidates[0]
220+
with open(virtualenv_py) as f:
221+
content = f.read()
222+
223+
patched_import = "from packaging.version import parse as LooseVersion"
224+
if patched_import in content:
225+
mx.log(f"ASV virtualenv plugin already patched: {virtualenv_py}")
226+
return
227+
228+
distutils_import = "from distutils.version import LooseVersion"
229+
if distutils_import not in content:
230+
mx.abort(f"Unexpected ASV virtualenv plugin contents, cannot patch: {virtualenv_py}")
231+
232+
content = content.replace(
233+
distutils_import,
234+
"try:\n"
235+
" from packaging.version import parse as LooseVersion\n"
236+
"except Exception:\n"
237+
" from distutils.version import LooseVersion",
238+
1,
239+
)
240+
with open(virtualenv_py, "w") as f:
241+
f.write(content)
242+
mx.log(f"Patched ASV virtualenv plugin for CPython 3.12+: {virtualenv_py}")
243+
244+
210245
class PyPerfJsonRule(mx_benchmark.Rule):
211246
"""Parses a JSON file produced by PyPerf and creates a measurement result."""
212247

@@ -638,6 +673,8 @@ def _vmRun(self, vm, workdir, command, benchmarks, bmSuiteArgs):
638673
vm.run(workdir, ["-m", "venv", join(workdir, vm_venv)])
639674
pip = join(workdir, vm_venv, "bin", "pip")
640675
mx.run([pip, "install", *self.BENCHMARK_REQ], cwd=workdir)
676+
if vm.name() == "cpython":
677+
patch_asv_for_cpython_312(workdir, vm_venv)
641678
mx.run(
642679
[join(workdir, vm_venv, "bin", "asv"), "machine", "--yes"], cwd=benchdir
643680
)
@@ -773,6 +810,8 @@ def _vmRun(self, vm, workdir, command, benchmarks, bmSuiteArgs):
773810
env = os.environ.copy()
774811
env['PIP_CONSTRAINT'] = constraints.name
775812
mx.run([pip, "install", *self.BENCHMARK_REQ], cwd=workdir, env=env)
813+
if vm.name() == "cpython":
814+
patch_asv_for_cpython_312(workdir, vm_venv)
776815
mx.run(
777816
[join(workdir, vm_venv, "bin", "asv"), "machine", "--yes"], cwd=benchdir
778817
)

0 commit comments

Comments
 (0)