Skip to content

Commit e407661

Browse files
authored
gh-65772: Clean-up turtle module (#104218)
* Remove the unused, private, and undocumented name `_ver` and the commented-out `print` call. * Don't add math functions to `__all__`. Beginners should learn to `import math` to access them. * Gregor Lindel, who wrote this version of turtle, dropped plans to implement turtle on another toolkit at least a decade ago. Drop `_dot` code preparing for this, but add a hint comment. * `_Screen` is meant to be a singleton class. To enforce that, it needs either a `__new__` that returns the singleton or `else...raise` in `__iter__`. Merely removing the `if` clauses as suggested might break something if a user were to call `_Screen` directly. Leave the code alone until a problem is evident. * Turtledemo injects into _Screen both _root and _canvas, configured as it needs them to be. Making _canvas an `__init__` option would require skipping some but not all of the lines under 'if _Screen._canvas is None:`. Leave working code alone.
1 parent 6616710 commit e407661

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

Lib/turtle.py

+21-40
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# misrepresented as being the original software.
2222
# 3. This notice may not be removed or altered from any source distribution.
2323

24-
2524
"""
2625
Turtle graphics is a popular way for introducing programming to
2726
kids. It was part of the original Logo programming language developed
@@ -97,13 +96,8 @@
9796
9897
Behind the scenes there are some features included with possible
9998
extensions in mind. These will be commented and documented elsewhere.
100-
10199
"""
102100

103-
_ver = "turtle 1.1b- - for Python 3.1 - 4. 5. 2009"
104-
105-
# print(_ver)
106-
107101
import tkinter as TK
108102
import types
109103
import math
@@ -141,7 +135,7 @@
141135
_tg_utilities = ['write_docstringdict', 'done']
142136

143137
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
144-
_tg_utilities + ['Terminator']) # + _math_functions)
138+
_tg_utilities + ['Terminator'])
145139

146140
_alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
147141
'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',
@@ -598,9 +592,6 @@ def _write(self, pos, txt, align, font, pencolor):
598592
x0, y0, x1, y1 = self.cv.bbox(item)
599593
return item, x1-1
600594

601-
## def _dot(self, pos, size, color):
602-
## """may be implemented for some other graphics toolkit"""
603-
604595
def _onclick(self, item, fun, num=1, add=None):
605596
"""Bind fun to mouse-click event on turtle.
606597
fun must be a function with two arguments, the coordinates
@@ -2726,7 +2717,7 @@ def _cc(self, args):
27262717
if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
27272718
raise TurtleGraphicsError("bad color sequence: %s" % str(args))
27282719
return "#%02x%02x%02x" % (r, g, b)
2729-
2720+
27302721
def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None:
27312722
"""Instantly move turtle to an absolute position.
27322723
@@ -2738,14 +2729,14 @@ def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None:
27382729
call: teleport(x, y) # two coordinates
27392730
--or: teleport(x) # teleport to x position, keeping y as is
27402731
--or: teleport(y=y) # teleport to y position, keeping x as is
2741-
--or: teleport(x, y, fill_gap=True)
2732+
--or: teleport(x, y, fill_gap=True)
27422733
# teleport but fill the gap in between
27432734
27442735
Move turtle to an absolute position. Unlike goto(x, y), a line will not
27452736
be drawn. The turtle's orientation does not change. If currently
27462737
filling, the polygon(s) teleported from will be filled after leaving,
27472738
and filling will begin again after teleporting. This can be disabled
2748-
with fill_gap=True, which makes the imaginary line traveled during
2739+
with fill_gap=True, which makes the imaginary line traveled during
27492740
teleporting act as a fill barrier like in goto(x, y).
27502741
27512742
Example (for a Turtle instance named turtle):
@@ -2773,7 +2764,7 @@ def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None:
27732764
self._position = Vec2D(new_x, new_y)
27742765
self.pen(pendown=pendown)
27752766
if was_filling and not fill_gap:
2776-
self.begin_fill()
2767+
self.begin_fill()
27772768

27782769
def clone(self):
27792770
"""Create and return a clone of the turtle.
@@ -3455,27 +3446,22 @@ def dot(self, size=None, *color):
34553446
if size is None:
34563447
size = self._pensize + max(self._pensize, 4)
34573448
color = self._colorstr(color)
3458-
if hasattr(self.screen, "_dot"):
3459-
item = self.screen._dot(self._position, size, color)
3460-
self.items.append(item)
3461-
if self.undobuffer:
3462-
self.undobuffer.push(("dot", item))
3463-
else:
3464-
pen = self.pen()
3465-
if self.undobuffer:
3466-
self.undobuffer.push(["seq"])
3467-
self.undobuffer.cumulate = True
3468-
try:
3469-
if self.resizemode() == 'auto':
3470-
self.ht()
3471-
self.pendown()
3472-
self.pensize(size)
3473-
self.pencolor(color)
3474-
self.forward(0)
3475-
finally:
3476-
self.pen(pen)
3477-
if self.undobuffer:
3478-
self.undobuffer.cumulate = False
3449+
# If screen were to gain a dot function, see GH #104218.
3450+
pen = self.pen()
3451+
if self.undobuffer:
3452+
self.undobuffer.push(["seq"])
3453+
self.undobuffer.cumulate = True
3454+
try:
3455+
if self.resizemode() == 'auto':
3456+
self.ht()
3457+
self.pendown()
3458+
self.pensize(size)
3459+
self.pencolor(color)
3460+
self.forward(0)
3461+
finally:
3462+
self.pen(pen)
3463+
if self.undobuffer:
3464+
self.undobuffer.cumulate = False
34793465

34803466
def _write(self, txt, align, font):
34813467
"""Performs the writing for write()
@@ -3751,11 +3737,6 @@ class _Screen(TurtleScreen):
37513737
_title = _CFG["title"]
37523738

37533739
def __init__(self):
3754-
# XXX there is no need for this code to be conditional,
3755-
# as there will be only a single _Screen instance, anyway
3756-
# XXX actually, the turtle demo is injecting root window,
3757-
# so perhaps the conditional creation of a root should be
3758-
# preserved (perhaps by passing it as an optional parameter)
37593740
if _Screen._root is None:
37603741
_Screen._root = self._root = _Root()
37613742
self._root.title(_Screen._title)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove unneeded comments and code in turtle.py.

0 commit comments

Comments
 (0)