Skip to content

Commit 7277e63

Browse files
committed
Add vm.to_repr and call for formatting list/tuple/dict elements.
1 parent 70f6d73 commit 7277e63

File tree

5 files changed

+9
-3
lines changed

5 files changed

+9
-3
lines changed

tests/snippets/strings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
assert str(2.1) == "2.1"
1616
assert str() == ""
1717
assert str("abc") == "abc"
18+
19+
assert str(["a", "b", "can't"]) == "['a', 'b', 'can\\'t']"

vm/src/obj/objdict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn dict_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5050
let elements = get_elements(o);
5151
let mut str_parts = vec![];
5252
for elem in elements {
53-
match vm.to_str(elem.1) {
53+
match vm.to_repr(elem.1) {
5454
Ok(s) => {
5555
let value_str = objstr::get_value(&s);
5656
str_parts.push(format!("{}: {}", elem.0, value_str));

vm/src/obj/objlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn list_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7474
let elements = get_elements(o);
7575
let mut str_parts = vec![];
7676
for elem in elements {
77-
match vm.to_str(elem) {
77+
match vm.to_repr(elem) {
7878
Ok(s) => str_parts.push(objstr::get_value(&s)),
7979
Err(err) => return Err(err),
8080
}

vm/src/obj/objtuple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn tuple_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3636

3737
let mut str_parts = vec![];
3838
for elem in elements {
39-
match vm.to_str(elem) {
39+
match vm.to_repr(elem) {
4040
Ok(s) => str_parts.push(objstr::get_value(&s)),
4141
Err(err) => return Err(err),
4242
}

vm/src/vm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ impl VirtualMachine {
142142
self.call_method(obj, "__str__", vec![])
143143
}
144144

145+
pub fn to_repr(&mut self, obj: PyObjectRef) -> PyResult {
146+
self.call_method(obj, "__repr__", vec![])
147+
}
148+
145149
pub fn current_frame(&self) -> &Frame {
146150
self.frames.last().unwrap()
147151
}

0 commit comments

Comments
 (0)