Skip to content

Commit

Permalink
Merge pull request RustPython#2938 from DimitrisJim/deque_repr
Browse files Browse the repository at this point in the history
Fix deque repr hang if element __repr__ mutates.
  • Loading branch information
youknowone authored Aug 22, 2021
2 parents 04d77c2 + fd23e4e commit 60edbdd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions extra_tests/snippets/stdlib_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@
assert not (d < d)
assert d == d
assert not (d != d)


# Test that calling an evil __repr__ can't hang deque
class BadRepr:
def __repr__(self):
self.d.pop()
return ''

b = BadRepr()
d = deque([1, b, 2])
b.d = d
repr(d)
4 changes: 2 additions & 2 deletions vm/src/stdlib/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ mod _collections {
#[pymethod(magic)]
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
let repr = if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
let elements = zelf
.borrow_deque()
let deque = zelf.borrow_deque().clone();
let elements = deque
.iter()
.map(|obj| vm.to_repr(obj))
.collect::<Result<Vec<_>, _>>()?;
Expand Down

0 comments on commit 60edbdd

Please sign in to comment.