Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using the removed importlib.resources.path on Python >=3.13 #12402

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not >= (3, 9)? Why use a deprecated function when the replacement is available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't seem to matter, so just trying to make the minimal change here. If it did matter (as it has before with importlib and traversables, it's complex machinery), I'd rather find out through 3.13 early adopters.

It's also possible that something still changes before 3.13.rc1, and this would allow dropping the if-else.

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
11 changes: 7 additions & 4 deletions mesonbuild/mesondata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import importlib.resources
from pathlib import PurePosixPath, Path
import sys
import typing as T

if T.TYPE_CHECKING:
Expand All @@ -27,10 +28,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
Loading