Skip to content

Commit 90db981

Browse files
Merge pull request RustPython#707 from RustPython/joey/proper-pyref-display
Fix the Display impl for PyRef
2 parents ce120c1 + 5980771 commit 90db981

File tree

5 files changed

+59
-39
lines changed

5 files changed

+59
-39
lines changed

vm/src/obj/objlist.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,57 +245,62 @@ impl PyListRef {
245245
}
246246
}
247247

248-
fn eq(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
248+
fn eq(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
249249
if self.as_object().is(&other) {
250-
return Ok(true);
250+
return Ok(vm.new_bool(true));
251251
}
252252

253253
if objtype::isinstance(&other, &vm.ctx.list_type()) {
254254
let zelf = self.elements.borrow();
255255
let other = get_elements(&other);
256-
Ok(seq_equal(vm, &zelf, &other)?)
256+
let res = seq_equal(vm, &zelf, &other)?;
257+
Ok(vm.new_bool(res))
257258
} else {
258-
Ok(false)
259+
Ok(vm.ctx.not_implemented())
259260
}
260261
}
261262

262-
fn lt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
263+
fn lt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
263264
if objtype::isinstance(&other, &vm.ctx.list_type()) {
264265
let zelf = self.elements.borrow();
265266
let other = get_elements(&other);
266-
Ok(seq_lt(vm, &zelf, &other)?)
267+
let res = seq_lt(vm, &zelf, &other)?;
268+
Ok(vm.new_bool(res))
267269
} else {
268-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '<'", self, other)))
270+
Ok(vm.ctx.not_implemented())
269271
}
270272
}
271273

272-
fn gt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
274+
fn gt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
273275
if objtype::isinstance(&other, &vm.ctx.list_type()) {
274276
let zelf = self.elements.borrow();
275277
let other = get_elements(&other);
276-
Ok(seq_gt(vm, &zelf, &other)?)
278+
let res = seq_gt(vm, &zelf, &other)?;
279+
Ok(vm.new_bool(res))
277280
} else {
278-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '>'", self, other)))
281+
Ok(vm.ctx.not_implemented())
279282
}
280283
}
281284

