Skip to content

Commit 363fab8

Browse files
authored
bpo-27115: Use Query subclass for IDLE editor Goto (GH-18871)
Replace tkinter tkSimpleDialog.askinteger with a standard IDLE query dialog. The new box checks for positivity before returning.
1 parent e7cab7f commit 363fab8

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

Lib/idlelib/NEWS.txt

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Released on 2020-10-05?
33
======================================
44

55

6+
bpo-27115: For 'Go to Line', use a Query entry box subclass with
7+
IDLE standard behavior and improved error checking.
8+
69
bpo-39885: Since clicking to get an IDLE context menu moves the
710
cursor, any text selection should be and now is cleared.
811

Lib/idlelib/editor.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -672,18 +672,16 @@ def replace_event(self, event):
672672

673673
def goto_line_event(self, event):
674674
text = self.text
675-
lineno = tkSimpleDialog.askinteger("Goto",
676-
"Go to line number:",parent=text)
677-
if lineno is None:
678-
return "break"
679-
if lineno <= 0:
680-
text.bell()
681-
return "break"
682-
683-
text.tag_remove("sel", "1.0", "end")
684-
text.mark_set("insert", f'{lineno}.0')
685-
text.see("insert")
686-
self.set_line_and_column()
675+
lineno = query.Goto(
676+
text, "Go To Line",
677+
"Enter a positive integer\n"
678+
"('big' = end of file):"
679+
).result
680+
if lineno is not None:
681+
text.tag_remove("sel", "1.0", "end")
682+
text.mark_set("insert", f'{lineno}.0')
683+
text.see("insert")
684+
self.set_line_and_column()
687685
return "break"
688686

689687
def open_module(self):

Lib/idlelib/idle_test/test_query.py

+43
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,33 @@ def test_good_module_name(self):
138138
self.assertEqual(dialog.entry_error['text'], '')
139139

140140

141+
class GotoTest(unittest.TestCase):
142+
"Test Goto subclass of Query."
143+
144+
class Dummy_ModuleName:
145+
entry_ok = query.Goto.entry_ok # Function being tested.
146+
def __init__(self, dummy_entry):
147+
self.entry = Var(value=dummy_entry)
148+
self.entry_error = {'text': ''}
149+
def showerror(self, message):
150+
self.entry_error['text'] = message
151+
152+
def test_bogus_goto(self):
153+
dialog = self.Dummy_ModuleName('a')
154+
self.assertEqual(dialog.entry_ok(), None)
155+
self.assertIn('not a base 10 integer', dialog.entry_error['text'])
156+
157+
def test_bad_goto(self):
158+
dialog = self.Dummy_ModuleName('0')
159+
self.assertEqual(dialog.entry_ok(), None)
160+
self.assertIn('not a positive integer', dialog.entry_error['text'])
161+
162+
def test_good_goto(self):
163+
dialog = self.Dummy_ModuleName('1')
164+
self.assertEqual(dialog.entry_ok(), 1)
165+
self.assertEqual(dialog.entry_error['text'], '')
166+
167+
141168
# 3 HelpSource test classes each test one method.
142169

143170
class HelpsourceBrowsefileTest(unittest.TestCase):
@@ -363,6 +390,22 @@ def test_click_module_name(self):
363390
root.destroy()
364391

365392

393+
class GotoGuiTest(unittest.TestCase):
394+
395+
@classmethod
396+
def setUpClass(cls):
397+
requires('gui')
398+
399+
def test_click_module_name(self):
400+
root = Tk()
401+
root.withdraw()
402+
dialog = query.Goto(root, 'T', 't', _utest=True)
403+
dialog.entry.insert(0, '22')
404+
dialog.button_ok.invoke()
405+
self.assertEqual(dialog.result, 22)
406+
root.destroy()
407+
408+
366409
class HelpsourceGuiTest(unittest.TestCase):
367410

368411
@classmethod

Lib/idlelib/query.py

+16
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ def entry_ok(self):
223223
return file_path
224224

225225

226+
class Goto(Query):
227+
"Get a positive line number for editor Go To Line."
228+
# Used in editor.EditorWindow.goto_line_event.
229+
230+
def entry_ok(self):
231+
try:
232+
lineno = int(self.entry.get())
233+
except ValueError:
234+
self.showerror('not a base 10 integer.')
235+
return None
236+
if lineno <= 0:
237+
self.showerror('not a positive integer.')
238+
return None
239+
return lineno
240+
241+
226242
class HelpSource(Query):
227243
"Get menu name and help source for Help menu."
228244
# Used in ConfigDialog.HelpListItemAdd/Edit, (941/9)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
For 'Go to Line', use a Query box subclass with IDLE standard behavior
2+
and improved error checking.

0 commit comments

Comments
 (0)