summaryrefslogtreecommitdiff
path: root/mesonbuild/interpreter
diff options
context:
space:
mode:
authorJouke Witteveen <j.witteveen@gmail.com>2025-07-24 14:17:55 +0200
committerJussi Pakkanen <jussi.pakkanen@mailbox.org>2025-11-28 12:41:01 +0200
commit0fb9c5a6839205cc32c2632dd20f723f00dc165b (patch)
tree0dab116976dbf400ed9f95860ac683435466d321 /mesonbuild/interpreter
parent5e399434f2c3bb791f0c16f01fa85680c3f90fb7 (diff)
downloadmeson-0fb9c5a6839205cc32c2632dd20f723f00dc165b.tar.gz
interpreter: Add a slice() method to arrays
This can come in handy for instance when a custom target creates both headers and sources. Slicing the output of a `to_list()` call provides convenient access to just the headers or just the sources.
Diffstat (limited to 'mesonbuild/interpreter')
-rw-r--r--mesonbuild/interpreter/primitives/array.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/mesonbuild/interpreter/primitives/array.py b/mesonbuild/interpreter/primitives/array.py
index d0a244179..86c1ce207 100644
--- a/mesonbuild/interpreter/primitives/array.py
+++ b/mesonbuild/interpreter/primitives/array.py
@@ -7,12 +7,14 @@ import typing as T
from ...interpreterbase import (
InterpreterObject,
IterableObject,
+ KwargInfo,
MesonOperator,
ObjectHolder,
typed_operator,
noKwargs,
noPosargs,
noArgsFlattening,
+ typed_kwargs,
typed_pos_args,
FeatureNew,
@@ -80,6 +82,18 @@ class ArrayHolder(ObjectHolder[T.List[TYPE_var]], IterableObject):
return args[1]
return self.held_object[index]
+ @FeatureNew('array.slice', '1.10.0')
+ @typed_kwargs('array.slice', KwargInfo('step', int, default=1))
+ @typed_pos_args('array.slice', optargs=[int, int])
+ @InterpreterObject.method('slice')
+ def slice_method(self, args: T.Tuple[T.Optional[int], T.Optional[int]], kwargs: T.Dict[str, int]) -> TYPE_var:
+ start, stop = args
+ if start is not None and stop is None:
+ raise InvalidArguments('Providing only one positional slice argument is ambiguous.')
+ if kwargs['step'] == 0:
+ raise InvalidArguments('Slice step cannot be zero.')
+ return self.held_object[start:stop:kwargs['step']]
+
@typed_operator(MesonOperator.PLUS, object)
@InterpreterObject.operator(MesonOperator.PLUS)
def op_plus(self, other: TYPE_var) -> T.List[TYPE_var]: