From 0548983f6c8a56c77bffeeb6d93332453e426cb4 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 01:45:11 -0400 Subject: [PATCH 1/8] bpo-27115: Use Query subclass for IDLE editor Goto. This checks entry errors without closing the box. --- Lib/idlelib/query.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Lib/idlelib/query.py b/Lib/idlelib/query.py index 097e6e61e3569cf..4e5e61c3ab869d6 100644 --- a/Lib/idlelib/query.py +++ b/Lib/idlelib/query.py @@ -187,6 +187,23 @@ def entry_ok(self): return name +class Goto(Query): + "Get a positive line number for editor Go To Line." + # Used in editor.EditorWindow.goto_line_event. + + def __init__(self, parent, title, message, *, _htest=False, _utest=False): + super().__init__(parent, title, message, _htest=_htest, _utest=_utest) + + def entry_ok(self): + try: + lineno = int(self.entry.get()) + except ValueError: + self.showerror('not a base 10 integer.') + if lineno <= 0: + self.showerror('not a positive integer.') + return lineno + + class ModuleName(Query): "Get a module name for Open Module menu entry." # Used in open_module (editor.EditorWindow until move to iobinding). From 1faa4716ea118d275656106e6010bd3bceb42732 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 02:47:46 -0400 Subject: [PATCH 2/8] News items. --- Lib/idlelib/NEWS.txt | 3 +++ Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index b88b1e3cbffdc4a..fff5be328237659 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -3,6 +3,9 @@ Released on 2020-10-05? ====================================== +bpo-27115: For Edit Go to Line, use a Query entry box subclass with +IDLE standard behavior and improved error checking. + bpo-39885: Since clicking to get an IDLE context menu moves the cursor, any text selection should be and now is cleared. diff --git a/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst b/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst new file mode 100644 index 000000000000000..b03f115a1a6ad33 --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst @@ -0,0 +1,2 @@ +For Edit Go to Line, use a Query box subclass with IDLE standard behavior +and improved error checking. From 7573998d652431fbfc023e44412f7428414b57c8 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 02:48:25 -0400 Subject: [PATCH 3/8] Fix new box and use it. --- Lib/idlelib/editor.py | 13 +++++-------- Lib/idlelib/query.py | 2 ++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Lib/idlelib/editor.py b/Lib/idlelib/editor.py index 51941900d5c7414..67271c14eb840ff 100644 --- a/Lib/idlelib/editor.py +++ b/Lib/idlelib/editor.py @@ -672,14 +672,11 @@ def replace_event(self, event): def goto_line_event(self, event): text = self.text - lineno = tkSimpleDialog.askinteger("Goto", - "Go to line number:",parent=text) - if lineno is None: - return "break" - if lineno <= 0: - text.bell() - return "break" - + lineno = query.Goto( + text, "Go To Line", + "Enter a positive integer\n" + "('big' = end of file):" + ).result text.tag_remove("sel", "1.0", "end") text.mark_set("insert", f'{lineno}.0') text.see("insert") diff --git a/Lib/idlelib/query.py b/Lib/idlelib/query.py index 4e5e61c3ab869d6..22d3a9fb9d27d82 100644 --- a/Lib/idlelib/query.py +++ b/Lib/idlelib/query.py @@ -199,8 +199,10 @@ def entry_ok(self): lineno = int(self.entry.get()) except ValueError: self.showerror('not a base 10 integer.') + return None if lineno <= 0: self.showerror('not a positive integer.') + return None return lineno From 157ccf62c8b52b57eb323f93e398d4ae889301ca Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 02:52:25 -0400 Subject: [PATCH 4/8] Move new class. --- Lib/idlelib/query.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Lib/idlelib/query.py b/Lib/idlelib/query.py index 22d3a9fb9d27d82..8183ef38b1866a4 100644 --- a/Lib/idlelib/query.py +++ b/Lib/idlelib/query.py @@ -187,25 +187,6 @@ def entry_ok(self): return name -class Goto(Query): - "Get a positive line number for editor Go To Line." - # Used in editor.EditorWindow.goto_line_event. - - def __init__(self, parent, title, message, *, _htest=False, _utest=False): - super().__init__(parent, title, message, _htest=_htest, _utest=_utest) - - def entry_ok(self): - try: - lineno = int(self.entry.get()) - except ValueError: - self.showerror('not a base 10 integer.') - return None - if lineno <= 0: - self.showerror('not a positive integer.') - return None - return lineno - - class ModuleName(Query): "Get a module name for Open Module menu entry." # Used in open_module (editor.EditorWindow until move to iobinding). @@ -243,6 +224,25 @@ def entry_ok(self): return file_path +class Goto(Query): + "Get a positive line number for editor Go To Line." + # Used in editor.EditorWindow.goto_line_event. + + def __init__(self, parent, title, message, *, _htest=False, _utest=False): + super().__init__(parent, title, message, _htest=_htest, _utest=_utest) + + def entry_ok(self): + try: + lineno = int(self.entry.get()) + except ValueError: + self.showerror('not a base 10 integer.') + return None + if lineno <= 0: + self.showerror('not a positive integer.') + return None + return lineno + + class HelpSource(Query): "Get menu name and help source for Help menu." # Used in ConfigDialog.HelpListItemAdd/Edit, (941/9) From 68f2ee47de0ea3dcc6e79406fdfd44af67c4df3d Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 03:11:31 -0400 Subject: [PATCH 5/8] Add tests. --- Lib/idlelib/idle_test/test_query.py | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Lib/idlelib/idle_test/test_query.py b/Lib/idlelib/idle_test/test_query.py index f957585190dc83e..6d026cb5320683e 100644 --- a/Lib/idlelib/idle_test/test_query.py +++ b/Lib/idlelib/idle_test/test_query.py @@ -138,6 +138,33 @@ def test_good_module_name(self): self.assertEqual(dialog.entry_error['text'], '') +class GotoTest(unittest.TestCase): + "Test Goto subclass of Query." + + class Dummy_ModuleName: + entry_ok = query.Goto.entry_ok # Function being tested. + def __init__(self, dummy_entry): + self.entry = Var(value=dummy_entry) + self.entry_error = {'text': ''} + def showerror(self, message): + self.entry_error['text'] = message + + def test_bogus_goto(self): + dialog = self.Dummy_ModuleName('a') + self.assertEqual(dialog.entry_ok(), None) + self.assertIn('not a base 10 integer', dialog.entry_error['text']) + + def test_bad_goto(self): + dialog = self.Dummy_ModuleName('0') + self.assertEqual(dialog.entry_ok(), None) + self.assertIn('not a positive integer', dialog.entry_error['text']) + + def test_good_goto(self): + dialog = self.Dummy_ModuleName('1') + self.assertEqual(dialog.entry_ok(), 1) + self.assertEqual(dialog.entry_error['text'], '') + + # 3 HelpSource test classes each test one method. class HelpsourceBrowsefileTest(unittest.TestCase): @@ -363,6 +390,22 @@ def test_click_module_name(self): root.destroy() +class GotoGuiTest(unittest.TestCase): + + @classmethod + def setUpClass(cls): + requires('gui') + + def test_click_module_name(self): + root = Tk() + root.withdraw() + dialog = query.Goto(root, 'T', 't', _utest=True) + dialog.entry.insert(0, '22') + dialog.button_ok.invoke() + self.assertEqual(dialog.result, 22) + root.destroy() + + class HelpsourceGuiTest(unittest.TestCase): @classmethod From c70df53b66d61abb4054bf835e25738ff68a9923 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 15:33:50 -0400 Subject: [PATCH 6/8] Delete unneeded Goto.__init__. --- Lib/idlelib/query.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/idlelib/query.py b/Lib/idlelib/query.py index 8183ef38b1866a4..2c75e3cb6e801d9 100644 --- a/Lib/idlelib/query.py +++ b/Lib/idlelib/query.py @@ -228,9 +228,6 @@ class Goto(Query): "Get a positive line number for editor Go To Line." # Used in editor.EditorWindow.goto_line_event. - def __init__(self, parent, title, message, *, _htest=False, _utest=False): - super().__init__(parent, title, message, _htest=_htest, _utest=_utest) - def entry_ok(self): try: lineno = int(self.entry.get()) From 7409695561c4e13acfde755768c7435145285501 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 15:48:00 -0400 Subject: [PATCH 7/8] Skip goto if no lineno. --- Lib/idlelib/editor.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/idlelib/editor.py b/Lib/idlelib/editor.py index 67271c14eb840ff..b0f88b5463d1b6e 100644 --- a/Lib/idlelib/editor.py +++ b/Lib/idlelib/editor.py @@ -677,10 +677,11 @@ def goto_line_event(self, event): "Enter a positive integer\n" "('big' = end of file):" ).result - text.tag_remove("sel", "1.0", "end") - text.mark_set("insert", f'{lineno}.0') - text.see("insert") - self.set_line_and_column() + if lineno is not None: + text.tag_remove("sel", "1.0", "end") + text.mark_set("insert", f'{lineno}.0') + text.see("insert") + self.set_line_and_column() return "break" def open_module(self): From 769bf06da310cd6b3368d1b5b75b0d61899ea127 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 9 Mar 2020 15:57:04 -0400 Subject: [PATCH 8/8] Tweak news items. --- Lib/idlelib/NEWS.txt | 2 +- Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index fff5be328237659..3a4873b624f1e4c 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -3,7 +3,7 @@ Released on 2020-10-05? ====================================== -bpo-27115: For Edit Go to Line, use a Query entry box subclass with +bpo-27115: For 'Go to Line', use a Query entry box subclass with IDLE standard behavior and improved error checking. bpo-39885: Since clicking to get an IDLE context menu moves the diff --git a/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst b/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst index b03f115a1a6ad33..76af19e6014b4ed 100644 --- a/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst +++ b/Misc/NEWS.d/next/IDLE/2020-03-09-02-45-12.bpo-27115.8hSHMo.rst @@ -1,2 +1,2 @@ -For Edit Go to Line, use a Query box subclass with IDLE standard behavior +For 'Go to Line', use a Query box subclass with IDLE standard behavior and improved error checking.