diff options
author | Kyrylo Shpytsya <kshpitsa@gmail.com> | 2018-04-29 21:58:41 +0200 |
---|---|---|
committer | Mathieu Duponchelle <mathieu@centricular.com> | 2018-05-20 21:19:44 +0200 |
commit | 09dc48d941ed69cc8ecaa9133e0c9ddd26c08a7d (patch) | |
tree | b05bd66872db9c06cca4780997424ecb70e5c444 | |
parent | ecb88380827e33161a29c5ca6f3448a6cdd557a5 (diff) | |
download | meson-09dc48d941ed69cc8ecaa9133e0c9ddd26c08a7d.zip meson-09dc48d941ed69cc8ecaa9133e0c9ddd26c08a7d.tar.gz meson-09dc48d941ed69cc8ecaa9133e0c9ddd26c08a7d.tar.bz2 |
Add methods to the dict builtin
Adds "has_key" and "get".
Adapted and updated by Mathieu Duponchelle <mathieu@centricular.com>
-rw-r--r-- | mesonbuild/interpreterbase.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py index edc399d..1e14829 100644 --- a/mesonbuild/interpreterbase.py +++ b/mesonbuild/interpreterbase.py @@ -559,6 +559,8 @@ The result of this is undefined and will become a hard error in a future Meson r return self.int_method_call(obj, method_name, args) if isinstance(obj, list): return self.array_method_call(obj, method_name, args) + if isinstance(obj, dict): + return self.dict_method_call(obj, method_name, args) if isinstance(obj, mesonlib.File): raise InvalidArguments('File object "%s" is not callable.' % obj) if not isinstance(obj, InterpreterObject): @@ -717,6 +719,43 @@ The result of this is undefined and will become a hard error in a future Meson r m = 'Arrays do not have a method called {!r}.' raise InterpreterException(m.format(method_name)) + def dict_method_call(self, obj, method_name, args): + (posargs, kwargs) = self.reduce_arguments(args) + if is_disabled(posargs, kwargs): + return Disabler() + + if method_name in ('has_key', 'get'): + if method_name == 'has_key': + if len(posargs) != 1: + raise InterpreterException('has_key() takes exactly one argument.') + else: + if len(posargs) not in (1, 2): + raise InterpreterException('get() takes one or two arguments.') + + key = posargs[0] + if not isinstance(key, (str)): + raise InvalidArguments('Dictionary key must be a string.') + + has_key = key in obj + + if method_name == 'has_key': + return has_key + + if has_key: + return obj[key] + + if len(posargs) == 2: + return posargs[1] + + raise InterpreterException('Key {!r} is not in the dictionary.'.format(key)) + + if method_name == 'keys': + if len(posargs) != 0: + raise InterpreterException('keys() takes no arguments.') + return list(obj.keys()) + + raise InterpreterException('Dictionaries do not have a method called "%s".' % method_name) + def reduce_arguments(self, args): assert(isinstance(args, mparser.ArgumentNode)) if args.incorrect_order(): |