Skip to content

Commit a611ba2

Browse files
committed
handle cause and context with same code block
1 parent 83914a9 commit a611ba2

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

‎vm/src/exceptions.rs‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,32 +251,33 @@ fn write_exception_recursive<W: Write>(
251251
// use `wite_exception` as a public interface.
252252
// It is similar to `print_exception_recursive` from `CPython`.
253253
seen.insert(exc.as_object().get_id());
254-
if let Some(cause) = exc.cause() {
254+
255+
#[allow(clippy::manual_map)]
256+
if let Some((cause_or_context, msg)) = if let Some(cause) = exc.cause() {
255257
// This can be a special case: `raise e from e`,
256258
// we just ignore it and treat like `raise e` without any extra steps.
257-
if !seen.contains(&cause.as_object().get_id()) {
258-
write_exception_recursive(output, vm, &cause, seen)?;
259-
writeln!(
260-
output,
261-
"\nThe above exception was the direct cause of the following exception:\n"
262-
)?;
263-
} else {
264-
seen.insert(cause.as_object().get_id());
265-
}
259+
Some((
260+
cause,
261+
"\nThe above exception was the direct cause of the following exception:\n",
262+
))
266263
} else if let Some(context) = exc.context() {
267264
// This can be a special case:
268265
// e = ValueError('e')
269266
// e.__context__ = e
270267
// In this case, we just ignore
271268
// `__context__` part from going into recursion.
272-
if !seen.contains(&context.as_object().get_id()) {
273-
write_exception_recursive(output, vm, &context, seen)?;
274-
writeln!(
275-
output,
276-
"\nDuring handling of the above exception, another exception occurred:\n"
277-
)?;
269+
Some((
270+
context,
271+
"\nDuring handling of the above exception, another exception occurred:\n",
272+
))
273+
} else {
274+
None
275+
} {
276+
if !seen.contains(&cause_or_context.as_object().get_id()) {
277+
write_exception_recursive(output, vm, &cause_or_context, seen)?;
278+
writeln!(output, "{}", msg)?;
278279
} else {
279-
seen.insert(context.as_object().get_id());
280+
seen.insert(cause_or_context.as_object().get_id());
280281
}
281282
}
282283

0 commit comments

Comments
 (0)