Skip to content

Commit 978e7ea

Browse files
Merge pull request RustPython#241 from coolreader18/formatted-exceptions
Format exceptions nicely (no more RefCell { value: .. }!)
2 parents 946df53 + 0ac0432 commit 978e7ea

13 files changed

+104
-57
lines changed

vm/src/builtins.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,11 @@ fn builtin_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
408408
let len_method_name = "__len__".to_string();
409409
match vm.get_method(obj.clone(), &len_method_name) {
410410
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
411-
Err(..) => Err(vm.context().new_str(
412-
format!(
413-
"TypeError: object of this {:?} type has no method {:?}",
414-
obj, len_method_name
415-
)
416-
.to_string(),
417-
)),
411+
Err(..) => Err(vm.new_type_error(format!(
412+
"object of type '{}' has no method {:?}",
413+
objtype::get_type_name(&obj.typ()),
414+
len_method_name
415+
))),
418416
}
419417
}
420418

@@ -596,13 +594,10 @@ fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
596594
let string = objstr::get_value(string);
597595
let string_len = string.chars().count();
598596
if string_len > 1 {
599-
return Err(vm.new_type_error(
600-
format!(
601-
"ord() expected a character, but string of length {} found",
602-
string_len
603-
)
604-
.to_string(),
605-
));
597+
return Err(vm.new_type_error(format!(
598+
"ord() expected a character, but string of length {} found",
599+
string_len
600+
)));
606601
}
607602
match string.chars().next() {
608603
Some(character) => Ok(vm

vm/src/frame.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ impl Frame {
511511
Err(exception)
512512
} else {
513513
let msg = format!(
514-
"Can only raise BaseException derived types, not {:?}",
515-
exception
514+
"Can only raise BaseException derived types, not {}",
515+
exception.borrow()
516516
);
517517
let type_error_type = vm.ctx.exceptions.type_error.clone();
518518
let type_error = vm.new_exception(type_error_type, msg);
@@ -850,7 +850,7 @@ impl Frame {
850850
scope = scope.get_parent();
851851
} else {
852852
let name_error_type = vm.ctx.exceptions.name_error.clone();
853-
let msg = format!("Has not attribute '{}'", name);
853+
let msg = format!("name '{}' is not defined", name);
854854
let name_error = vm.new_exception(name_error_type, msg);
855855
break Err(name_error);
856856
}

vm/src/obj/objcomplex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn complex_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6868
if objtype::isinstance(i2, &vm.ctx.complex_type()) {
6969
Ok(vm.ctx.new_complex(v1 + get_value(i2)))
7070
} else {
71-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
71+
Err(vm.new_type_error(format!("Cannot add {} and {}", i.borrow(), i2.borrow())))
7272
}
7373
}
7474

vm/src/obj/objfloat.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn make_float(vm: &mut VirtualMachine, obj: &PyObjectRef) -> Result<f64, PyO
5252
)?;
5353
Ok(get_value(&res))
5454
} else {
55-
Err(vm.new_type_error(format!("Cannot cast {:?} to float", obj)))
55+
Err(vm.new_type_error(format!("Cannot cast {} to float", obj.borrow())))
5656
}
5757
}
5858

@@ -159,7 +159,7 @@ fn float_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
159159
.ctx
160160
.new_float(v1 + objint::get_value(i2).to_f64().unwrap()))
161161
} else {
162-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
162+
Err(vm.new_type_error(format!("Cannot add {} and {}", i.borrow(), i2.borrow())))
163163
}
164164
}
165165

@@ -176,7 +176,11 @@ fn float_divmod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
176176
let r2 = float_mod(vm, args.clone());
177177
Ok(vm.ctx.new_tuple(vec![r1.unwrap(), r2.unwrap()]))
178178
} else {
179-
Err(vm.new_type_error(format!("Cannot divmod power {:?} and {:?}", i, i2)))
179+
Err(vm.new_type_error(format!(
180+
"Cannot divmod power {} and {}",
181+
i.borrow(),
182+
i2.borrow()
183+
)))
180184
}
181185
}
182186

@@ -193,7 +197,11 @@ fn float_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
193197
.ctx
194198
.new_float((get_value(i) / objint::get_value(i2).to_f64().unwrap()).floor()))
195199
} else {
196-
Err(vm.new_type_error(format!("Cannot floordiv {:?} and {:?}", i, i2)))
200+
Err(vm.new_type_error(format!(
201+
"Cannot floordiv {} and {}",
202+
i.borrow(),
203+
i2.borrow()
204+
)))
197205
}
198206
}
199207

@@ -211,7 +219,7 @@ fn float_sub(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
211219
.ctx
212220
.new_float(v1 - objint::get_value(i2).to_f64().unwrap()))
213221
} else {
214-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
222+
Err(vm.new_type_error(format!("Cannot add {} and {}", i.borrow(), i2.borrow())))
215223
}
216224
}
217225

