UserArrayOption: Small cleanup in value parsing

It is nicer to early raise exception if the value from meson_options.txt
is not a string in "[]" format than duplicating the parser code for both
cases.

Also it was checking for duplicated items only in the user_input case,
but we should also check for dups in the default value from
meson_options.txt.
pull/3705/head
Xavier Claessens 7 years ago committed by Nirbheek Chauhan
parent 64bfc6cf7b
commit 58a9555ddf
  1. 25
      mesonbuild/coredata.py

@ -148,15 +148,10 @@ class UserArrayOption(UserOption):
# options). Users can put their input in as a comma separated # options). Users can put their input in as a comma separated
# string, but for defining options in meson_options.txt the format # string, but for defining options in meson_options.txt the format
# should match that of a combo # should match that of a combo
if not user_input: if not user_input and isinstance(value, str) and not value.startswith('['):
if isinstance(value, str): raise MesonException('Value does not define an array: ' + value)
if not value.startswith('['):
raise MesonException('Valuestring does not define an array: ' + value) if isinstance(value, str):
newvalue = ast.literal_eval(value)
else:
newvalue = value
else:
assert isinstance(value, str)
if value.startswith('['): if value.startswith('['):
newvalue = ast.literal_eval(value) newvalue = ast.literal_eval(value)
else: else:
@ -164,11 +159,15 @@ class UserArrayOption(UserOption):
newvalue = shlex.split(value) newvalue = shlex.split(value)
else: else:
newvalue = [v.strip() for v in value.split(',')] newvalue = [v.strip() for v in value.split(',')]
if len(set(newvalue)) != len(newvalue): elif isinstance(value, list):
mlog.log(mlog.red('DEPRECATION:'), '''Duplicated values in an array type is deprecated. newvalue = value
This will become a hard error in the future.''') else:
if not isinstance(newvalue, list):
raise MesonException('"{0}" should be a string array, but it is not'.format(str(newvalue))) raise MesonException('"{0}" should be a string array, but it is not'.format(str(newvalue)))
if len(set(newvalue)) != len(newvalue):
msg = 'Duplicated values in array option "%s" is deprecated. ' \
'This will become a hard error in the future.' % (self.name)
mlog.log(mlog.red('DEPRECATION:'), msg)
for i in newvalue: for i in newvalue:
if not isinstance(i, str): if not isinstance(i, str):
raise MesonException('String array element "{0}" is not a string.'.format(str(newvalue))) raise MesonException('String array element "{0}" is not a string.'.format(str(newvalue)))

Loading…
Cancel
Save