Skip to content

Commit 939cfa6

Browse files
xclaessedcbaker
authored andcommitted
cargo: Move toml implementation into its own module
1 parent dcfb6c8 commit 939cfa6

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

mesonbuild/cargo/interpreter.py

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@
1111

1212
from __future__ import annotations
1313
import dataclasses
14-
import importlib
15-
import json
1614
import os
17-
import shutil
1815
import collections
1916
import urllib.parse
2017
import itertools
2118
import typing as T
2219

2320
from . import builder, version, cfg
24-
from ..mesonlib import MesonException, Popen_safe, MachineChoice
21+
from .toml import load_toml, TomlImplementationMissing
22+
from ..mesonlib import MesonException, MachineChoice
2523
from .. import coredata, mlog
2624
from ..wrap.wrap import PackageDefinition
2725

2826
if T.TYPE_CHECKING:
29-
from types import ModuleType
30-
3127
from typing_extensions import Protocol, Self
3228

3329
from . import manifest
@@ -45,25 +41,6 @@ class DataclassInstance(Protocol):
4541
manifest.FixedBuildTarget)
4642

4743

48-
# tomllib is present in python 3.11, before that it is a pypi module called tomli,
49-
# we try to import tomllib, then tomli,
50-
# TODO: add a fallback to toml2json?
51-
tomllib: T.Optional[ModuleType] = None
52-
toml2json: T.Optional[str] = None
53-
for t in ['tomllib', 'tomli']:
54-
try:
55-
tomllib = importlib.import_module(t)
56-
break
57-
except ImportError:
58-
pass
59-
else:
60-
# TODO: it would be better to use an Executable here, which could be looked
61-
# up in the cross file or provided by a wrap. However, that will have to be
62-
# passed in externally, since we don't have (and I don't think we should),
63-
# have access to the `Environment` for that in this module.
64-
toml2json = shutil.which('toml2json')
65-
66-
6744
_EXTRA_KEYS_WARNING = (
6845
"This may (unlikely) be an error in the cargo manifest, or may be a missing "
6946
"implementation in Meson. If this issue can be reproduced with the latest "
@@ -72,29 +49,6 @@ class DataclassInstance(Protocol):
7249
"version that is generating this warning if possible."
7350
)
7451

75-
class TomlImplementationMissing(MesonException):
76-
pass
77-
78-
79-
def load_toml(filename: str) -> T.Dict[object, object]:
80-
if tomllib:
81-
with open(filename, 'rb') as f:
82-
raw = tomllib.load(f)
83-
else:
84-
if toml2json is None:
85-
raise TomlImplementationMissing('Could not find an implementation of tomllib, nor toml2json')
86-
87-
p, out, err = Popen_safe([toml2json, filename])
88-
if p.returncode != 0:
89-
raise MesonException('toml2json failed to decode output\n', err)
90-
91-
raw = json.loads(out)
92-
93-
if not isinstance(raw, dict):
94-
raise MesonException("Cargo.toml isn't a dictionary? How did that happen?")
95-
96-
return raw
97-
9852

9953
def fixup_meson_varname(name: str) -> str:
10054
"""Fixup a meson variable name

mesonbuild/cargo/toml.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from __future__ import annotations
2+
3+
import importlib
4+
import shutil
5+
import json
6+
import typing as T
7+
8+
from ..mesonlib import MesonException, Popen_safe
9+
if T.TYPE_CHECKING:
10+
from types import ModuleType
11+
12+
13+
# tomllib is present in python 3.11, before that it is a pypi module called tomli,
14+
# we try to import tomllib, then tomli,
15+
tomllib: T.Optional[ModuleType] = None
16+
toml2json: T.Optional[str] = None
17+
for t in ['tomllib', 'tomli']:
18+
try:
19+
tomllib = importlib.import_module(t)
20+
break
21+
except ImportError:
22+
pass
23+
else:
24+
# TODO: it would be better to use an Executable here, which could be looked
25+
# up in the cross file or provided by a wrap. However, that will have to be
26+
# passed in externally, since we don't have (and I don't think we should),
27+
# have access to the `Environment` for that in this module.
28+
toml2json = shutil.which('toml2json')
29+
30+
class TomlImplementationMissing(MesonException):
31+
pass
32+
33+
34+
def load_toml(filename: str) -> T.Dict[str, object]:
35+
if tomllib:
36+
with open(filename, 'rb') as f:
37+
raw = tomllib.load(f)
38+
else:
39+
if toml2json is None:
40+
raise TomlImplementationMissing('Could not find an implementation of tomllib, nor toml2json')
41+
42+
p, out, err = Popen_safe([toml2json, filename])
43+
if p.returncode != 0:
44+
raise MesonException('toml2json failed to decode output\n', err)
45+
46+
raw = json.loads(out)
47+
48+
# tomllib.load() returns T.Dict[str, T.Any] but not other implementations.
49+
return T.cast('T.Dict[str, object]', raw)

0 commit comments

Comments
 (0)