Print exception with traceback
(
&self,
output: &mut W,
exc: &Py<PyBaseException>,
)
| 130 | |
| 131 | /// Print exception with traceback |
| 132 | pub fn write_exception_inner<W: Write>( |
| 133 | &self, |
| 134 | output: &mut W, |
| 135 | exc: &Py<PyBaseException>, |
| 136 | ) -> Result<(), W::Error> { |
| 137 | let vm = self; |
| 138 | if let Some(tb) = exc.traceback.read().clone() { |
| 139 | writeln!(output, "Traceback (most recent call last):")?; |
| 140 | for tb in tb.iter() { |
| 141 | write_traceback_entry(output, &tb)?; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | let varargs = exc.args(); |
| 146 | let args_repr = vm.exception_args_as_string(varargs, true); |
| 147 | |
| 148 | let exc_class = exc.class(); |
| 149 | |
| 150 | if exc_class.fast_issubclass(vm.ctx.exceptions.syntax_error) { |
| 151 | return self.write_syntaxerror(output, exc, exc_class, &args_repr); |
| 152 | } |
| 153 | |
| 154 | let exc_name = exc_class.name(); |
| 155 | match args_repr.len() { |
| 156 | 0 => write!(output, "{exc_name}"), |
| 157 | 1 => write!(output, "{}: {}", exc_name, args_repr[0]), |
| 158 | _ => write!( |
| 159 | output, |
| 160 | "{}: ({})", |
| 161 | exc_name, |
| 162 | args_repr.into_iter().format(", "), |
| 163 | ), |
| 164 | }?; |
| 165 | |
| 166 | match offer_suggestions(exc, vm) { |
| 167 | Some(suggestions) => writeln!(output, ". Did you mean: '{suggestions}'?"), |
| 168 | None => writeln!(output), |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Format and write a SyntaxError |
| 173 | /// This logic is derived from TracebackException._format_syntax_error |
no test coverage detected