Skip to content

Commit ff83147

Browse files
Make nested _undo_sequence() join the active sequence
1 parent 2aa8cbf commit ff83147

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎Lib/test/test_turtle.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,14 @@ def test_undo_sequence_resets_after_exception(self):
706706
self.assertRaises(ValueError, self.turtle.write, "spam")
707707
self.assertFalse(self.turtle.undobuffer.cumulate)
708708

709+
def test_nested_undo_sequence(self):
710+
with self.turtle._undo_sequence():
711+
self.turtle.teleport(10, 20)
712+
self.turtle.forward(10)
713+
self.assertEqual(self.turtle.undobufferentries(), 1)
714+
self.turtle.undo()
715+
self.assertEqual(self.turtle.pos(), (0, 0))
716+
709717
def test_stamp_without_undobuffer(self):
710718
shape = turtle.Shape("polygon", ((0, 0), (5, 9), (-5, 9)))
711719
self.turtle.screen._shapes = {self.turtle.shape(): shape}

‎Lib/turtle.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,14 +1656,15 @@ def _goto(self, end):
16561656
def _undo_sequence(self):
16571657
"""Record the enclosed actions as a single undo step."""
16581658
undobuffer = self.undobuffer
1659-
if undobuffer:
1660-
undobuffer.push(["seq"])
1661-
undobuffer.cumulate = True
1659+
if not undobuffer or undobuffer.cumulate:
1660+
yield
1661+
return
1662+
undobuffer.push(["seq"])
1663+
undobuffer.cumulate = True
16621664
try:
16631665
yield
16641666
finally:
1665-
if undobuffer:
1666-
undobuffer.cumulate = False
1667+
undobuffer.cumulate = False
16671668

16681669
def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None:
16691670
"""To be overwritten by child class RawTurtle.

0 commit comments

Comments
 (0)