diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst index fb886a714d7..de58f266bf5 100644 --- a/Doc/library/idle.rst +++ b/Doc/library/idle.rst @@ -713,6 +713,10 @@ or ``print`` or ``write`` to sys.stdout or sys.stderr, IDLE should be started in a command line window. The secondary subprocess will then be attached to that window for input and output. +The IDLE code running in the execution process adds frames to the call stack +that would not be there otherwise. IDLE wraps ``sys.getrecursionlimit`` and +``sys.setrecursionlimit`` to reduce the effect of the additional stack frames. + If ``sys`` is reset by user code, such as with ``importlib.reload(sys)``, IDLE's changes are lost and input from the keyboard and output to the screen will not work correctly. diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index 689539e73c1..5d4a936d63f 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -3,6 +3,11 @@ Released on 2019-10-20? ====================================== +bpo-26806: To compensate for stack frames added by IDLE and avoid +possible problems with low recursion limits, add 30 to limits in the +user code execution process. Subtract 30 when reporting recursion +limits to make this addition mostly transparent. + bpo-37325: Fix tab focus traversal order for help source and custom run dialogs. diff --git a/Lib/idlelib/help.html b/Lib/idlelib/help.html index 91803fd06c8..cee6887df68 100644 --- a/Lib/idlelib/help.html +++ b/Lib/idlelib/help.html @@ -14,7 +14,7 @@ - + @@ -135,113 +135,75 @@ described below are moved around to conform to Apple guidelines.

File menu (Shell and Editor)

-
New File
-
Create a new file editing window.
-
Open…
-
Open an existing file with an Open dialog.
-
Recent Files
-
Open a list of recent files. Click one to open it.
-
Open Module…
-
Open an existing module (searches sys.path).
+
New File
Create a new file editing window.
+
Open…
Open an existing file with an Open dialog.
+
Recent Files
Open a list of recent files. Click one to open it.
+
Open Module…
Open an existing module (searches sys.path).
-
Class Browser
-
Show functions, classes, and methods in the current Editor file in a +
Class Browser
Show functions, classes, and methods in the current Editor file in a tree structure. In the shell, open a module first.
-
Path Browser
-
Show sys.path directories, modules, functions, classes and methods in a +
Path Browser
Show sys.path directories, modules, functions, classes and methods in a tree structure.
-
Save
-
Save the current window to the associated file, if there is one. Windows +
Save
Save the current window to the associated file, if there is one. Windows that have been changed since being opened or last saved have a * before and after the window title. If there is no associated file, do Save As instead.
-
Save As…
-
Save the current window with a Save As dialog. The file saved becomes the +
Save As…
Save the current window with a Save As dialog. The file saved becomes the new associated file for the window.
-
Save Copy As…
-
Save the current window to different file without changing the associated +
Save Copy As…
Save the current window to different file without changing the associated file.
-
Print Window
-
Print the current window to the default printer.
-
Close
-
Close the current window (ask to save if unsaved).
-
Exit
-
Close all windows and quit IDLE (ask to save unsaved windows).
+
Print Window
Print the current window to the default printer.
+
Close
Close the current window (ask to save if unsaved).
+
Exit
Close all windows and quit IDLE (ask to save unsaved windows).

Edit menu (Shell and Editor)

-
Undo
-
Undo the last change to the current window. A maximum of 1000 changes may +
Undo
Undo the last change to the current window. A maximum of 1000 changes may be undone.
-
Redo
-
Redo the last undone change to the current window.
-
Cut
-
Copy selection into the system-wide clipboard; then delete the selection.
-
Copy
-
Copy selection into the system-wide clipboard.
-
Paste
-
Insert contents of the system-wide clipboard into the current window.
+
Redo
Redo the last undone change to the current window.
+
Cut
Copy selection into the system-wide clipboard; then delete the selection.
+
Copy
Copy selection into the system-wide clipboard.
+
Paste
Insert contents of the system-wide clipboard into the current window.

The clipboard functions are also available in context menus.