@@ -228,7 +236,7 @@ fn float_mod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
228236
.ctx
229237
.new_float(get_value(i) % objint::get_value(i2).to_f64().unwrap()))
230238
} else {
231-
Err(vm.new_type_error(format!("Cannot mod {:?} and {:?}", i, i2)))
239+
Err(vm.new_type_error(format!("Cannot mod {} and {}", i.borrow(), i2.borrow())))
232240
}
233241
}
234242

@@ -254,7 +262,7 @@ fn float_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
254262
let result = v1.powf(objint::get_value(i2).to_f64().unwrap());
255263
Ok(vm.ctx.new_float(result))
256264
} else {
257-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
265+
Err(vm.new_type_error(format!("Cannot add {} and {}", i.borrow(), i2.borrow())))
258266
}
259267
}
260268

vm/src/obj/objint.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,17 @@ fn int_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
188188
arg_check!(
189189
vm,
190190
args,
191-
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
191+
required = [(_i, Some(vm.ctx.int_type())), (i2, None)]
192192
);
193-
let i = BigInt::from_pyobj(i);
193+
let i = BigInt::from_pyobj(_i);
194194
if objtype::isinstance(i2, &vm.ctx.int_type()) {
195195
Ok(vm.ctx.new_int(i + get_value(i2)))
196196
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
197197
Ok(vm
198198
.ctx
199199
.new_float(i.to_f64().unwrap() + objfloat::get_value(i2)))
200200
} else {
201-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", i, i2)))
201+
Err(vm.new_type_error(format!("Cannot add {} and {}", _i.borrow(), i2.borrow())))
202202
}
203203
}
204204

@@ -217,7 +217,11 @@ fn int_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
217217
if objtype::isinstance(i2, &vm.ctx.int_type()) {
218218
Ok(vm.ctx.new_int(get_value(i) / get_value(i2)))
219219
} else {
220-
Err(vm.new_type_error(format!("Cannot floordiv {:?} and {:?}", i, i2)))
220+
Err(vm.new_type_error(format!(
221+
"Cannot floordiv {} and {}",
222+
i.borrow(),
223+
i2.borrow()
224+
)))
221225
}
222226
}
223227

@@ -243,17 +247,21 @@ fn int_sub(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
243247
arg_check!(
244248
vm,
245249
args,
246-
required = [(i, Some(vm.ctx.int_type())), (i2, None)]
250+
required = [(_i, Some(vm.ctx.int_type())), (i2, None)]
247251
);
248-
let i = BigInt::from_pyobj(i);
252+
let i = BigInt::from_pyobj(_i);
249253
if objtype::isinstance(i2, &vm.ctx.int_type()) {
250254
Ok(vm.ctx.new_int(i - get_value(i2)))
251255
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
252256
Ok(vm
253257
.ctx
254258
.new_float(i.to_f64().unwrap() - objfloat::get_value(i2)))
255259
} else {
256-
Err(vm.new_not_implemented_error(format!("Cannot substract {:?} and {:?}", i, i2)))
260+
Err(vm.new_not_implemented_error(format!(
261+
"Cannot substract {} and {}",
262+
_i.borrow(),
263+
i2.borrow()
264+
)))
257265
}
258266
}
259267

@@ -270,7 +278,11 @@ fn int_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
270278
.ctx
271279
.new_float(get_value(i).to_f64().unwrap() * objfloat::get_value(i2)))
272280
} else {
273-
Err(vm.new_type_error(format!("Cannot multiply {:?} and {:?}", i, i2)))
281+
Err(vm.new_type_error(format!(
282+
"Cannot multiply {} and {}",
283+
i.borrow(),
284+
i2.borrow()
285+
)))
274286
}
275287
}
276288

@@ -290,7 +302,7 @@ fn int_truediv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
290302
.ctx
291303
.new_float(v1.to_f64().unwrap() / objfloat::get_value(i2)))
292304
} else {
293-
Err(vm.new_type_error(format!("Cannot divide {:?} and {:?}", i, i2)))
305+
Err(vm.new_type_error(format!("Cannot divide {} and {}", i.borrow(), i2.borrow())))
294306
}
295307
}
296308

@@ -304,7 +316,7 @@ fn int_mod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
304316
if objtype::isinstance(i2, &vm.ctx.int_type()) {
305317
Ok(vm.ctx.new_int(v1 % get_value(i2)))
306318
} else {
307-
Err(vm.new_type_error(format!("Cannot modulo {:?} and {:?}", i, i2)))
319+
Err(vm.new_type_error(format!("Cannot modulo {} and {}", i.borrow(), i2.borrow())))
308320
}
309321
}
310322

@@ -333,7 +345,11 @@ fn int_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
333345
let v2 = objfloat::get_value(i2);
334346
Ok(vm.ctx.new_float((v1.to_f64().unwrap()).powf(v2)))
335347
} else {
336-
Err(vm.new_type_error(format!("Cannot raise power {:?} and {:?}", i, i2)))
348+
Err(vm.new_type_error(format!(
349+
"Cannot raise power {} and {}",
350+
i.borrow(),
351+
i2.borrow()
352+
)))
337353
}
338354
}
339355

