interpreter: Move RangeHolder out of interpreterbase to interpreter

pull/9366/head
Daniel Mensinger 3 years ago
parent af0587cb49
commit dfec4385a7
  1. 6
      mesonbuild/interpreter/interpreter.py
  2. 2
      mesonbuild/interpreter/primitives/__init__.py
  3. 34
      mesonbuild/interpreter/primitives/range.py
  4. 2
      mesonbuild/interpreterbase/__init__.py
  5. 23
      mesonbuild/interpreterbase/baseobjects.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)

@ -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

@ -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)

@ -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,

@ -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)

Loading…
Cancel
Save