-
Select All
-
Select the entire contents of the current window.
-
Find…
-
Open a search dialog with many options
-
Find Again
-
Repeat the last search, if there is one.
-
Find Selection
-
Search for the currently selected string, if there is one.
-
Find in Files…
-
Open a file search dialog. Put results in a new output window.
-
Replace…
-
Open a search-and-replace dialog.
-
Go to Line
-
Move cursor to the line number requested and make that line visible.
-
Show Completions
-
Open a scrollable list allowing selection of keywords and attributes. See +
Select All
Select the entire contents of the current window.
+
Find…
Open a search dialog with many options
+
Find Again
Repeat the last search, if there is one.
+
Find Selection
Search for the currently selected string, if there is one.
+
Find in Files…
Open a file search dialog. Put results in a new output window.
+
Replace…
Open a search-and-replace dialog.
+
Go to Line
Move cursor to the line number requested and make that line visible.
+
Show Completions
Open a scrollable list allowing selection of keywords and attributes. See Completions in the Editing and navigation section below.
-
Expand Word
-
Expand a prefix you have typed to match a full word in the same window; +
Expand Word
Expand a prefix you have typed to match a full word in the same window; repeat to get a different expansion.
-
Show call tip
-
After an unclosed parenthesis for a function, open a small window with +
Show call tip
After an unclosed parenthesis for a function, open a small window with function parameter hints. See Calltips in the Editing and navigation section below.
-
Show surrounding parens
-
Highlight the surrounding parenthesis.
+
Show surrounding parens
Highlight the surrounding parenthesis.

Format menu (Editor window only)

-
Indent Region
-
Shift selected lines right by the indent width (default 4 spaces).
-
Dedent Region
-
Shift selected lines left by the indent width (default 4 spaces).
-
Comment Out Region
-
Insert ## in front of selected lines.
-
Uncomment Region
-
Remove leading # or ## from selected lines.
-
Tabify Region
-
Turn leading stretches of spaces into tabs. (Note: We recommend using +
Indent Region
Shift selected lines right by the indent width (default 4 spaces).
+
Dedent Region
Shift selected lines left by the indent width (default 4 spaces).
+
Comment Out Region
Insert ## in front of selected lines.
+
Uncomment Region
Remove leading # or ## from selected lines.
+
Tabify Region
Turn leading stretches of spaces into tabs. (Note: We recommend using 4 space blocks to indent Python code.)
-
Untabify Region
-
Turn all tabs into the correct number of spaces.
-
Toggle Tabs
-
Open a dialog to switch between indenting with spaces and tabs.
-
New Indent Width
-
Open a dialog to change indent width. The accepted default by the Python +
Untabify Region
Turn all tabs into the correct number of spaces.
+
Toggle Tabs
Open a dialog to switch between indenting with spaces and tabs.
+
New Indent Width
Open a dialog to change indent width. The accepted default by the Python community is 4 spaces.
-
Format Paragraph
-
Reformat the current blank-line-delimited paragraph in comment block or +
Format Paragraph
Reformat the current blank-line-delimited paragraph in comment block or multiline string or selected line in a string. All lines in the paragraph will be formatted to less than N columns, where N defaults to 72.
-
Strip trailing whitespace
-
Remove trailing space and other whitespace characters after the last +
Strip trailing whitespace
Remove trailing space and other whitespace characters after the last non-whitespace character of a line by applying str.rstrip to each line, including lines within multiline strings.
@@ -249,20 +211,17 @@ including lines within multiline strings.

Run menu (Editor window only)

-
Python Shell
-
Open or wake up the Python Shell window.
+
Python Shell
Open or wake up the Python Shell window.
-
Check Module
-
Check the syntax of the module currently open in the Editor window. If the +
Check Module
Check the syntax of the module currently open in the Editor window. If the module has not been saved IDLE will either prompt the user to save or autosave, as selected in the General tab of the Idle Settings dialog. If there is a syntax error, the approximate location is indicated in the Editor window.
-
Run Module
-
Do Check Module. If no error, restart the shell to clean the +
Run Module
Do Check Module. If no error, restart the shell to clean the environment, then execute the module. Output is displayed in the Shell window. Note that output requires use of print or write. When execution is complete, the Shell retains focus and displays a prompt. @@ -271,8 +230,7 @@ This is similar to executing a file with -
Run… Customized
-
Same as Run Module, but run the module with customized +
Run… Customized
Same as Run Module, but run the module with customized settings. Command Line Arguments extend sys.argv as if passed on a command line. The module can be run in the Shell without restarting.
@@ -280,56 +238,44 @@ on a command line. The module can be run in the Shell without restarting.

Shell menu (Shell window only)

-
View Last Restart
-
Scroll the shell window to the last Shell restart.
-
Restart Shell
-
Restart the shell to clean the environment.
-
Previous History
-
Cycle through earlier commands in history which match the current entry.
-
Next History
-
Cycle through later commands in history which match the current entry.
-
Interrupt Execution
-
Stop a running program.
+
View Last Restart
Scroll the shell window to the last Shell restart.
+
Restart Shell
Restart the shell to clean the environment.
+
Previous History
Cycle through earlier commands in history which match the current entry.
+
Next History
Cycle through later commands in history which match the current entry.
+
Interrupt Execution
Stop a running program.

Debug menu (Shell window only)

-
Go to File/Line
-
Look on the current line. with the cursor, and the line above for a filename +
Go to File/Line
Look on the current line. with the cursor, and the line above for a filename and line number. If found, open the file if not already open, and show the line. Use this to view source lines referenced in an exception traceback and lines found by Find in Files. Also available in the context menu of the Shell window and Output windows.
-
Debugger (toggle)
-
When activated, code entered in the Shell or run from an Editor will run +
Debugger (toggle)
When activated, code entered in the Shell or run from an Editor will run under the debugger. In the Editor, breakpoints can be set with the context menu. This feature is still incomplete and somewhat experimental.
-
Stack Viewer
-
Show the stack traceback of the last exception in a tree widget, with +
Stack Viewer
Show the stack traceback of the last exception in a tree widget, with access to locals and globals.
-
Auto-open Stack Viewer
-
Toggle automatically opening the stack viewer on an unhandled exception.
+
Auto-open Stack Viewer
Toggle automatically opening the stack viewer on an unhandled exception.

Options menu (Shell and Editor)

-
Configure IDLE
-
Open a configuration dialog and change preferences for the following: +
Configure IDLE
Open a configuration dialog and change preferences for the following: fonts, indentation, keybindings, text color themes, startup windows and size, additional help sources, and extensions. On macOS, open the configuration dialog by selecting Preferences in the application menu. For more, see Setting preferences under Help and preferences.
-
Show/Hide Code Context (Editor Window only)
-
Open a pane at the top of the edit window which shows the block context +
Show/Hide Code Context (Editor Window only)
Open a pane at the top of the edit window which shows the block context of the code which has scrolled above the top of the window. See Code Context in the Editing and Navigation section below.
-
Zoom/Restore Height
-
Toggles the window between normal size and maximum height. The initial size +
Zoom/Restore Height
Toggles the window between normal size and maximum height. The initial size defaults to 40 lines by 80 chars unless changed on the General tab of the Configure IDLE dialog. The maximum height for a screen is determined by momentarily maximizing a window the first time one is zoomed on the screen. @@ -345,16 +291,12 @@ no effect when a window is maximized.

Help menu (Shell and Editor)

-
About IDLE
-
Display version, copyright, license, credits, and more.
-
IDLE Help
-
Display this IDLE document, detailing the menu options, basic editing and +
About IDLE
Display version, copyright, license, credits, and more.
+
IDLE Help
Display this IDLE document, detailing the menu options, basic editing and navigation, and other tips.
-
Python Docs
-
Access local Python documentation, if installed, or start a web browser +
Python Docs
Access local Python documentation, if installed, or start a web browser and open docs.python.org showing the latest Python documentation.
-
Turtle Demo
-
Run the turtledemo module with example Python code and turtle drawings.
+
Turtle Demo
Run the turtledemo module with example Python code and turtle drawings.

Additional help sources may be added here with the Configure IDLE dialog under the General tab. See the Help sources subsection below @@ -365,32 +307,25 @@ for more on Help menu choices.

Open a context menu by right-clicking in a window (Control-click on macOS). Context menus have the standard clipboard functions also on the Edit menu.

-
Cut
-
Copy selection into the system-wide clipboard; then delete the selection.
-
Copy
-
Copy selection into the system-wide clipboard.
-
Paste
-
Insert contents of the system-wide clipboard into the current window.
+
Cut
Copy selection into the system-wide clipboard; then delete the selection.
+
Copy
Copy selection into the system-wide clipboard.
+
Paste
Insert contents of the system-wide clipboard into the current window.

Editor windows also have breakpoint functions. Lines with a breakpoint set are specially marked. Breakpoints only have an effect when running under the debugger. Breakpoints for a file are saved in the user’s .idlerc directory.

-
Set Breakpoint
-
Set a breakpoint on the current line.
-
Clear Breakpoint
-
Clear the breakpoint on that line.
+
Set Breakpoint
Set a breakpoint on the current line.
+
Clear Breakpoint
Clear the breakpoint on that line.

Shell and Output windows also have the following.

-
Go to file/line
-
Same as in Debug menu.
+
Go to file/line
Same as in Debug menu.

The Shell window also has an output squeezing facility explained in the Python Shell window subsection below.

-
Squeeze
-
If the cursor is over an output line, squeeze all the output between +
Squeeze
If the cursor is over an output line, squeeze all the output between the code above and the prompt below down to a ‘Squeezed text’ label.
@@ -670,6 +605,9 @@ such as multiprocessing. If such subprocess use print or write to sys.stdout or sys.stderr, IDLE should be started in a command line window. The secondary subprocess will then be attached to that window for input and output.

+

The IDLE code running in the execution process adds frames to the call stack +that would not be there otherwise. IDLE wraps sys.getrecursionlimit and +sys.setrecursionlimit to reduce their visibility.

If sys is reset by user code, such as with importlib.reload(sys), IDLE’s changes are lost and input from the keyboard and output to the screen will not work correctly.

@@ -772,7 +710,7 @@ re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible.

-

Deprecated since version 3.4.

+

Deprecated since version 3.4.

@@ -885,7 +823,7 @@ also used for testing.

Previous topic

tkinter.scrolledtext — Scrolled Text Widget

+ title="previous chapter">tkinter.scrolledtext — Scrolled Text Widget

Next topic

Other Graphical User Interface Packages

@@ -957,11 +895,11 @@ also used for testing.



- Last updated on Jun 17, 2019. + Last updated on Jul 04, 2019. Found a bug?
- Created using Sphinx 1.8.1. + Created using Sphinx 2.1.1.
diff --git a/Lib/idlelib/idle_test/test_run.py b/Lib/idlelib/idle_test/test_run.py index 46f0235fbfd..d0f1e9207bb 100644 --- a/Lib/idlelib/idle_test/test_run.py +++ b/Lib/idlelib/idle_test/test_run.py @@ -6,6 +6,8 @@ from unittest import mock from test.support import captured_stderr import io +import sys + class RunTest(unittest.TestCase): @@ -260,5 +262,36 @@ class PseudeOutputFilesTest(unittest.TestCase): self.assertRaises(TypeError, f.close, 1) +class TestSysRecursionLimitWrappers(unittest.TestCase): + + def test_bad_setrecursionlimit_calls(self): + run.install_recursionlimit_wrappers() + self.addCleanup(run.uninstall_recursionlimit_wrappers) + f = sys.setrecursionlimit + self.assertRaises(TypeError, f, limit=100) + self.assertRaises(TypeError, f, 100, 1000) + self.assertRaises(ValueError, f, 0) + + def test_roundtrip(self): + run.install_recursionlimit_wrappers() + self.addCleanup(run.uninstall_recursionlimit_wrappers) + + # check that setting the recursion limit works + orig_reclimit = sys.getrecursionlimit() + self.addCleanup(sys.setrecursionlimit, orig_reclimit) + sys.setrecursionlimit(orig_reclimit + 3) + + # check that the new limit is returned by sys.getrecursionlimit() + new_reclimit = sys.getrecursionlimit() + self.assertEqual(new_reclimit, orig_reclimit + 3) + + def test_default_recursion_limit_preserved(self): + orig_reclimit = sys.getrecursionlimit() + run.install_recursionlimit_wrappers() + self.addCleanup(run.uninstall_recursionlimit_wrappers) + new_reclimit = sys.getrecursionlimit() + self.assertEqual(new_reclimit, orig_reclimit) + + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 6b3928b7bf2..c6ed76b23a2 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -4,10 +4,12 @@ Simplified, pyshell.ModifiedInterpreter spawns a subprocess with f'''{sys.executable} -c "__import__('idlelib.run').run.main()"''' '.run' is needed because __import__ returns idlelib, not idlelib.run. """ +import functools import io import linecache import queue import sys +import textwrap import time import traceback import _thread as thread @@ -305,6 +307,64 @@ def fix_scaling(root): font['size'] = round(-0.75*size) +RECURSIONLIMIT_DELTA = 30 +def install_recursionlimit_wrappers(): + """Install wrappers to always add 30 to the recursion limit.""" + # see: bpo-26806 + + @functools.wraps(sys.setrecursionlimit) + def setrecursionlimit(*args, **kwargs): + # mimic the original sys.setrecursionlimit()'s input handling + if kwargs: + raise TypeError( + "setrecursionlimit() takes no keyword arguments") + try: + limit, = args + except ValueError: + raise TypeError(f"setrecursionlimit() takes exactly one " + f"argument ({len(args)} given)") + if not limit > 0: + raise ValueError( + "recursion limit must be greater or equal than 1") + + return setrecursionlimit.__wrapped__(limit + RECURSIONLIMIT_DELTA) + + setrecursionlimit.__doc__ += "\n\n" + textwrap.fill(textwrap.dedent(f"""\ + This IDLE wrapper adds {RECURSIONLIMIT_DELTA} to prevent possible + uninterruptible loops. + """).strip()) + + @functools.wraps(sys.getrecursionlimit) + def getrecursionlimit(): + return getrecursionlimit.__wrapped__() - RECURSIONLIMIT_DELTA + + getrecursionlimit.__doc__ += "\n\n" + textwrap.fill(textwrap.dedent(f"""\ + This IDLE wrapper subtracts {RECURSIONLIMIT_DELTA} to compensate for + the {RECURSIONLIMIT_DELTA} IDLE adds when setting the limit. + """).strip()) + + # add the delta to the default recursion limit, to compensate + sys.setrecursionlimit(sys.getrecursionlimit() + RECURSIONLIMIT_DELTA) + + sys.setrecursionlimit = setrecursionlimit + sys.getrecursionlimit = getrecursionlimit + + +def uninstall_recursionlimit_wrappers(): + """Uninstall the recursion limit wrappers from the sys module. + + IDLE only uses this for tests. Users can import run and call + this to remove the wrapping. + """ + if ( + getattr(sys.setrecursionlimit, '__wrapped__', None) and + getattr(sys.getrecursionlimit, '__wrapped__', None) + ): + sys.setrecursionlimit = sys.setrecursionlimit.__wrapped__ + sys.getrecursionlimit = sys.getrecursionlimit.__wrapped__ + sys.setrecursionlimit(sys.getrecursionlimit() - RECURSIONLIMIT_DELTA) + + class MyRPCServer(rpc.RPCServer): def handle_error(self, request, client_address): @@ -448,6 +508,8 @@ class MyHandler(rpc.RPCHandler): # sys.stdin gets changed from within IDLE's shell. See issue17838. self._keep_stdin = sys.stdin + install_recursionlimit_wrappers() + self.interp = self.get_remote_proxy("interp") rpc.RPCHandler.getresponse(self, myseq=None, wait=0.05) diff --git a/Misc/NEWS.d/next/IDLE/2019-06-10-22-48-50.bpo-26806.Zltkum.rst b/Misc/NEWS.d/next/IDLE/2019-06-10-22-48-50.bpo-26806.Zltkum.rst new file mode 100644 index 00000000000..8514bb9292f --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2019-06-10-22-48-50.bpo-26806.Zltkum.rst @@ -0,0 +1,4 @@ +To compensate for stack frames added by IDLE and avoid possible problems +with low recursion limits, add 30 to limits in the user code execution +process. Subtract 30 when reporting recursion limits to make this addition +mostly transparent.