From 7ade3828313dd437f4e2176ccbbc1ef52322de15 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Fri, 20 Oct 2023 17:29:26 +0200 Subject: [PATCH] Stop using removed `importlib.resources` functions on Python >=3.13 Closes gh-12401 --- mesonbuild/dependencies/python.py | 8 +++++++- mesonbuild/modules/python.py | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py index 1607728883df..186a6830ddbf 100644 --- a/mesonbuild/dependencies/python.py +++ b/mesonbuild/dependencies/python.py @@ -15,6 +15,7 @@ import functools, json, os, textwrap from pathlib import Path +import sys import typing as T from .. import mesonlib, mlog @@ -110,8 +111,13 @@ def sanity(self) -> bool: # Sanity check, we expect to have something that at least quacks in tune import importlib.resources + if sys.version_info >= (3, 13): + traversable = importlib.resources.files('mesonbuild.scripts').joinpath('python_info.py') + context_mgr = importlib.resources.as_file(traversable) + else: + context_mgr = importlib.resources.path('mesonbuild.scripts', 'python_info.py') - with importlib.resources.path('mesonbuild.scripts', 'python_info.py') as f: + with context_mgr as f: cmd = self.get_command() + [str(f)] p, stdout, stderr = mesonlib.Popen_safe(cmd) diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index ac74e13dc5b7..baeb85936614 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import annotations -import copy, json, os, shutil +import copy, json, os, shutil, sys import typing as T from . import ExtensionModule, ModuleInfo @@ -329,7 +329,10 @@ def should_append(f, isdir: bool = False): import importlib.resources pycompile = os.path.join(self.interpreter.environment.get_scratch_dir(), 'pycompile.py') with open(pycompile, 'wb') as f: - f.write(importlib.resources.read_binary('mesonbuild.scripts', 'pycompile.py')) + if sys.version_info >= (3, 13): + f.write(importlib.resources.files('mesonbuild.scripts').joinpath('pycompile.py').read_bytes()) + else: + f.write(importlib.resources.read_binary('mesonbuild.scripts', 'pycompile.py')) for i in self.installations.values(): if isinstance(i, PythonExternalProgram) and i.run_bytecompile[i.info['version']]: