Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3379b3d
Add context managers for turtle.fill and turtle.poly
yngvem Oct 6, 2024
879d886
Add documentation
MarieRoald Oct 20, 2024
54f709c
Add context manager to turtle.TurtleScreen for disabling auto-update
MarieRoald Oct 27, 2024
437c8ce
Forward functions correctly
yngvem Oct 27, 2024
d11d8f9
Add documentation
yngvem Oct 27, 2024
1f04ee4
πŸ“œπŸ€– Added by blurb_it.
blurb-it[bot] Nov 3, 2024
3947de1
Fix typo in docs
MarieRoald Nov 3, 2024
5ee489b
Apply suggestions from code review
MarieRoald Nov 8, 2024
28a5ac6
Address review comments
MarieRoald Nov 8, 2024
6621bd3
Apply suggestions from code review
MarieRoald Nov 8, 2024
2f5c488
Address review comments
MarieRoald Nov 8, 2024
302a1ed
Apply suggestions from code review
MarieRoald Nov 9, 2024
ad779d5
Use setUp for unit tests
yngvem Nov 9, 2024
fd89c95
Address review comments
yngvem Nov 9, 2024
fe39751
Add missing blank line
MarieRoald Nov 9, 2024
0a5e250
Update Lib/turtle.py
MarieRoald Nov 9, 2024
6d381ec
Apply suggestions from code review
MarieRoald Nov 14, 2024
42d678d
Update Lib/turtle.py
MarieRoald Nov 14, 2024
7928306
Address reviewer comments
MarieRoald Nov 14, 2024
815a8a3
Update Lib/test/test_turtle.py
MarieRoald Dec 18, 2024
fc01695
Merge branch 'main' into fix-issue-126349
hugovk Dec 29, 2024
8487336
Re-sort
hugovk Dec 29, 2024
401c07f
Merge branch 'main' into fix-issue-126349
erlend-aasland Dec 30, 2024
3f2b4ef
Pull in main and resolve conflicts
erlend-aasland Dec 30, 2024
0c2fca0
Remove unneeded stylistic change; remove unprintable char from NEWS e…
erlend-aasland Dec 30, 2024
6115eef
Change assert to self.assertEqual
MarieRoald Jan 5, 2025
11d9bc3
Change assert to self.assertEqual
MarieRoald Jan 18, 2025
061fe2a
Improve usage of unittest.mock.patch
MarieRoald Jan 18, 2025
32bbf68
Merge branch 'main' into fix-issue-126349
MarieRoald Jan 18, 2025
b2ab84b
Merge branch 'main' into fix-issue-126349
MarieRoald Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add context managers for turtle.fill and turtle.poly
Co-authored-by: Marie Roald <[email protected]>
  • Loading branch information
yngvem and MarieRoald committed Nov 3, 2024
commit 3379b3d6620a55a95712d8707f7ffc7b66fbea82
108 changes: 108 additions & 0 deletions Lib/test/test_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import unittest
import unittest.mock
import tempfile
import sys
Comment thread
picnixz marked this conversation as resolved.
Outdated
from contextlib import contextmanager
from test import support
from test.support import import_helper
from test.support import os_helper
Expand Down Expand Up @@ -527,6 +529,112 @@ def test_save(self) -> None:
assert f.read() == "postscript"


class TestTurtle(unittest.TestCase):
@contextmanager
def patch_screen(self):
"""Patch turtle._Screen for testing without a display.

We must patch the `_Screen` class itself instead of the `_Screen`
instance because instatiating it requires a display.
"""
m = unittest.mock.MagicMock()
m.__class__ = turtle._Screen
m.mode.return_value = "standard"

patch = unittest.mock.patch('turtle._Screen.__new__', return_value=m)
try:
yield patch.__enter__()
finally:
patch.__exit__(*sys.exc_info())

def test_begin_end_fill(self):
"""begin_fill and end_fill counter each other."""
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
with self.patch_screen():
t = turtle.Turtle()

self.assertFalse(t.filling())
t.begin_fill()
self.assertTrue(t.filling())
t.end_fill()
self.assertFalse(t.filling())

def test_fill(self):
"""The context manager behaves like begin_ and end_ fill."""
with self.patch_screen():
t = turtle.Turtle()

self.assertFalse(t.filling())
with t.fill():
self.assertTrue(t.filling())
self.assertFalse(t.filling())

def test_fill_resets_after_exception(self):
"""The context manager cleans up correctly after exceptions."""
with self.patch_screen():
t = turtle.Turtle()
try:
with t.fill():
self.assertTrue(t.filling())
raise Exception
Comment thread
picnixz marked this conversation as resolved.
Outdated
except Exception:
self.assertFalse(t.filling())

def test_fill_context_when_filling(self):
"""The context manager works even when the turtle is already filling."""
with self.patch_screen():
t = turtle.Turtle()

t.begin_fill()
self.assertTrue(t.filling())
with t.fill():
self.assertTrue(t.filling())
self.assertFalse(t.filling())

def test_begin_end_poly(self):
"""begin_fill and end_poly counter each other."""
with self.patch_screen():
t = turtle.Turtle()

self.assertFalse(t._creatingPoly)
t.begin_poly()
self.assertTrue(t._creatingPoly)
t.end_poly()
self.assertFalse(t._creatingPoly)

def test_poly(self):
"""The context manager behaves like begin_ and end_ poly."""
with self.patch_screen():
t = turtle.Turtle()

self.assertFalse(t._creatingPoly)
with t.poly():
self.assertTrue(t._creatingPoly)
self.assertFalse(t._creatingPoly)

def test_poly_resets_after_exception(self):
"""The context manager cleans up correctly after exceptions."""
with self.patch_screen():
t = turtle.Turtle()
try:
with t.poly():
self.assertTrue(t._creatingPoly)
raise Exception
Comment thread
picnixz marked this conversation as resolved.
Outdated
except Exception:
self.assertFalse(t._creatingPoly)

def test_poly_context_when_creating_poly(self):
"""The context manager works when the turtle is already creating poly.
"""
with self.patch_screen():
t = turtle.Turtle()

t.begin_poly()
self.assertTrue(t._creatingPoly)
with t.poly():
self.assertTrue(t._creatingPoly)
self.assertFalse(t._creatingPoly)


class TestModuleLevel(unittest.TestCase):
def test_all_signatures(self):
import inspect
Expand Down
19 changes: 18 additions & 1 deletion Lib/turtle.py
Comment thread
erlend-aasland marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
from copy import deepcopy
from tkinter import simpledialog

from contextlib import contextmanager
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated

_tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen',
'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D']
_tg_screen_functions = ['addshape', 'bgcolor', 'bgpic', 'bye',
Expand Down Expand Up @@ -3382,6 +3384,14 @@ def filling(self):
"""
return isinstance(self._fillpath, list)

@contextmanager
def fill(self):
self.begin_fill()
try:
yield
finally:
self.end_fill()

def begin_fill(self):
"""Called just before drawing a shape to be filled.

Expand All @@ -3402,7 +3412,6 @@ def begin_fill(self):
self.undobuffer.push(("beginfill", self._fillitem))
self._update()


Comment thread
hugovk marked this conversation as resolved.
def end_fill(self):
"""Fill the shape drawn after the call begin_fill().

Expand Down Expand Up @@ -3506,6 +3515,14 @@ def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")):
if self.undobuffer:
self.undobuffer.cumulate = False

@contextmanager
def poly(self):
self.begin_poly()
try:
yield
finally:
self.end_poly()

def begin_poly(self):
"""Start recording the vertices of a polygon.

Expand Down