diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index f0b650b5e..9a89b0c2a 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py @@ -30,7 +30,7 @@ from ..interpreterbase import noPosargs, noKwargs, permittedKwargs, noArgsFlatte from ..interpreterbase import InterpreterException, InvalidArguments, InvalidCode, SubdirDoneRequest from ..interpreterbase import Disabler, disablerIfNotFound from ..interpreterbase import FeatureNew, FeatureDeprecated, FeatureNewKwargs, FeatureDeprecatedKwargs -from ..interpreterbase import ObjectHolder, RangeHolder +from ..interpreterbase import ObjectHolder from ..interpreterbase.baseobjects import TYPE_nkwargs, TYPE_nvar, TYPE_var, TYPE_kwargs from ..modules import ExtensionModule, ModuleObject, MutableModuleObject, NewExtensionModule, NotFoundExtensionModule from ..cmake import CMakeInterpreter @@ -2761,7 +2761,7 @@ This will become a hard error in the future.''', location=self.current_node) @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: + def func_range(self, node, args: T.Tuple[int, T.Optional[int], T.Optional[int]], kwargs: T.Dict[str, T.Any]) -> P_OBJ.RangeHolder: start, stop, step = args # Just like Python's range, we allow range(stop), range(start, stop), or # range(start, stop, step) @@ -2777,4 +2777,4 @@ This will become a hard error in the future.''', location=self.current_node) raise InterpreterException('stop cannot be less than start') if step < 1: raise InterpreterException('step must be >=1') - return RangeHolder(start, stop, step, subproject=self.subproject) + return P_OBJ.RangeHolder(start, stop, step, subproject=self.subproject) diff --git a/mesonbuild/interpreter/primitives/__init__.py b/mesonbuild/interpreter/primitives/__init__.py index d876f5605..b4fe621ba 100644 --- a/mesonbuild/interpreter/primitives/__init__.py +++ b/mesonbuild/interpreter/primitives/__init__.py @@ -6,6 +6,7 @@ __all__ = [ 'BooleanHolder', 'DictHolder', 'IntegerHolder', + 'RangeHolder', 'StringHolder', 'MesonVersionString', 'MesonVersionStringHolder', @@ -15,4 +16,5 @@ from .array import ArrayHolder from .boolean import BooleanHolder from .dict import DictHolder from .integer import IntegerHolder +from .range import RangeHolder from .string import StringHolder, MesonVersionString, MesonVersionStringHolder diff --git a/mesonbuild/interpreter/primitives/range.py b/mesonbuild/interpreter/primitives/range.py new file mode 100644 index 000000000..9054278b2 --- /dev/null +++ b/mesonbuild/interpreter/primitives/range.py @@ -0,0 +1,34 @@ +# Copyright 2021 The Meson development team +# SPDX-license-identifier: Apache-2.0 + +import typing as T + +from ...interpreterbase import ( + MesonInterpreterObject, + IterableObject, + MesonOperator, + InvalidArguments, +) + +class RangeHolder(MesonInterpreterObject, IterableObject): + def __init__(self, start: int, stop: int, step: int, *, subproject: str) -> None: + super().__init__(subproject=subproject) + self.range = range(start, stop, step) + self.operators.update({ + MesonOperator.INDEX: self.op_index, + }) + + def op_index(self, other: int) -> int: + try: + return self.range[other] + except: + raise InvalidArguments(f'Index {other} out of bounds of range.') + + def iter_tuple_size(self) -> None: + return None + + def iter_self(self) -> T.Iterator[int]: + return iter(self.range) + + def size(self) -> int: + return len(self.range) diff --git a/mesonbuild/interpreterbase/__init__.py b/mesonbuild/interpreterbase/__init__.py index 14462a27d..7c4b1db3b 100644 --- a/mesonbuild/interpreterbase/__init__.py +++ b/mesonbuild/interpreterbase/__init__.py @@ -17,7 +17,6 @@ __all__ = [ 'MesonInterpreterObject', 'ObjectHolder', 'IterableObject', - 'RangeHolder', 'MutableInterpreterObject', 'MesonOperator', @@ -78,7 +77,6 @@ from .baseobjects import ( MesonInterpreterObject, ObjectHolder, IterableObject, - RangeHolder, MutableInterpreterObject, TV_fw_var, diff --git a/mesonbuild/interpreterbase/baseobjects.py b/mesonbuild/interpreterbase/baseobjects.py index 3c7085b8a..7e98b48c7 100644 --- a/mesonbuild/interpreterbase/baseobjects.py +++ b/mesonbuild/interpreterbase/baseobjects.py @@ -176,26 +176,3 @@ class IterableObject(metaclass=ABCMeta): def size(self) -> int: raise MesonBugException(f'size not implemented for {self.__class__.__name__}') - -class RangeHolder(MesonInterpreterObject, IterableObject): - def __init__(self, start: int, stop: int, step: int, *, subproject: str) -> None: - super().__init__(subproject=subproject) - self.range = range(start, stop, step) - self.operators.update({ - MesonOperator.INDEX: self.op_index, - }) - - def op_index(self, other: int) -> int: - try: - return self.range[other] - except: - raise InvalidArguments(f'Index {other} out of bounds of range.') - - def iter_tuple_size(self) -> None: - return None - - def iter_self(self) -> T.Iterator[int]: - return iter(self.range) - - def size(self) -> int: - return len(self.range)