282-
fn ge(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
285+
fn ge(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
283286
if objtype::isinstance(&other, &vm.ctx.list_type()) {
284287
let zelf = self.elements.borrow();
285288
let other = get_elements(&other);
286-
Ok(seq_ge(vm, &zelf, &other)?)
289+
let res = seq_ge(vm, &zelf, &other)?;
290+
Ok(vm.new_bool(res))
287291
} else {
288-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '>='", self, other)))
292+
Ok(vm.ctx.not_implemented())
289293
}
290294
}
291295

292-
fn le(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
296+
fn le(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
293297
if objtype::isinstance(&other, &vm.ctx.list_type()) {
294298
let zelf = self.elements.borrow();
295299
let other = get_elements(&other);
296-
Ok(seq_le(vm, &zelf, &other)?)
300+
let res = seq_le(vm, &zelf, &other)?;
301+
Ok(vm.new_bool(res))
297302
} else {
298-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '<='", self, other)))
303+
Ok(vm.ctx.not_implemented())
299304
}
300305
}
301306
}

vm/src/obj/objstr.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::hash::{Hash, Hasher};
23
use std::ops::Range;
34
use std::str::FromStr;
@@ -26,11 +27,9 @@ pub struct PyString {
2627
}
2728
pub type PyStringRef = PyRef<PyString>;
2829

29-
impl<T: ToString> From<T> for PyString {
30-
fn from(t: T) -> PyString {
31-
PyString {
32-
value: t.to_string(),
33-
}
30+
impl fmt::Display for PyString {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
fmt::Display::fmt(&self.value, f)
3433
}
3534
}
3635

vm/src/obj/objtuple.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,47 @@ impl PyValue for PyTuple {
4646
pub type PyTupleRef = PyRef<PyTuple>;
4747

4848
impl PyTupleRef {
49-
fn lt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
49+
fn lt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
5050
if objtype::isinstance(&other, &vm.ctx.tuple_type()) {
5151
let zelf = self.elements.borrow();
5252
let other = get_elements(&other);
53-
Ok(seq_lt(vm, &zelf, &other)?)
53+
let res = seq_lt(vm, &zelf, &other)?;
54+
Ok(vm.new_bool(res))
5455
} else {
55-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '<'", self, other)))
56+
Ok(vm.ctx.not_implemented())
5657
}
5758
}
5859

59-
fn gt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
60+
fn gt(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
6061
if objtype::isinstance(&other, &vm.ctx.tuple_type()) {
6162
let zelf = self.elements.borrow();
6263
let other = get_elements(&other);
63-
Ok(seq_gt(vm, &zelf, &other)?)
64+
let res = seq_gt(vm, &zelf, &other)?;
65+
Ok(vm.new_bool(res))
6466
} else {
65-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '>'", self, other)))
67+
Ok(vm.ctx.not_implemented())
6668
}
6769
}
6870

69-
fn ge(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
71+
fn ge(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
7072
if objtype::isinstance(&other, &vm.ctx.tuple_type()) {
7173
let zelf = self.elements.borrow();
7274
let other = get_elements(&other);
73-
Ok(seq_ge(vm, &zelf, &other)?)
75+
let res = seq_ge(vm, &zelf, &other)?;
76+
Ok(vm.new_bool(res))
7477
} else {
75-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '>='", self, other)))
78+
Ok(vm.ctx.not_implemented())
7679
}
7780
}
7881

79-
fn le(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
82+
fn le(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
8083
if objtype::isinstance(&other, &vm.ctx.tuple_type()) {
8184
let zelf = self.elements.borrow();
8285
let other = get_elements(&other);
83-
Ok(seq_le(vm, &zelf, &other)?)
86+
let res = seq_le(vm, &zelf, &other)?;
87+
Ok(vm.new_bool(res))
8488
} else {
85-
Err(vm.new_type_error(format!("Cannot compare {} and {} using '<='", self, other)))
89+
Ok(vm.ctx.not_implemented())
8690
}
8791
}
8892

@@ -93,7 +97,7 @@ impl PyTupleRef {
9397
let elements = e1.iter().chain(e2.iter()).cloned().collect();
9498
Ok(vm.ctx.new_tuple(elements))
9599
} else {
96-
Err(vm.new_type_error(format!("Cannot add {} and {}", self, other)))
100+
Ok(vm.ctx.not_implemented())
97101
}
98102
}
99103

@@ -112,13 +116,14 @@ impl PyTupleRef {
112116
Ok(count)
113117
}
114118

115-
fn eq(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult<bool> {
119+
fn eq(self, other: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
116120
if objtype::isinstance(&other, &vm.ctx.tuple_type()) {
117121
let zelf = &self.elements.borrow();
118122
let other = get_elements(&other);
119-
seq_equal(vm, &zelf, &other)
123+
let res = seq_equal(vm, &zelf, &other)?;
124+
Ok(vm.new_bool(res))
120125
} else {
121-
Ok(false)
126+
Ok(vm.ctx.not_implemented())
122127
}
123128
}
124129

vm/src/obj/objtype.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::RefCell;
22
use std::collections::HashMap;
3+
use std::fmt;
34

45
use crate::function::PyFuncArgs;
56
use crate::pyobject::{
@@ -20,6 +21,12 @@ pub struct PyClass {
2021
pub mro: Vec<PyClassRef>,
2122
}
2223

24+
impl fmt::Display for PyClass {
25+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26+
fmt::Display::fmt(&self.name, f)
27+
}
28+
}
29+
2330
pub type PyClassRef = PyRef<PyClass>;
2431

2532
impl PyValue for PyClass {

vm/src/pyobject.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,13 @@ impl<T> IntoPyObject for PyRef<T> {
768768
}
769769
}
770770

771-
impl<T> fmt::Display for PyRef<T> {
771+
impl<T: fmt::Display> fmt::Display for PyRef<T>
772+
where
773+
T: PyValue + fmt::Display,
774+
{
772775
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
773-
self.obj.fmt(f)
776+
let value: &T = self.obj.payload().expect("unexpected payload for type");
777+
fmt::Display::fmt(value, f)
774778
}
775779
}
776780

0 commit comments

Comments
 (0)