3232import warnings
3333
3434from typing import (
35- Any , ClassVar , DefaultDict , Dict , List , Optional , Sequence , Set , TextIO ,
36- Tuple , Type , Union
35+ Any , Callable , ClassVar , DefaultDict , Dict , List , Optional , Sequence , Set ,
36+ TextIO , Tuple , Type , TypeVar , Union
3737)
3838
3939
4949import mesonpy ._wheelfile
5050
5151from mesonpy ._compat import (
52- Collection , Iterator , Literal , Mapping , Path , typing_get_args
52+ Collection , Iterator , Literal , Mapping , ParamSpec , Path , typing_get_args
5353)
5454
5555
6767
6868
6969_COLORS = {
70+ 'red' : '\33 [31m' ,
7071 'cyan' : '\33 [36m' ,
7172 'yellow' : '\33 [93m' ,
7273 'light_blue' : '\33 [94m' ,
@@ -137,11 +138,16 @@ def _setup_cli() -> None:
137138 colorama .init () # fix colors on windows
138139
139140
140- class ConfigError (Exception ):
141+ class Error (RuntimeError ):
142+ def __str__ (self ) -> str :
143+ return str (self .args [0 ])
144+
145+
146+ class ConfigError (Error ):
141147 """Error in the backend configuration."""
142148
143149
144- class MesonBuilderError (Exception ):
150+ class MesonBuilderError (Error ):
145151 """Error when building the Meson package."""
146152
147153
@@ -1047,12 +1053,29 @@ def _env_ninja_command(*, version: str = _NINJA_REQUIRED_VERSION) -> Optional[pa
10471053 return None
10481054
10491055
1056+ P = ParamSpec ('P' )
1057+ T = TypeVar ('T' )
1058+
1059+
1060+ def _pyproject_hook (func : Callable [P , T ]) -> Callable [P , T ]:
1061+ @functools .wraps (func )
1062+ def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> T :
1063+ try :
1064+ return func (* args , ** kwargs )
1065+ except Error as exc :
1066+ print ('{red}meson-python: error:{reset} {msg}' .format (msg = str (exc ), ** _STYLES ))
1067+ raise SystemExit (1 )
1068+ return wrapper
1069+
1070+
1071+ @_pyproject_hook
10501072def get_requires_for_build_sdist (
10511073 config_settings : Optional [Dict [str , str ]] = None ,
10521074) -> List [str ]:
10531075 return [_depstr .ninja ] if _env_ninja_command () is None else []
10541076
10551077
1078+ @_pyproject_hook
10561079def build_sdist (
10571080 sdist_directory : str ,
10581081 config_settings : Optional [Dict [Any , Any ]] = None ,
@@ -1064,6 +1087,7 @@ def build_sdist(
10641087 return project .sdist (out ).name
10651088
10661089
1090+ @_pyproject_hook
10671091def get_requires_for_build_wheel (
10681092 config_settings : Optional [Dict [str , str ]] = None ,
10691093) -> List [str ]:
@@ -1089,6 +1113,7 @@ def get_requires_for_build_wheel(
10891113 return dependencies
10901114
10911115
1116+ @_pyproject_hook
10921117def build_wheel (
10931118 wheel_directory : str ,
10941119 config_settings : Optional [Dict [Any , Any ]] = None ,
0 commit comments