refactor logic for parsing dependency variables into type_checking module

We will momentarily use this to implement typed_kwargs, but not for all
usage sites.
pull/10495/head
Eli Schwartz 3 years ago
parent b13a95c301
commit ca52dac38b
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 22
      mesonbuild/interpreter/interpreter.py
  2. 31
      mesonbuild/interpreter/type_checking.py
  3. 2
      test cases/failing/46 pkgconfig variables zero length/test.json
  4. 2
      test cases/failing/47 pkgconfig variables zero length value/test.json
  5. 2
      test cases/failing/48 pkgconfig variables not key value/test.json

@ -77,6 +77,8 @@ from .type_checking import (
REQUIRED_KW,
NoneType,
in_set_validator,
variables_validator,
variables_convertor,
env_convertor_with_method
)
from . import primitives as P_OBJ
@ -633,23 +635,19 @@ class Interpreter(InterpreterBase, HoldableObject):
if dict_new and variables:
FeatureNew.single_use(f'{argname} as dictionary', '0.56.0', self.subproject, location=self.current_node)
else:
varlist = mesonlib.stringlistify(variables)
variables = mesonlib.stringlistify(variables)
if list_new:
FeatureNew.single_use(f'{argname} as list of strings', '0.56.0', self.subproject, location=self.current_node)
variables = {}
for v in varlist:
try:
(key, value) = v.split('=', 1)
except ValueError:
raise InterpreterException(f'Variable {v!r} must have a value separated by equals sign.')
variables[key.strip()] = value.strip()
invalid_msg = variables_validator(variables)
if invalid_msg is not None:
raise InterpreterException(invalid_msg)
variables = variables_convertor(variables)
for k, v in variables.items():
if not k or not v:
raise InterpreterException('Empty variable name or value')
if any(c.isspace() for c in k):
raise InterpreterException(f'Invalid whitespace in variable name "{k}"')
if not isinstance(v, str):
raise InterpreterException('variables values must be strings.')
return variables
@FeatureNewKwargs('declare_dependency', '0.46.0', ['link_whole'])

@ -107,6 +107,37 @@ def _lower_strlist(input: T.List[str]) -> T.List[str]:
return [i.lower() for i in input]
def variables_validator(contents: T.Union[T.List[str], T.Dict[str, str]]) -> T.Optional[str]:
if isinstance(contents, dict):
variables = contents
else:
variables = {}
for v in contents:
try:
key, val = v.split('=', 1)
except ValueError:
return f'variable {v!r} must have a value separated by equals sign.'
variables[key.strip()] = val.strip()
for k, v in variables.items():
if not k:
return 'empty variable name'
if not v:
return 'empty variable value'
if any(c.isspace() for c in k):
return f'invalid whitespace in variable name {k!r}'
return None
def variables_convertor(contents: T.Union[T.List[str], T.Dict[str, str]]) -> T.Dict[str, str]:
if isinstance(contents, dict):
return contents
variables = {}
for v in contents:
key, val = v.split('=', 1)
variables[key.strip()] = val.strip()
return variables
NATIVE_KW = KwargInfo(
'native', bool,
default=False,

@ -1,7 +1,7 @@
{
"stdout": [
{
"line": "test cases/failing/46 pkgconfig variables zero length/meson.build:8:5: ERROR: Empty variable name or value"
"line": "test cases/failing/46 pkgconfig variables zero length/meson.build:8:5: ERROR: empty variable name"
}
]
}

@ -1,7 +1,7 @@
{
"stdout": [
{
"line": "test cases/failing/47 pkgconfig variables zero length value/meson.build:8:5: ERROR: Empty variable name or value"
"line": "test cases/failing/47 pkgconfig variables zero length value/meson.build:8:5: ERROR: empty variable value"
}
]
}

@ -1,7 +1,7 @@
{
"stdout": [
{
"line": "test cases/failing/48 pkgconfig variables not key value/meson.build:8:5: ERROR: Variable 'this_should_be_key_value' must have a value separated by equals sign."
"line": "test cases/failing/48 pkgconfig variables not key value/meson.build:8:5: ERROR: variable 'this_should_be_key_value' must have a value separated by equals sign."
}
]
}

Loading…
Cancel
Save