diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2021-02-06 11:14:25 -0500 |
---|---|---|
committer | Xavier Claessens <xclaesse@gmail.com> | 2021-03-16 20:50:16 -0400 |
commit | a45f939092cc2c60d49040bdb0147758a1403f61 (patch) | |
tree | 76db6837f0ca5cf9a0546f80a8a88dc7a79d24bb /mesonbuild/interpreter.py | |
parent | 6415453f17f85e16d1d2ca3553325b769d0f124d (diff) | |
download | meson-a45f939092cc2c60d49040bdb0147758a1403f61.zip meson-a45f939092cc2c60d49040bdb0147758a1403f61.tar.gz meson-a45f939092cc2c60d49040bdb0147758a1403f61.tar.bz2 |
Add range() function
Fixes: #5026.
Diffstat (limited to 'mesonbuild/interpreter.py')
-rw-r--r-- | mesonbuild/interpreter.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index d554cce..364cf70 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -30,7 +30,7 @@ from .interpreterbase import check_stringlist, flatten, noPosargs, noKwargs, str from .interpreterbase import InterpreterException, InvalidArguments, InvalidCode, SubdirDoneRequest from .interpreterbase import InterpreterObject, MutableInterpreterObject, Disabler, disablerIfNotFound from .interpreterbase import FeatureNew, FeatureDeprecated, FeatureNewKwargs, FeatureDeprecatedKwargs -from .interpreterbase import ObjectHolder, MesonVersionString +from .interpreterbase import ObjectHolder, MesonVersionString, RangeHolder from .interpreterbase import TYPE_var, TYPE_nkwargs from .modules import ModuleReturnValue, ModuleObject, ModuleState from .cmake import CMakeInterpreter @@ -2509,7 +2509,8 @@ class Interpreter(InterpreterBase): 'static_library': self.func_static_lib, 'both_libraries': self.func_both_lib, 'test': self.func_test, - 'vcs_tag': self.func_vcs_tag + 'vcs_tag': self.func_vcs_tag, + 'range': self.func_range, }) if 'MESON_UNIT_TEST' in os.environ: self.funcs.update({'exception': self.func_exception}) @@ -5033,3 +5034,24 @@ This will become a hard error in the future.''', location=self.current_node) raise InvalidCode('Is_disabler takes one argument.') varname = args[0] return isinstance(varname, Disabler) + + @noKwargs + @FeatureNew('range', '0.58.0') + @typed_pos_args('range', int, optargs=[int, int]) + def func_range(self, node, args: T.Tuple[int, T.Optional[int], T.Optional[int]], kwargs: T.Dict[str, T.Any]) -> RangeHolder: + start, stop, step = args + # Just like Python's range, we allow range(stop), range(start, stop), or + # range(start, stop, step) + if stop is None: + stop = start + start = 0 + if step is None: + step = 1 + # This is more strict than Python's range() + if start < 0: + raise InterpreterException('start cannot be negative') + if stop < start: + raise InterpreterException('stop cannot be less than start') + if step < 1: + raise InterpreterException('step must be >=1') + return RangeHolder(start, stop, step) |