Skip to content

Commit

Permalink
add filename_filter to utils.format_error
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Sep 9, 2024
1 parent ac62746 commit f389e68
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.1.6 (2024-09-09)
1. add filename_filter to utils.format_error

### 1.1.5 (2024-08-29)
1. add `utils.get_hash_int`

Expand Down
15 changes: 15 additions & 0 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
- `index` (`Union[int, slice]`, optional): Specifies which frames to include in the output. By default, it's set to `slice(-3, None, None)`, showing the last three frames. Can be an integer for a single frame or a slice object for a range of frames.
- `filter` (`Optional[Callable]`, optional): A callable that determines whether a given frame should be included. Defaults to `_tb_filter`, which typically filters out frames from "site-packages". If set to `None`, no filtering occurs.
- `template` (`str`, optional): A string template defining how the error message should be formatted. It can include placeholders like `{trace_routes}`, `{error_line}`, and `{error.__class__.__name__}`. The default template provides a concise summary of the error location and type.
- `filename_filter` (`Tuple[str, str]`, optional): A tuple specifying the include and exclude strings of filename. Defaults to `("", "")`, which means no filtering occurs.
- `**kwargs`: Additional keyword arguments to be used within the formatting template.

Returns:
Expand Down Expand Up @@ -519,6 +520,20 @@
... except Exception as e:
... format_error(e, filter=lambda i: '<doctest' in str(i))
"[<doctest>:<module>(4)] version_info_to_nodot(0) >>> TypeError('int' object is not subscriptable)"
>>> try:
... # test with filename_filter[0]
... from pip._internal.utils.compatibility_tags import version_info_to_nodot
... version_info_to_nodot(0)
... except Exception as e:
... format_error(e, filter=None, filename_filter=("site-packages", ""))
'[compatibility_tags.py:version_info_to_nodot(23)] return "".join(map(str, version_info[:2])) >>> TypeError(\'int\' object is not subscriptable)'
>>> try:
... # test with filename_filter[1]
... from pip._internal.utils.compatibility_tags import version_info_to_nodot
... version_info_to_nodot(0)
... except Exception as e:
... format_error(e, filter=None, filename_filter=("", "site-packages"))
'[<doctest>:<module>(4)] return "".join(map(str, version_info[:2])) >>> TypeError(\'int\' object is not subscriptable)'

```

Expand Down
2 changes: 1 addition & 1 deletion morebuiltins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.5"
__version__ = "1.1.6"
__all__ = [
"morebuiltins.utils",
"morebuiltins.date",
Expand Down
28 changes: 26 additions & 2 deletions morebuiltins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ def format_error(
index: Union[int, slice] = slice(-3, None, None),
filter: Optional[Callable] = _tb_filter,
template="[{trace_routes}] {error_line} >>> {error.__class__.__name__}({error!s:.100})",
filename_filter=("", ""),
**kwargs,
) -> str:
r"""Extracts frame information from an exception, with an option to filter out “site-packages” details by default.
Expand All @@ -794,6 +795,7 @@ def format_error(
- `index` (`Union[int, slice]`, optional): Specifies which frames to include in the output. By default, it's set to `slice(-3, None, None)`, showing the last three frames. Can be an integer for a single frame or a slice object for a range of frames.
- `filter` (`Optional[Callable]`, optional): A callable that determines whether a given frame should be included. Defaults to `_tb_filter`, which typically filters out frames from "site-packages". If set to `None`, no filtering occurs.
- `template` (`str`, optional): A string template defining how the error message should be formatted. It can include placeholders like `{trace_routes}`, `{error_line}`, and `{error.__class__.__name__}`. The default template provides a concise summary of the error location and type.
- `filename_filter` (`Tuple[str, str]`, optional): A tuple specifying the include and exclude strings of filename. Defaults to `("", "")`, which means no filtering occurs.
- `**kwargs`: Additional keyword arguments to be used within the formatting template.
Returns:
Expand Down Expand Up @@ -850,19 +852,41 @@ def format_error(
... except Exception as e:
... format_error(e, filter=lambda i: '<doctest' in str(i))
"[<doctest>:<module>(4)] version_info_to_nodot(0) >>> TypeError('int' object is not subscriptable)"
>>> try:
... # test with filename_filter[0]
... from pip._internal.utils.compatibility_tags import version_info_to_nodot
... version_info_to_nodot(0)
... except Exception as e:
... format_error(e, filter=None, filename_filter=("site-packages", ""))
'[compatibility_tags.py:version_info_to_nodot(23)] return "".join(map(str, version_info[:2])) >>> TypeError(\'int\' object is not subscriptable)'
>>> try:
... # test with filename_filter[1]
... from pip._internal.utils.compatibility_tags import version_info_to_nodot
... version_info_to_nodot(0)
... except Exception as e:
... format_error(e, filter=None, filename_filter=("", "site-packages"))
'[<doctest>:<module>(4)] return "".join(map(str, version_info[:2])) >>> TypeError(\'int\' object is not subscriptable)'
"""
try:
filter = filter or always_return_value(True)
tbs = [tb for tb in traceback.extract_tb(error.__traceback__) if filter(tb)]
if filter:
_filter = filter
else:
_filter = always_return_value(True)
tbs = [tb for tb in traceback.extract_tb(error.__traceback__) if _filter(tb)]
if isinstance(index, slice):
tbs = tbs[index]
elif isinstance(index, int):
tbs = [tbs[index]]
else:
raise ValueError("Invalid index type")
trace_route_list = []
include, exclude = filename_filter
for tb in tbs:
filename = tb.filename
if include and include not in filename:
continue
if exclude and exclude in filename:
continue
if exists(filename):
_basename = basename(filename)
elif filename[0] == "<":
Expand Down

0 comments on commit f389e68

Please sign in to comment.