8000 bpo-1529353: IDLE: squeeze large output in the shell by taleinat · Pull Request #7626 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-1529353: IDLE: squeeze large output in the shell #7626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a7e130a
bpo-1529353: update the Squeezer extension and its tests
taleinat Jun 11, 2018
0af6a62
bpo-1529353: use idlelib.textview for previewing squeezed output
taleinat Aug 12, 2018
71fb2c1
Merge branch 'master' into bpo-1529353
taleinat Aug 12, 2018
bda6572
Refactor Squeezer from an extension to an integral part of IDLE.
taleinat Aug 13, 2018
53e74d9
Merge branch 'master' into bpo-1529353
taleinat Aug 27, 2018
902091e
Update Squeezer's tests given the latest changes
taleinat Aug 27, 2018
6d7035b
add ability to control text wrapping in IDLE's text viewer
taleinat Aug 28, 2018
45fb1a6
ask for user confirmation before expanding huge squeezed outputs
taleinat Aug 28, 2018
18106e8
increase Squeezer's default auto-squeeze-min-lines from 30 to 50
taleinat Aug 28, 2018
2c795e3
move 'copy' and 'preview' squeezed output actions to a context menu
taleinat Aug 28, 2018
1a1288a
Squeezer: rename "preview" to "view"
taleinat Aug 28, 2018
f753b37
Squeezer: remove tooltip configuration options (delay set to 80ms)
taleinat Aug 30, 2018
6087d3f
Squeezer: remove expand/view-last-squeezed events
taleinat Aug 30, 2018
b7d27cb
Squeezer: make view window non-modal and add horizontal scrollbar
taleinat Aug 30, 2018
ce07987
Merge remote-tracking branch 'origin/master' into pr_7626.
terryjreedy Sep 25, 2018
9676991
Correct error in merge resolution.
8000 terryjreedy Sep 25, 2018
e768e0f
Make test_squeezer runnable.
terryjreedy Sep 25, 2018
fcc9084
bpo-1529353: explicitly create root Tk() objects in tests
taleinat Sep 25, 2018
468d9ec
bpo-1529353: reformat doc-strings as PEP8 and rename test classes
taleinat Sep 25, 2018
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
Prev Previous commit
Next Next commit
ask for user confirmation before expanding huge squeezed outputs
  • Loading branch information
taleinat committed Aug 28, 2018
commit 45fb1a69581ffddb250a798d7b63e303567004e5
43 changes: 42 additions & 1 deletion Lib/idlelib/idle_test/test_squeezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def test_expand(self):
retval = expandingbutton.expand(event=Mock())
self.assertEqual(retval, None)

# check that the text was inserting into the text widget
# check that the text was inserted into the text widget
self.assertEqual(text_widget.get('1.0', 'end'), 'TEXT\n')

# check that the 'TAGS' tag was set on the inserted text
Expand All @@ -503,6 +503,47 @@ def test_expand(self):
self.assertEqual(squeezer.expandingbuttons.remove.call_count, 1)
squeezer.expandingbuttons.remove.assert_called_with(expandingbutton)

def test_expand_dangerous_oupput(self):
"""attempting to expand very long output asks user for confirmation"""
squeezer = self.make_mock_squeezer()
text = 'a' * 10**5
expandingbutton = ExpandingButton(text, 'TAGS', 30, squeezer)
expandingbutton.set_is_dangerous()
self.assertTrue(expandingbutton.is_dangerous)

# insert the button into the text widget
# (this is normally done by the Squeezer class)
text_widget = expandingbutton.text
text_widget.window_create("1.0", window=expandingbutton)

# set base_text to the text widget, so that changes are actually made
# to it (by ExpandingButton) and we can inspect these changes afterwards
expandingbutton.base_text = expandingbutton.text

# patch the message box module to always return False
with patch('idlelib.squeezer.tkMessageBox') as mock_msgbox:
mock_msgbox.askokcancel.return_value = False
mock_msgbox.askyesno.return_value = False

# trigger the expand event
retval = expandingbutton.expand(event=Mock())

# check that the event chain was broken and no text was inserted
self.assertEqual(retval, 'break')
self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), '')

# patch the message box module to always return True
with patch('idlelib.squeezer.tkMessageBox') as mock_msgbox:
mock_msgbox.askokcancel.return_value = True
mock_msgbox.askyesno.return_value = True

# trigger the expand event
retval = expandingbutton.expand(event=Mock())

# check that the event chain wasn't broken and the text was inserted
self.assertEqual(retval, None)
self.assertEqual(expandingbutton.text.get('1.0', 'end-1c'), text)

def test_copy(self):
"""test the copy event"""
# testing with the actual clipboard proved problematic, so this test
Expand Down
37 changes: 36 additions & 1 deletion Lib/idlelib/squeezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import tkinter as tk
from tkinter.font import Font
import tkinter.messagebox as tkMessageBox

from idlelib.config import idleConf
from idlelib.textview import view_text
Expand Down Expand Up @@ -106,6 +107,7 @@ class ExpandingButton(tk.Button):
def __init__(self, s, tags, numoflines, squeezer):
self.s = s
self.tags = tags
self.numoflines = numoflines
self.squeezer = squeezer
self.editwin = editwin = squeezer.editwin
self.text = text = editwin.text
Expand All @@ -114,7 +116,7 @@ def __init__(self, s, tags, numoflines, squeezer):
# before the iomark
self.base_text = editwin.per.bottom

button_text = "Squeezed text (%d lines)." % numoflines
button_text = "Squeezed text (%d lines)." % self.numoflines
tk.Button.__init__(self, text, text=button_text,
background="#FFFFC0", activebackground="#FFFFE0")

Expand All @@ -132,12 +134,45 @@ def __init__(self, s, tags, numoflines, squeezer):
self.selection_handle(
lambda offset, length: s[int(offset):int(offset) + int(length)])

self.is_dangerous = None
self.after_idle(self.set_is_dangerous)

def set_is_dangerous(self):
dangerous_line_len = 50 * self.text.winfo_width()
self.is_dangerous = (
self.numoflines > 1000 or
len(self.s) > 50000 or
any(
len(line_match.group(0)) >= dangerous_line_len
for line_match in re.finditer(r'[^\n]+', self.s)
)
)

def expand(self, event):
"""expand event handler

This inserts the original text in place of the button in the Text
widget, removes the button and updates the Squeezer instance.

If the original text is dangerously long, i.e. expanding it could
cause a performance degradation, ask the user for confirmation.
"""
if self.is_dangerous is None:
self.set_is_dangerous()
if self.is_dangerous:
confirm = tkMessageBox.askokcancel(
title="Expand huge output?",
message="\n\n".join([
"The squeezed output is very long: %d lines, %d chars.",
"Expanding it could make IDLE slow or unresponsive.",
"It is recommended to preview or copy the output instead.",
"Really expand?"
]) % (self.numoflines, len(self.s)),
default=tkMessageBox.CANCEL,
parent=self.text)
if not confirm:
return "break"

self.base_text.insert(self.text.index(self), self.s, self.tags)
self.base_text.delete(self)
self.squeezer.expandingbuttons.remove(self)
Expand Down
0