Skip to content

Commit e46a395

Browse files
committed
traceback.format_exception changed in 3.10; use compatible call
1 parent 171ff38 commit e46a395

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/dispatch/proto.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,15 @@ def _from_proto(cls, proto: call_pb.CallResult) -> CallResult:
297297
)
298298

299299
@classmethod
300-
def from_value(cls, output: Any, correlation_id: Optional[int] = None) -> CallResult:
300+
def from_value(
301+
cls, output: Any, correlation_id: Optional[int] = None
302+
) -> CallResult:
301303
return CallResult(correlation_id=correlation_id, output=output)
302304

303305
@classmethod
304-
def from_error(cls, error: Error, correlation_id: Optional[int] = None) -> CallResult:
306+
def from_error(
307+
cls, error: Error, correlation_id: Optional[int] = None
308+
) -> CallResult:
305309
return CallResult(correlation_id=correlation_id, error=error)
306310

307311

@@ -352,7 +356,9 @@ def __init__(
352356
self.value = value
353357
self.traceback = traceback
354358
if not traceback and value:
355-
self.traceback = "".join(format_exception(value)).encode("utf-8")
359+
self.traceback = "".join(
360+
format_exception(value.__class__, value, value.__traceback__)
361+
).encode("utf-8")
356362

357363
@classmethod
358364
def from_exception(cls, ex: Exception, status: Optional[Status] = None) -> Error:

tests/dispatch/test_error.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ def test_conversion_between_exception_and_error(self):
1212
except Exception as e:
1313
original_exception = e
1414
error = Error.from_exception(e)
15-
original_traceback = "".join(traceback.format_exception(original_exception))
15+
original_traceback = "".join(
16+
traceback.format_exception(
17+
original_exception.__class__,
18+
original_exception,
19+
original_exception.__traceback__,
20+
)
21+
)
1622

1723
# For some reasons traceback.format_exception does not include the caret
1824
# (^) in the original traceback, but it does in the reconstructed one,
@@ -24,7 +30,13 @@ def strip_caret(s):
2430

2531
reconstructed_exception = error.to_exception()
2632
reconstructed_traceback = strip_caret(
27-
"".join(traceback.format_exception(reconstructed_exception))
33+
"".join(
34+
traceback.format_exception(
35+
reconstructed_exception.__class__,
36+
reconstructed_exception,
37+
reconstructed_exception.__traceback__,
38+
)
39+
)
2840
)
2941

3042
assert type(reconstructed_exception) is type(original_exception)
@@ -34,7 +46,13 @@ def strip_caret(s):
3446
error2 = Error.from_exception(reconstructed_exception)
3547
reconstructed_exception2 = error2.to_exception()
3648
reconstructed_traceback2 = strip_caret(
37-
"".join(traceback.format_exception(reconstructed_exception2))
49+
"".join(
50+
traceback.format_exception(
51+
reconstructed_exception2.__class__,
52+
reconstructed_exception2,
53+
reconstructed_exception2.__traceback__,
54+
)
55+
)
3856
)
3957

4058
assert type(reconstructed_exception2) is type(original_exception)

0 commit comments

Comments
 (0)