Skip to content

Commit c8d8528

Browse files
committed
object.__str__ delegates to __repr__.
1 parent f42b8b1 commit c8d8528

File tree

8 files changed

+20
-16
lines changed

8 files changed

+20
-16
lines changed

vm/src/obj/objdict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn dict_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4444
Ok(vm.ctx.new_int(elements.len() as i32))
4545
}
4646

47-
fn dict_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
47+
fn dict_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4848
arg_check!(vm, args, required = [(o, Some(vm.ctx.dict_type()))]);
4949

5050
let elements = get_elements(o);
@@ -76,5 +76,5 @@ pub fn init(context: &PyContext) {
7676
let ref dict_type = context.dict_type;
7777
dict_type.set_attr("__len__", context.new_rustfunc(dict_len));
7878
dict_type.set_attr("__new__", context.new_rustfunc(dict_new));
79-
dict_type.set_attr("__str__", context.new_rustfunc(dict_str));
79+
dict_type.set_attr("__repr__", context.new_rustfunc(dict_repr));
8080
}

vm/src/obj/objfloat.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::super::vm::VirtualMachine;
55
use super::objint;
66
use super::objtype;
77

8-
fn str(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
8+
fn float_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
99
arg_check!(vm, args, required = [(float, Some(vm.ctx.float_type()))]);
1010
let v = get_value(float);
1111
Ok(vm.new_str(v.to_string()))
@@ -120,7 +120,6 @@ pub fn init(context: &PyContext) {
120120
float_type.set_attr("__add__", context.new_rustfunc(float_add));
121121
float_type.set_attr("__init__", context.new_rustfunc(float_init));
122122
float_type.set_attr("__pow__", context.new_rustfunc(float_pow));
123-
float_type.set_attr("__str__", context.new_rustfunc(str));
124123
float_type.set_attr("__sub__", context.new_rustfunc(float_sub));
125-
float_type.set_attr("__repr__", context.new_rustfunc(str));
124+
float_type.set_attr("__repr__", context.new_rustfunc(float_repr));
126125
}

vm/src/obj/objint.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::super::vm::VirtualMachine;
66
use super::objfloat;
77
use super::objtype;
88

9-
fn str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
9+
fn int_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1010
arg_check!(vm, args, required = [(int, Some(vm.ctx.int_type()))]);
1111
let v = get_value(int);
1212
Ok(vm.new_str(v.to_string()))
@@ -219,8 +219,7 @@ pub fn init(context: &PyContext) {
219219
int_type.set_attr("__mul__", context.new_rustfunc(int_mul));
220220
int_type.set_attr("__or__", context.new_rustfunc(int_or));
221221
int_type.set_attr("__pow__", context.new_rustfunc(int_pow));
222-
int_type.set_attr("__repr__", context.new_rustfunc(str));
223-
int_type.set_attr("__str__", context.new_rustfunc(str));
222+
int_type.set_attr("__repr__", context.new_rustfunc(int_repr));
224223
int_type.set_attr("__sub__", context.new_rustfunc(int_sub));
225224
int_type.set_attr("__truediv__", context.new_rustfunc(int_truediv));
226225
int_type.set_attr("__xor__", context.new_rustfunc(int_xor));

vm/src/obj/objlist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn list_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6868
}
6969
}
7070

71-
fn list_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
71+
fn list_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7272
arg_check!(vm, args, required = [(o, Some(vm.ctx.list_type()))]);
7373

7474
let elements = get_elements(o);
@@ -136,7 +136,7 @@ pub fn init(context: &PyContext) {
136136
list_type.set_attr("__eq__", context.new_rustfunc(list_eq));
137137
list_type.set_attr("__add__", context.new_rustfunc(list_add));
138138
list_type.set_attr("__len__", context.new_rustfunc(list_len));
139-
list_type.set_attr("__str__", context.new_rustfunc(list_str));
139+
list_type.set_attr("__repr__", context.new_rustfunc(list_repr));
140140
list_type.set_attr("append", context.new_rustfunc(append));
141141
list_type.set_attr("clear", context.new_rustfunc(clear));
142142
list_type.set_attr("reverse", context.new_rustfunc(reverse));

vm/src/obj/objobject.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ fn object_ne(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5050
}
5151

5252
fn object_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
53+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.object()))]);
54+
vm.call_method(zelf.clone(), "__repr__", vec![])
55+
}
56+
57+
fn object_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5358
arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]);
5459
let type_name = objtype::get_type_name(&obj.typ());
5560
let address = obj.get_id();
@@ -64,6 +69,7 @@ pub fn init(context: &PyContext) {
6469
object.set_attr("__ne__", context.new_rustfunc(object_ne));
6570
object.set_attr("__dict__", context.new_member_descriptor(object_dict));
6671
object.set_attr("__str__", context.new_rustfunc(object_str));
72+
object.set_attr("__repr__", context.new_rustfunc(object_repr));
6773
}
6874

6975
fn object_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {

vm/src/obj/objtuple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn tuple_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2929
Ok(vm.context().new_int(elements.len() as i32))
3030
}
3131

32-
fn tuple_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
32+
fn tuple_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3333
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.tuple_type()))]);
3434

3535
let elements = get_elements(zelf);
@@ -62,5 +62,5 @@ pub fn init(context: &PyContext) {
6262
let ref tuple_type = context.tuple_type;
6363
tuple_type.set_attr("__eq__", context.new_rustfunc(tuple_eq));
6464
tuple_type.set_attr("__len__", context.new_rustfunc(tuple_len));
65-
tuple_type.set_attr("__str__", context.new_rustfunc(tuple_str));
65+
tuple_type.set_attr("__repr__", context.new_rustfunc(tuple_repr));
6666
}

vm/src/obj/objtype.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn init(context: &PyContext) {
2525
type_type.set_attr("__new__", context.new_rustfunc(type_new));
2626
type_type.set_attr("__mro__", context.new_member_descriptor(type_mro));
2727
type_type.set_attr("__class__", context.new_member_descriptor(type_new));
28-
type_type.set_attr("__str__", context.new_rustfunc(type_str));
28+
type_type.set_attr("__repr__", context.new_rustfunc(type_repr));
2929
}
3030

3131
fn type_mro(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -221,7 +221,7 @@ pub fn new(typ: PyObjectRef, name: &str, bases: Vec<PyObjectRef>, dict: PyObject
221221
))
222222
}
223223

224-
fn type_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
224+
fn type_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
225225
arg_check!(vm, args, required = [(obj, Some(vm.ctx.type_type()))]);
226226
let type_name = get_type_name(&obj);
227227
Ok(vm.new_str(format!("<class '{}'>", type_name)))

vm/src/objbool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
3434
pub fn init(context: &PyContext) {
3535
let ref bool_type = context.bool_type;
3636
bool_type.set_attr("__new__", context.new_rustfunc(bool_new));
37-
bool_type.set_attr("__str__", context.new_rustfunc(bool_str));
37+
bool_type.set_attr("__repr__", context.new_rustfunc(bool_repr));
3838
bool_type.set_attr("__eq__", context.new_rustfunc(bool_eq));
3939
}
4040

@@ -71,7 +71,7 @@ pub fn get_value(obj: &PyObjectRef) -> bool {
7171
}
7272
}
7373

74-
fn bool_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
74+
fn bool_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
7575
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bool_type()))]);
7676
let v = get_value(obj);
7777
let s = if v {

0 commit comments

Comments
 (0)