Skip to content

Commit

Permalink
machinefile: ensure that arrays are single deep arrays for strings
Browse files Browse the repository at this point in the history
We don't actually allow anything except elementry types (`str`, `bool`,
`int`, `array[str]`) here, so we can tighten the typing. This in turn
helps us to simplify the typing in environment.py
  • Loading branch information
dcbaker committed Feb 12, 2025
1 parent 392e4bc commit 27d3d0e
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions mesonbuild/machinefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
from .mesonlib import MesonException

if T.TYPE_CHECKING:
from typing_extensions import TypeAlias

from .coredata import StrOrBytesPath

SectionT: TypeAlias = T.Union[str, int, bool, T.List[str], T.List['SectionT']]

from .options import ElementaryOptionValues

class CmdLineFileParser(configparser.ConfigParser):
def __init__(self) -> None:
Expand All @@ -36,8 +32,8 @@ def optionxform(self, optionstr: str) -> str:
class MachineFileParser():
def __init__(self, filenames: T.List[str], sourcedir: str) -> None:
self.parser = CmdLineFileParser()
self.constants: T.Dict[str, SectionT] = {'True': True, 'False': False}
self.sections: T.Dict[str, T.Dict[str, SectionT]] = {}
self.constants: T.Dict[str, ElementaryOptionValues] = {'True': True, 'False': False}
self.sections: T.Dict[str, T.Dict[str, ElementaryOptionValues]] = {}

for fname in filenames:
try:
Expand All @@ -62,9 +58,9 @@ def __init__(self, filenames: T.List[str], sourcedir: str) -> None:
continue
self.sections[s] = self._parse_section(s)

def _parse_section(self, s: str) -> T.Dict[str, SectionT]:
def _parse_section(self, s: str) -> T.Dict[str, ElementaryOptionValues]:
self.scope = self.constants.copy()
section: T.Dict[str, SectionT] = {}
section: T.Dict[str, ElementaryOptionValues] = {}
for entry, value in self.parser.items(s):
if ' ' in entry or '\t' in entry or "'" in entry or '"' in entry:
raise MesonException(f'Malformed variable name {entry!r} in machine file.')
Expand All @@ -83,7 +79,7 @@ def _parse_section(self, s: str) -> T.Dict[str, SectionT]:
self.scope[entry] = res
return section

def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
def _evaluate_statement(self, node: mparser.BaseNode) -> ElementaryOptionValues:
if isinstance(node, (mparser.StringNode)):
return node.value
elif isinstance(node, mparser.BooleanNode):
Expand All @@ -93,7 +89,9 @@ def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
elif isinstance(node, mparser.ParenthesizedNode):
return self._evaluate_statement(node.inner)
elif isinstance(node, mparser.ArrayNode):
return [self._evaluate_statement(arg) for arg in node.args.arguments]
a = [self._evaluate_statement(arg) for arg in node.args.arguments]
assert all(isinstance(s, str) for s in a), 'for mypy'
return T.cast('T.List[str]', a)
elif isinstance(node, mparser.IdNode):
return self.scope[node.value]
elif isinstance(node, mparser.ArithmeticNode):
Expand All @@ -109,7 +107,7 @@ def _evaluate_statement(self, node: mparser.BaseNode) -> SectionT:
return os.path.join(l, r)
raise MesonException('Unsupported node type')

def parse_machine_files(filenames: T.List[str], sourcedir: str) -> T.Dict[str, T.Dict[str, SectionT]]:
def parse_machine_files(filenames: T.List[str], sourcedir: str) -> T.Dict[str, T.Dict[str, ElementaryOptionValues]]:
parser = MachineFileParser(filenames, sourcedir)
return parser.sections

Expand Down

0 comments on commit 27d3d0e

Please sign in to comment.