From 60b3075813732360ce0b4fe9eaa565940a035f21 Mon Sep 17 00:00:00 2001 From: Yukari Kaname Date: Mon, 2 Mar 2026 16:19:54 +0800 Subject: [PATCH 1/2] gh-91167: Fix cloned turtle pen not clearing completely When calling clear() on a cloned turtle, it incorrectly cleared the source turtle's drawings instead of its own. Fix by reinitializing the clone's currentLine and items to track only its own drawing state. --- Lib/test/test_turtle.py | 15 +++++++++++++++ Lib/turtle.py | 2 ++ 2 files changed, 17 insertions(+) diff --git a/Lib/test/test_turtle.py b/Lib/test/test_turtle.py index 12d2eed874148c..dd66ed63049575 100644 --- a/Lib/test/test_turtle.py +++ b/Lib/test/test_turtle.py @@ -671,6 +671,21 @@ def test_dot_signature(self): self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (0, 257, 0)) self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, 0, 257, 0) + def test_clone_clear_does_not_delete_source_items(self): + screen = self.turtle.screen + screen._delete.reset_mock() + screen._createline.side_effect = lambda: object() + + self.turtle.circle(20) + clone = self.turtle.clone() + source_items = set(self.turtle.items) + + clone.forward(50) + clone.clear() + + deleted_items = {call.args[0] for call in screen._delete.call_args_list} + self.assertFalse(source_items & deleted_items) + class TestModuleLevel(unittest.TestCase): def test_all_signatures(self): import inspect diff --git a/Lib/turtle.py b/Lib/turtle.py index b52d681b3af1c3..768f114755e2b7 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -2879,6 +2879,8 @@ def clone(self): q.turtle._item = [screen._createpoly() for item in screen._shapes[self.turtle.shapeIndex]._data] q.currentLineItem = screen._createline() + q.currentLine = [q._position] if q._drawing else [] + q.items = [q.currentLineItem] q._update() return q From 516865ed124ed29e89098864e794db98c09ab938 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 10:28:50 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-03-31-10-28-49.gh-issue-91167.CAf5BZ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-03-31-10-28-49.gh-issue-91167.CAf5BZ.rst diff --git a/Misc/NEWS.d/next/Library/2026-03-31-10-28-49.gh-issue-91167.CAf5BZ.rst b/Misc/NEWS.d/next/Library/2026-03-31-10-28-49.gh-issue-91167.CAf5BZ.rst new file mode 100644 index 00000000000000..fc2d5ae37e3204 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-31-10-28-49.gh-issue-91167.CAf5BZ.rst @@ -0,0 +1 @@ +Fix cloned turtle clear not clearing source pen state.