Skip to content

Commit b74a7ea

Browse files
committed
ENH: nicely report meson-python errors instead of raising exceptions
1 parent 0974f03 commit b74a7ea

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

mesonpy/__init__.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import warnings
3333

3434
from 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

@@ -49,7 +49,7 @@
4949
import mesonpy._wheelfile
5050

5151
from 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

@@ -67,6 +67,7 @@
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
10501072
def 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
10561079
def 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
10671091
def 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
10921117
def build_wheel(
10931118
wheel_directory: str,
10941119
config_settings: Optional[Dict[Any, Any]] = None,

mesonpy/_compat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from typing import Union
1010

1111

12+
if sys.version_info >= (3, 10):
13+
from typing import ParamSpec
14+
else:
15+
from typing_extensions import ParamSpec
16+
1217
if sys.version_info >= (3, 9):
1318
from collections.abc import (
1419
Collection, Iterable, Iterator, Mapping, Sequence
@@ -46,5 +51,6 @@ def is_relative_to(path: pathlib.Path, other: Union[pathlib.Path, str]) -> bool:
4651
'Literal',
4752
'Mapping',
4853
'Path',
54+
'ParamSpec',
4955
'Sequence',
5056
]

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ requires = [
55
'meson>=0.63.3',
66
'pyproject-metadata>=0.6.1',
77
'tomli>=1.0.0; python_version<"3.11"',
8-
'typing-extensions>=3.7.4; python_version<"3.8"',
8+
'typing-extensions>=3.7.4; python_version<"3.10"',
99
]
1010

1111
[project]
@@ -28,7 +28,7 @@ dependencies = [
2828
'meson>=0.63.3',
2929
'pyproject-metadata>=0.6.1', # not a hard dependency, only needed for projects that use PEP 621 metadata
3030
'tomli>=1.0.0; python_version<"3.11"',
31-
'typing-extensions>=3.7.4; python_version<"3.8"',
31+
'typing-extensions>=3.7.4; python_version<"3.10"',
3232
]
3333

3434
dynamic = [

0 commit comments

Comments
 (0)