Untitled diff
35 lines
def _ensure_stringlike(self, option, what, default=None):
def _ensure_stringlike(self, option, what, default=None):
val = getattr(self, option)
val = getattr(self, option)
if val is None:
if val is None:
setattr(self, option, default)
setattr(self, option, default)
return default
return default
elif not isinstance(val, str):
elif not isinstance(val, str):
raise DistutilsOptionError(f"'{option}' must be a {what} (got `{val}`)")
raise DistutilsOptionError("'%s' must be a %s (got `%s`)"
% (option, what, val))
return val
return val
def ensure_string_list(self, option: str) -> None:
def ensure_string_list(self, option):
r"""Ensure that 'option' is a list of strings. If 'option' is
r"""Ensure that 'option' is a list of strings. If 'option' is
currently a string, we split it either on /,\s*/ or /\s+/, so
currently a string, we split it either on /,\s*/ or /\s+/, so
"foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
"foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
["foo", "bar", "baz"].
["foo", "bar", "baz"].
..
TODO: This method seems to be similar to the one in ``distutils.cmd``
Probably it is just here for backward compatibility with old Python versions?
:meta private:
"""
"""
val = getattr(self, option)
val = getattr(self, option)
if val is None:
if val is None:
return
return
elif isinstance(val, str):
elif isinstance(val, str):
setattr(self, option, re.split(r',\s*|\s+', val))
setattr(self, option, re.split(r',\s*|\s+', val))
else:
else:
if isinstance(val, list):
if isinstance(val, list):
ok = all(isinstance(v, str) for v in val)
ok = all(isinstance(v, str) for v in val)
else:
else:
ok = False
ok = False
if not ok:
if not ok:
raise DistutilsOptionError(
raise DistutilsOptionError(
f"'{option}' must be a list of strings (got {val!r})"
"'%s' must be a list of strings (got %r)"
)
% (option, val))