@@ -349,7 +365,11 @@ fn int_divmod(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
349365
let r2 = int_mod(vm, args.clone());
350366
Ok(vm.ctx.new_tuple(vec![r1.unwrap(), r2.unwrap()]))
351367
} else {
352-
Err(vm.new_type_error(format!("Cannot divmod power {:?} and {:?}", i, i2)))
368+
Err(vm.new_type_error(format!(
369+
"Cannot divmod power {} and {}",
370+
i.borrow(),
371+
i2.borrow()
372+
)))
353373
}
354374
}
355375

@@ -364,7 +384,7 @@ fn int_xor(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
364384
let v2 = get_value(i2);
365385
Ok(vm.ctx.new_int(v1 ^ v2))
366386
} else {
367-
Err(vm.new_type_error(format!("Cannot xor {:?} and {:?}", i, i2)))
387+
Err(vm.new_type_error(format!("Cannot xor {} and {}", i.borrow(), i2.borrow())))
368388
}
369389
}
370390

@@ -379,7 +399,7 @@ fn int_or(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
379399
let v2 = get_value(i2);
380400
Ok(vm.ctx.new_int(v1 | v2))
381401
} else {
382-
Err(vm.new_type_error(format!("Cannot or {:?} and {:?}", i, i2)))
402+
Err(vm.new_type_error(format!("Cannot or {} and {}", i.borrow(), i2.borrow())))
383403
}
384404
}
385405

@@ -394,7 +414,7 @@ fn int_and(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
394414
let v2 = get_value(i2);
395415
Ok(vm.ctx.new_int(v1 & v2))
396416
} else {
397-
Err(vm.new_type_error(format!("Cannot and {:?} and {:?}", i, i2)))
417+
Err(vm.new_type_error(format!("Cannot and {} and {}", i.borrow(), i2.borrow())))
398418
}
399419
}
400420

vm/src/obj/objlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn list_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8686
let elements = e1.iter().chain(e2.iter()).map(|e| e.clone()).collect();
8787
Ok(vm.ctx.new_list(elements))
8888
} else {
89-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", o, o2)))
89+
Err(vm.new_type_error(format!("Cannot add {} and {}", o.borrow(), o2.borrow())))
9090
}
9191
}
9292

vm/src/obj/objobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn object_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
164164
let attribute_error = vm.context().exceptions.attribute_error.clone();
165165
Err(vm.new_exception(
166166
attribute_error,
167-
format!("{:?} object has no attribute {}", cls, name),
167+
format!("{} has no attribute '{}'", obj.borrow(), name),
168168
))
169169
}
170170
}

vm/src/obj/objset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn set_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5858
);
5959

6060
if !objtype::issubclass(cls, &vm.ctx.set_type()) {
61-
return Err(vm.new_type_error(format!("{:?} is not a subtype of set", cls)));
61+
return Err(vm.new_type_error(format!("{} is not a subtype of set", cls.borrow())));
6262
}
6363

6464
let elements = match iterable {

vm/src/obj/objstr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn str_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
144144
.ctx
145145
.new_str(format!("{}{}", get_value(&s), get_value(&s2))))
146146
} else {
147-
Err(vm.new_type_error(format!("Cannot add {:?} and {:?}", s, s2)))
147+
Err(vm.new_type_error(format!("Cannot add {} and {}", s.borrow(), s2.borrow())))
148148
}
149149
}
150150

@@ -273,7 +273,11 @@ fn str_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
273273
}
274274
Ok(vm.ctx.new_str(result))
275275
} else {
276-
Err(vm.new_type_error(format!("Cannot multiply {:?} and {:?}", s, s2)))
276+
Err(vm.new_type_error(format!(
277+
"Cannot multiply {} and {}",
278+
s.borrow(),
279+
s2.borrow()
280+
)))
277281
}
278282
}
279283

vm/src/obj/objtuple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn tuple_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8585
);
8686

8787
if !objtype::issubclass(cls, &vm.ctx.tuple_type()) {
88-
return Err(vm.new_type_error(format!("{:?} is not a subtype of tuple", cls)));
88+
return Err(vm.new_type_error(format!("{} is not a subtype of tuple", cls.borrow())));
8989
}
9090

9191
let elements = if let Some(iterable) = iterable {

vm/src/obj/objtype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn type_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
205205
let attribute_error = vm.context().exceptions.attribute_error.clone();
206206
Err(vm.new_exception(
207207
attribute_error,
208-
format!("{:?} object {:?} has no attribute {}", mcl, cls, name),
208+
format!("{} has no attribute '{}'", cls.borrow(), name),
209209
))
210210
}
211211
}

0 commit comments

Comments
 (0)