modules/python: simplify a number of interfaces

Including not calling back into `Interpreter.func_*`, which is not a
good idea both from a type saftey and perforamance point of view.
Instead there's now a shared _impl method
pull/8992/head
Dylan Baker 3 years ago
parent 9eec2a131b
commit a881e849b5
  1. 9
      mesonbuild/interpreter/interpreter.py
  2. 60
      mesonbuild/modules/python.py

@ -2032,9 +2032,12 @@ This will become a hard error in the future.''' % kwargs['input'], location=self
'"rename" and "sources" argument lists must be the same length if "rename" is given. ' '"rename" and "sources" argument lists must be the same length if "rename" is given. '
f'Rename has {len(rename)} elements and sources has {len(sources)}.') f'Rename has {len(rename)} elements and sources has {len(sources)}.')
data = build.Data( return self.install_data_impl(sources, kwargs['install_dir'], kwargs['install_mode'], rename)
sources, kwargs['install_dir'], kwargs['install_mode'],
self.subproject, rename) def install_data_impl(self, sources: T.List[mesonlib.File], install_dir: str,
install_mode: FileMode, rename: T.Optional[str]) -> build.Data:
"""Just the implementation with no validation."""
data = build.Data(sources, install_dir, install_mode, self.subproject, rename)
self.build.data.append(data) self.build.data.append(data)
return data return data

@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from os import name
from pathlib import Path from pathlib import Path
import os import os
import json import json
@ -28,7 +27,7 @@ from ..environment import detect_cpu_family
from ..interpreter import ExternalProgramHolder, extract_required_kwarg, permitted_dependency_kwargs from ..interpreter import ExternalProgramHolder, extract_required_kwarg, permitted_dependency_kwargs
from ..interpreterbase import ( from ..interpreterbase import (
noPosargs, noKwargs, permittedKwargs, noPosargs, noKwargs, permittedKwargs,
InvalidArguments, typed_pos_args, InvalidArguments, typed_pos_args, typed_kwargs, KwargInfo,
FeatureNew, FeatureNewKwargs, disablerIfNotFound FeatureNew, FeatureNewKwargs, disablerIfNotFound
) )
from ..mesonlib import MachineChoice, MesonException from ..mesonlib import MachineChoice, MesonException
@ -331,6 +330,17 @@ class PythonExternalProgram(ExternalProgram):
} }
_PURE_KW = KwargInfo('pure', bool, default=True)
_SUBDIR_KW = KwargInfo('subdir', str, default='')
if T.TYPE_CHECKING:
class PyInstallKw(TypedDict):
pure: bool
subdir: str
class PythonInstallation(ExternalProgramHolder): class PythonInstallation(ExternalProgramHolder):
def __init__(self, python: 'PythonExternalProgram', interpreter: 'Interpreter'): def __init__(self, python: 'PythonExternalProgram', interpreter: 'Interpreter'):
ExternalProgramHolder.__init__(self, python, interpreter) ExternalProgramHolder.__init__(self, python, interpreter)
@ -409,40 +419,24 @@ class PythonInstallation(ExternalProgramHolder):
raise mesonlib.MesonException('Python dependency not found') raise mesonlib.MesonException('Python dependency not found')
return dep return dep
@permittedKwargs(['pure', 'subdir']) @typed_pos_args('install_data', varargs=(str, mesonlib.File))
def install_sources_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') -> 'Data': @typed_kwargs('python_installation.install_sources', _PURE_KW, _SUBDIR_KW)
pure = kwargs.pop('pure', True) def install_sources_method(self, args: T.Tuple[T.List[T.Union[str, mesonlib.File]]],
if not isinstance(pure, bool): kwargs: 'PyInstallKw') -> 'Data':
raise InvalidArguments('"pure" argument must be a boolean.') return self.interpreter.install_data_impl(
self.interpreter.source_strings_to_files(args[0]),
subdir = kwargs.pop('subdir', '') self._get_install_dir_impl(kwargs['pure'], kwargs['subdir']),
if not isinstance(subdir, str): mesonlib.FileMode(),
raise InvalidArguments('"subdir" argument must be a string.') None)
if pure:
kwargs['install_dir'] = os.path.join(self.purelib_install_path, subdir)
else:
kwargs['install_dir'] = os.path.join(self.platlib_install_path, subdir)
return self.interpreter.func_install_data(None, args, kwargs)
@noPosargs @noPosargs
@permittedKwargs(['pure', 'subdir']) @typed_kwargs('python_installation.install_dir', _PURE_KW, _SUBDIR_KW)
def get_install_dir_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') -> str: def get_install_dir_method(self, args: T.List['TYPE_var'], kwargs: 'PyInstallKw') -> str:
pure = kwargs.pop('pure', True) return self._get_install_dir_impl(kwargs['pure'], kwargs['subdir'])
if not isinstance(pure, bool):
raise InvalidArguments('"pure" argument must be a boolean.')
subdir = kwargs.pop('subdir', '')
if not isinstance(subdir, str):
raise InvalidArguments('"subdir" argument must be a string.')
if pure:
res = os.path.join(self.purelib_install_path, subdir)
else:
res = os.path.join(self.platlib_install_path, subdir)
return res def _get_install_dir_impl(self, pure: bool, subdir: str) -> str:
return os.path.join(
self.purelib_install_path if pure else self.platlib_install_path, subdir)
@noPosargs @noPosargs
@noKwargs @noKwargs

Loading…
Cancel
Save