diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-22 15:25:33 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-22 15:25:33 (GMT) |
commit | 80a1803193bcaef7a50324974117c3aa3fb0e48f (patch) | |
tree | 61be7ea1dd62d519596fd1eb7b8d918167aee7bb /Lib/turtle.py | |
parent | a3369a524c6f862936c1d8a24e23c796aa91f2c7 (diff) | |
download | cpython-80a1803193bcaef7a50324974117c3aa3fb0e48f.zip cpython-80a1803193bcaef7a50324974117c3aa3fb0e48f.tar.gz cpython-80a1803193bcaef7a50324974117c3aa3fb0e48f.tar.bz2 |
Issue #6639: Module-level turtle functions no longer raise TclError after
closing the window.
Diffstat (limited to 'Lib/turtle.py')
-rw-r--r-- | Lib/turtle.py | 67 |
1 files changed, 33 insertions, 34 deletions
diff --git a/Lib/turtle.py b/Lib/turtle.py index f4400c9..cbd4f47 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -1288,7 +1288,7 @@ class TurtleScreen(TurtleScreenBase): def _incrementudc(self): """Increment update counter.""" if not TurtleScreen._RUNNING: - TurtleScreen._RUNNNING = True + TurtleScreen._RUNNING = True raise Terminator if self._tracing > 0: self._updatecounter += 1 @@ -3754,7 +3754,7 @@ class _Screen(TurtleScreen): Turtle._screen = None _Screen._root = None _Screen._canvas = None - TurtleScreen._RUNNING = True + TurtleScreen._RUNNING = False root.destroy() def bye(self): @@ -3795,7 +3795,6 @@ class _Screen(TurtleScreen): except AttributeError: exit(0) - class Turtle(RawTurtle): """RawTurtle auto-creating (scrolled) canvas. @@ -3818,18 +3817,6 @@ class Turtle(RawTurtle): Pen = Turtle -def _getpen(): - """Create the 'anonymous' turtle if not already present.""" - if Turtle._pen is None: - Turtle._pen = Turtle() - return Turtle._pen - -def _getscreen(): - """Create a TurtleScreen if not already present.""" - if Turtle._screen is None: - Turtle._screen = Screen() - return Turtle._screen - def write_docstringdict(filename="turtle_docstringdict"): """Create and write docstring-dictionary to file. @@ -3952,26 +3939,38 @@ def _screen_docrevise(docstr): ## as functions. So we can enhance, change, add, delete methods to these ## classes and do not need to change anything here. +__func_body = """\ +def {name}{paramslist}: + if {obj} is None: + if not TurtleScreen._RUNNING: + TurtleScreen._RUNNING = True + raise Terminator + {obj} = {init} + try: + return {obj}.{name}{argslist} + except TK.TclError: + if not TurtleScreen._RUNNING: + TurtleScreen._RUNNING = True + raise Terminator + raise +""" -for methodname in _tg_screen_functions: - pl1, pl2 = getmethparlist(eval('_Screen.' + methodname)) - if pl1 == "": - print(">>>>>>", pl1, pl2) - continue - defstr = ("def %(key)s%(pl1)s: return _getscreen().%(key)s%(pl2)s" % - {'key':methodname, 'pl1':pl1, 'pl2':pl2}) - exec(defstr) - eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__) - -for methodname in _tg_turtle_functions: - pl1, pl2 = getmethparlist(eval('Turtle.' + methodname)) - if pl1 == "": - print(">>>>>>", pl1, pl2) - continue - defstr = ("def %(key)s%(pl1)s: return _getpen().%(key)s%(pl2)s" % - {'key':methodname, 'pl1':pl1, 'pl2':pl2}) - exec(defstr) - eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__) +def _make_global_funcs(functions, cls, obj, init, docrevise): + for methodname in functions: + method = getattr(cls, methodname) + pl1, pl2 = getmethparlist(method) + if pl1 == "": + print(">>>>>>", pl1, pl2) + continue + defstr = __func_body.format(obj=obj, init=init, name=methodname, + paramslist=pl1, argslist=pl2) + exec(defstr, globals()) + globals()[methodname].__doc__ = docrevise(method.__doc__) + +_make_global_funcs(_tg_screen_functions, _Screen, + 'Turtle._screen', 'Screen()', _screen_docrevise) +_make_global_funcs(_tg_turtle_functions, Turtle, + 'Turtle._pen', 'Turtle()', _turtle_docrevise) done = mainloop |