python dependency: use exceptions to pass failure state around

Instead of returning Optional, a state that is only possible during
`__init__` and which prevents mypy from knowing what it is safe to
assume it will get.
This commit is contained in:
Eli Schwartz 2023-12-25 02:32:14 -05:00
parent 546fe0f92b
commit 6e1556028c
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
1 changed files with 8 additions and 8 deletions

View File

@ -8,7 +8,7 @@ from pathlib import Path
import typing as T import typing as T
from .. import mesonlib, mlog from .. import mesonlib, mlog
from .base import process_method_kw, DependencyMethods, DependencyTypeName, ExternalDependency, SystemDependency from .base import process_method_kw, DependencyException, DependencyMethods, DependencyTypeName, ExternalDependency, SystemDependency
from .configtool import ConfigToolDependency from .configtool import ConfigToolDependency
from .detect import packages from .detect import packages
from .factory import DependencyFactory from .factory import DependencyFactory
@ -245,7 +245,7 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase):
self.link_args = largs self.link_args = largs
self.is_found = True self.is_found = True
def get_windows_python_arch(self) -> T.Optional[str]: def get_windows_python_arch(self) -> str:
if self.platform.startswith('mingw'): if self.platform.startswith('mingw'):
if 'x86_64' in self.platform: if 'x86_64' in self.platform:
return 'x86_64' return 'x86_64'
@ -254,16 +254,14 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase):
elif 'aarch64' in self.platform: elif 'aarch64' in self.platform:
return 'aarch64' return 'aarch64'
else: else:
mlog.log(f'MinGW Python built with unknown platform {self.platform!r}, please file a bug') raise DependencyException(f'MinGW Python built with unknown platform {self.platform!r}, please file a bug')
return None
elif self.platform == 'win32': elif self.platform == 'win32':
return 'x86' return 'x86'
elif self.platform in {'win64', 'win-amd64'}: elif self.platform in {'win64', 'win-amd64'}:
return 'x86_64' return 'x86_64'
elif self.platform in {'win-arm64'}: elif self.platform in {'win-arm64'}:
return 'aarch64' return 'aarch64'
mlog.log(f'Unknown Windows Python platform {self.platform!r}') raise DependencyException('Unknown Windows Python platform {self.platform!r}')
return None
def get_windows_link_args(self, limited_api: bool) -> T.Optional[T.List[str]]: def get_windows_link_args(self, limited_api: bool) -> T.Optional[T.List[str]]:
if self.platform.startswith('win'): if self.platform.startswith('win'):
@ -330,8 +328,10 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase):
Find python3 libraries on Windows and also verify that the arch matches Find python3 libraries on Windows and also verify that the arch matches
what we are building for. what we are building for.
''' '''
pyarch = self.get_windows_python_arch() try:
if pyarch is None: pyarch = self.get_windows_python_arch()
except DependencyException as e:
mlog.log(str(e))
self.is_found = False self.is_found = False
return return
arch = detect_cpu_family(env.coredata.compilers.host) arch = detect_cpu_family(env.coredata.compilers.host)