Skip to content

Commit

Permalink
Stop using removed importlib.resources functions on Python >=3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
rgommers committed Oct 20, 2023
1 parent 450b3db commit 023d0b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 7 additions & 1 deletion mesonbuild/dependencies/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import functools, json, os, textwrap
from pathlib import Path
import sys
import typing as T

from .. import mesonlib, mlog
Expand Down Expand Up @@ -112,8 +113,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)]
env = os.environ.copy()
env['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
Expand Down
10 changes: 6 additions & 4 deletions mesonbuild/mesondata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ def __init__(self, path: str) -> None:

def write_once(self, path: Path) -> None:
if not path.exists():
data = importlib.resources.read_text( # [ignore encoding] it's on the next lines, Mr. Lint
('mesonbuild' / self.path.parent).as_posix().replace('/', '.'),
self.path.name,
encoding='utf-8')

package = ('mesonbuild' / self.path.parent).as_posix().replace('/', '.')
if sys.version_info >= (3, 13):
data = importlib.resources.files(package).joinpath(self.path.name).read_text(encoding='utf-8')
else:
data = importlib.resources.read_text(package, self.path.name, encoding='utf-8')
path.write_text(data, encoding='utf-8')

def write_to_private(self, env: 'Environment') -> Path:
Expand Down
7 changes: 5 additions & 2 deletions mesonbuild/modules/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
from __future__ import annotations

import copy, json, os, shutil, re
import copy, json, os, shutil, re, sys
import typing as T

from . import ExtensionModule, ModuleInfo
Expand Down Expand Up @@ -407,7 +407,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']]:
Expand Down

0 comments on commit 023d0b0

Please sign in to comment.