Skip to content

Commit 2af998e

Browse files
committed
Prevent recursion for Debug impls
1 parent 4b655b9 commit 2af998e

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

vm/src/frame.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,11 +1211,18 @@ impl Frame {
12111211

12121212
impl fmt::Debug for Frame {
12131213
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1214+
trace!("formatting stack");
12141215
let stack_str = self
12151216
.stack
12161217
.borrow()
12171218
.iter()
1218-
.map(|elem| format!("\n > {:?}", elem))
1219+
.map(|elem| {
1220+
if elem.payload.as_any().is::<Frame>() {
1221+
"\n > {frame}".to_string()
1222+
} else {
1223+
format!("\n > {:?}", elem)
1224+
}
1225+
})
12191226
.collect::<String>();
12201227
let block_str = self
12211228
.blocks

vm/src/obj/objdict.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::{Cell, RefCell};
22
use std::collections::HashMap;
3+
use std::fmt;
34
use std::ops::{Deref, DerefMut};
45

56
use crate::pyobject::{
@@ -14,13 +15,20 @@ use super::objtype;
1415

1516
pub type DictContentType = HashMap<String, (PyObjectRef, PyObjectRef)>;
1617

17-
#[derive(Default, Debug)]
18+
#[derive(Default)]
1819
pub struct PyDict {
1920
// TODO: should be private
2021
pub entries: RefCell<DictContentType>,
2122
}
2223
pub type PyDictRef = PyRef<PyDict>;
2324

25+
impl fmt::Debug for PyDict {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
// TODO: implement more detailed, non-recursive Debug formatter
28+
f.write_str("dict")
29+
}
30+
}
31+
2432
impl PyValue for PyDict {
2533
fn required_type(ctx: &PyContext) -> PyObjectRef {
2634
ctx.dict_type()

vm/src/obj/objlist.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ use crate::pyobject::{
1414
};
1515
use crate::vm::{ReprGuard, VirtualMachine};
1616
use num_traits::ToPrimitive;
17+
use std::fmt;
1718

18-
#[derive(Debug, Default)]
19+
#[derive(Default)]
1920
pub struct PyList {
2021
// TODO: shouldn't be public
2122
pub elements: RefCell<Vec<PyObjectRef>>,
2223
}
2324

25+
impl fmt::Debug for PyList {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
// TODO: implement more detailed, non-recursive Debug formatter
28+
f.write_str("list")
29+
}
30+
}
31+
2432
impl From<Vec<PyObjectRef>> for PyList {
2533
fn from(elements: Vec<PyObjectRef>) -> Self {
2634
PyList {

vm/src/obj/objset.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44

55
use std::cell::{Cell, RefCell};
6-
use std::collections::hash_map::DefaultHasher;
7-
use std::collections::HashMap;
6+
use std::collections::{hash_map::DefaultHasher, HashMap};
7+
use std::fmt;
88
use std::hash::{Hash, Hasher};
99

1010
use super::objbool;
@@ -17,11 +17,18 @@ use crate::pyobject::{
1717
};
1818
use crate::vm::{ReprGuard, VirtualMachine};
1919

20-
#[derive(Debug, Default)]
20+
#[derive(Default)]
2121
pub struct PySet {
2222
elements: RefCell<HashMap<u64, PyObjectRef>>,
2323
}
2424

25+
impl fmt::Debug for PySet {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
// TODO: implement more detailed, non-recursive Debug formatter
28+
f.write_str("set")
29+
}
30+
}
31+
2532
impl PyValue for PySet {
2633
fn required_type(ctx: &PyContext) -> PyObjectRef {
2734
ctx.set_type()

vm/src/obj/objtuple.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cell::{Cell, RefCell};
2+
use std::fmt;
23
use std::hash::{Hash, Hasher};
34

45
use crate::pyobject::{
@@ -15,13 +16,20 @@ use super::objsequence::{
1516
use super::objstr;
1617
use super::objtype;
1718

18-
#[derive(Debug, Default)]
19+
#[derive(Default)]
1920
pub struct PyTuple {
2021
// TODO: shouldn't be public
2122
// TODO: tuples are immutable, remove this RefCell
2223
pub elements: RefCell<Vec<PyObjectRef>>,
2324
}
2425

26+
impl fmt::Debug for PyTuple {
27+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28+
// TODO: implement more informational, non-recursive Debug formatter
29+
f.write_str("tuple")
30+
}
31+
}
32+
2533
impl From<Vec<PyObjectRef>> for PyTuple {
2634
fn from(elements: Vec<PyObjectRef>) -> Self {
2735
PyTuple {

0 commit comments

Comments
 (0)