diff options
-rw-r--r-- | docs/markdown/Python-module.md | 40 | ||||
-rw-r--r-- | mesonbuild/modules/python.py | 52 |
2 files changed, 72 insertions, 20 deletions
diff --git a/docs/markdown/Python-module.md b/docs/markdown/Python-module.md index cad74c9..51721f0 100644 --- a/docs/markdown/Python-module.md +++ b/docs/markdown/Python-module.md @@ -142,7 +142,7 @@ This function expects no arguments or keyword arguments. #### `get_path()` ``` meson -string py_installation.get_path(path_name) +string py_installation.get_path(path_name, fallback) ``` Get a path as defined by the `sysconfig` module. @@ -153,15 +153,28 @@ For example: purelib = py_installation.get_path('purelib') ``` -This function accepts a single argument, `path_name`, which is expected to -be a non-empty string. +This function requires at least one argument, `path_name`, +which is expected to be a non-empty string. + +If `fallback` is specified, it will be returned if no path +with the given name exists. Otherwise, attempting to read +a non-existing path will cause a fatal error. **Returns**: A string +#### `has_path()` + +``` meson + bool py_installation.has_path(path_name) +``` + +**Returns**: true if a path named `path_name` can be retrieved with +[][`get_path()`], false otherwise. + #### `get_variable()` ``` meson -string py_installation.get_variable(variable_name) +string py_installation.get_variable(variable_name, fallback) ``` Get a variable as defined by the `sysconfig` module. @@ -169,14 +182,27 @@ Get a variable as defined by the `sysconfig` module. For example: ``` meson -py_bindir = py_installation.get_variable('BINDIR') +py_bindir = py_installation.get_variable('BINDIR', '') ``` -This function accepts a single argument, `variable_name`, which is expected to -be a non-empty string. +This function requires at least one argument, `variable_name`, +which is expected to be a non-empty string. + +If `fallback` is specified, it will be returned if no variable +with the given name exists. Otherwise, attempting to read +a non-existing variable will cause a fatal error. **Returns**: A string +#### `has_variable()` + +``` meson + bool py_installation.has_variable(variable_name) +``` + +**Returns**: true if a variable named `variable_name` can be retrieved with +[][`get_variable()`], false otherwise. + ## `python_dependency` object This [dependency object] subclass will try various methods to obtain the diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py index 16bdfd6..afcae8f 100644 --- a/mesonbuild/modules/python.py +++ b/mesonbuild/modules/python.py @@ -313,32 +313,58 @@ class PythonInstallation(ExternalProgramHolder, InterpreterObject): return ModuleReturnValue(True, []) @noKwargs - def get_path(self, node, args, kwargs): + def has_path(self, node, args, kwargs): if len(args) != 1: - raise InvalidArguments('get_path takes exactly one positional argument.') + raise InvalidArguments('has_path takes exactly one positional argument.') path_name = args[0] if not isinstance(path_name, str): - raise InvalidArguments('get_path argument must be a string.') + raise InvalidArguments('has_path argument must be a string.') + + return ModuleReturnValue(path_name in self.paths, []) - path = self.paths.get(path_name) + @noKwargs + def get_path(self, node, args, kwargs): + if len(args) not in (1, 2): + raise InvalidArguments('get_path must have one or two arguments.') + path_name = args[0] + if not isinstance(path_name, str): + raise InvalidArguments('get_path argument must be a string.') - if path is None: - raise InvalidArguments('{} is not a valid path name'.format(path_name)) + try: + path = self.paths[path_name] + except KeyError: + if len(args) == 2: + path = args[1] + else: + raise InvalidArguments('{} is not a valid path name'.format(path_name)) return ModuleReturnValue(path, []) @noKwargs - def get_variable(self, node, args, kwargs): + def has_variable(self, node, args, kwargs): if len(args) != 1: - raise InvalidArguments('get_variable takes exactly one positional argument.') + raise InvalidArguments('has_variable takes exactly one positional argument.') var_name = args[0] if not isinstance(var_name, str): - raise InvalidArguments('get_variable argument must be a string.') + raise InvalidArguments('has_variable argument must be a string.') + + return ModuleReturnValue(var_name in self.variables, []) - var = self.variables.get(var_name) + @noKwargs + def get_variable(self, node, args, kwargs): + if len(args) not in (1, 2): + raise InvalidArguments('get_variable must have one or two arguments.') + var_name = args[0] + if not isinstance(var_name, str): + raise InvalidArguments('get_variable argument must be a string.') - if var is None: - raise InvalidArguments('{} is not a valid path name'.format(var_name)) + try: + var = self.variables[var_name] + except KeyError: + if len(args) == 2: + var = args[1] + else: + raise InvalidArguments('{} is not a valid variable name'.format(var_name)) return ModuleReturnValue(var, []) @@ -351,7 +377,7 @@ class PythonInstallation(ExternalProgramHolder, InterpreterObject): if method_name in ['extension_module', 'dependency', 'install_sources']: value = fn(self.interpreter, None, args, kwargs) return self.interpreter.holderify(value) - elif method_name in ['get_variable', 'get_path', 'found', 'language_version', 'get_install_dir']: + elif method_name in ['has_variable', 'get_variable', 'has_path', 'get_path', 'found', 'language_version', 'get_install_dir']: value = fn(None, args, kwargs) return self.interpreter.module_method_callback(value) else: |