Skip to content

Commit

Permalink
Add support for ExceptionGroup backport (through "exceptiongroup" lib)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Sep 8, 2023
1 parent a91c1cc commit 3f6ff51
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 122 deletions.
12 changes: 10 additions & 2 deletions loguru/_better_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ def is_exception_group(exc):
return isinstance(exc, ExceptionGroup)

else:
try:
from exceptiongroup import ExceptionGroup
except ImportError:

def is_exception_group(exc):
return False
def is_exception_group(exc):
return False

else:

def is_exception_group(exc):
return isinstance(exc, ExceptionGroup)


class SyntaxHighlighter:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"colorama==0.4.6 ; python_version>='3.8'",
"freezegun==1.1.0 ; python_version<'3.8'",
"freezegun==1.2.2 ; python_version>='3.8'",
"exceptiongroup==1.1.3; ; pythonversion>='3.7' and python_version<'3.11'"
# Type checking.
"mypy==v0.910 ; python_version<'3.6'",
"mypy==v0.971 ; python_version>='3.6' and python_version<'3.7'",
Expand Down
68 changes: 34 additions & 34 deletions tests/exceptions/output/modern/grouped_as_cause_and_context.txt
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@

+ Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 37, in main
| raise ExceptionGroup("group_1", [error_1, error_2])
| ExceptionGroup: group_1 (2 sub-exceptions)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 26, in main
| a()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 10, in a
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 16, in a
| 1 / 0
| ZeroDivisionError: division by zero
+---------------- 2 ----------------
| Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 25, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| b()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 14, in b
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in b
| raise ValueError("Error")
| ValueError: Error
+------------------------------------

The above exception was the direct cause of the following exception:

+ Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 33, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 39, in main
| raise ExceptionGroup("group_2", [error_2, error_1]) from err
| ExceptionGroup: group_2 (2 sub-exceptions)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 25, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| b()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 14, in b
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in b
| raise ValueError("Error")
| ValueError: Error
+---------------- 2 ----------------
| Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 26, in main
| a()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 10, in a
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 16, in a
| 1 / 0
| ZeroDivisionError: division by zero
+------------------------------------

During handling of the above exception, another exception occurred:

+ Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 42, in <module>
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 48, in <module>
| main()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 35, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 41, in main
| raise ExceptionGroup("group_3", [err])
| ExceptionGroup: group_3 (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 33, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 39, in main
| raise ExceptionGroup("group_2", [error_2, error_1]) from err
| ExceptionGroup: group_2 (2 sub-exceptions)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 25, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| b()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 14, in b
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in b
| raise ValueError("Error")
| ValueError: Error
+---------------- 2 ----------------
| Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 26, in main
| a()
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 10, in a
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 16, in a
| 1 / 0
| ZeroDivisionError: division by zero
+------------------------------------

+ Exception Group Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 37, in main
| raise ExceptionGroup("group_1", [error_1, error_2])
|  │ └ ValueError('Error')
|  └ ZeroDivisionError('division by zero')
Expand All @@ -81,22 +81,22 @@ During handling of the above exception, another exception occurred:
+-+---------------- 1 ----------------
| Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 26, in main
| a()
| └ <function a at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 10, in a
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 16, in a
| 1 / 0
|
| ZeroDivisionError: division by zero
+---------------- 2 ----------------
| Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 25, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| b()
| └ <function b at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 14, in b
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in b
| raise ValueError("Error")
|
| ValueError: Error
Expand All @@ -108,7 +108,7 @@ During handling of the above exception, another exception occurred:

+ Exception Group Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 33, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 39, in main
| raise ExceptionGroup("group_2", [error_2, error_1]) from err
|  │ └ ZeroDivisionError('division by zero')
|  └ ValueError('Error')
Expand All @@ -117,22 +117,22 @@ During handling of the above exception, another exception occurred:
+-+---------------- 1 ----------------
| Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 25, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| b()
| └ <function b at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 14, in b
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in b
| raise ValueError("Error")
|
| ValueError: Error
+---------------- 2 ----------------
| Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 26, in main
| a()
| └ <function a at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 10, in a
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 16, in a
| 1 / 0
|
| ZeroDivisionError: division by zero
Expand All @@ -144,18 +144,18 @@ During handling of the above exception, another exception occurred:

+ Exception Group Traceback (most recent call last):
|
| > File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 42, in <module>
| > File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 48, in <module>
| main()
| └ <function main at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 35, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 41, in main
| raise ExceptionGroup("group_3", [err])
|
| ExceptionGroup: group_3 (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 33, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 39, in main
| raise ExceptionGroup("group_2", [error_2, error_1]) from err
|  │ └ ZeroDivisionError('division by zero')
|  └ ValueError('Error')
Expand All @@ -164,22 +164,22 @@ During handling of the above exception, another exception occurred:
+-+---------------- 1 ----------------
| Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 25, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 31, in main
| b()
| └ <function b at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 14, in b
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in b
| raise ValueError("Error")
|
| ValueError: Error
+---------------- 2 ----------------
| Traceback (most recent call last):
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 20, in main
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 26, in main
| a()
| └ <function a at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 10, in a
| File "tests/exceptions/source/modern/grouped_as_cause_and_context.py", line 16, in a
| 1 / 0
|
| ZeroDivisionError: division by zero
Expand Down
8 changes: 4 additions & 4 deletions tests/exceptions/output/modern/grouped_max_depth.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

+ Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_max_depth.py", line 26, in <module>
| File "tests/exceptions/source/modern/grouped_max_depth.py", line 31, in <module>
| main()
| File "tests/exceptions/source/modern/grouped_max_depth.py", line 19, in main
| File "tests/exceptions/source/modern/grouped_max_depth.py", line 24, in main
| raise ExceptionGroup("group", [nesting_left, nesting_right, nesting_both])
| ExceptionGroup: group (3 sub-exceptions)
+-+---------------- 1 ----------------
Expand Down Expand Up @@ -159,11 +159,11 @@

+ Exception Group Traceback (most recent call last):
|
| > File "tests/exceptions/source/modern/grouped_max_depth.py", line 26, in <module>
| > File "tests/exceptions/source/modern/grouped_max_depth.py", line 31, in <module>
| main()
| └ <function main at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_max_depth.py", line 19, in main
| File "tests/exceptions/source/modern/grouped_max_depth.py", line 24, in main
| raise ExceptionGroup("group", [nesting_left, nesting_right, nesting_both])
|  │ │ └ ExceptionGroup('group', [ValueError(-99), ExceptionGroup('group', [ValueError(-98), ExceptionGroup('group', [ValueError(-97),...
|  │ └ ExceptionGroup('group', [ExceptionGroup('group', [ExceptionGroup('group', [ExceptionGroup('group', [ExceptionGroup('group', [...
Expand Down
8 changes: 4 additions & 4 deletions tests/exceptions/output/modern/grouped_max_length.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

+ Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/grouped_max_length.py", line 15, in <module>
| File "tests/exceptions/source/modern/grouped_max_length.py", line 20, in <module>
| main()
| File "tests/exceptions/source/modern/grouped_max_length.py", line 8, in main
| File "tests/exceptions/source/modern/grouped_max_length.py", line 13, in main
| raise ExceptionGroup("group", errors)
| ExceptionGroup: group (100 sub-exceptions)
+-+---------------- 1 ----------------
Expand Down Expand Up @@ -41,11 +41,11 @@

+ Exception Group Traceback (most recent call last):
|
| > File "tests/exceptions/source/modern/grouped_max_length.py", line 15, in <module>
| > File "tests/exceptions/source/modern/grouped_max_length.py", line 20, in <module>
| main()
| └ <function main at 0xDEADBEEF>
|
| File "tests/exceptions/source/modern/grouped_max_length.py", line 8, in main
| File "tests/exceptions/source/modern/grouped_max_length.py", line 13, in main
| raise ExceptionGroup("group", errors)
|  └ [ValueError(0), ValueError(1), ValueError(2), ValueError(3), ValueError(4), ValueError(5), ValueError(6), ValueError(7), Valu...
|
Expand Down
Loading

0 comments on commit 3f6ff51

Please sign in to comment.