[Python-checkins] cpython (2.7): Issue #21695: Catch AttributeError created when user closes grep output window

terry.reedy python-checkins at python.org
Tue Jun 10 08:50:17 CEST 2014


http://hg.python.org/cpython/rev/ec91ee7d9d8d
changeset:   91110:ec91ee7d9d8d
branch:      2.7
user:        Terry Jan Reedy <tjreedy at udel.edu>
date:        Tue Jun 10 02:49:29 2014 -0400
summary:
  Issue #21695: Catch AttributeError created when user closes grep output window
while still being written to. With no console, this closed Idle.
Also add missing import and a few other changes.

files:
  Lib/idlelib/GrepDialog.py |  54 +++++++++++++++-----------
  1 files changed, 31 insertions(+), 23 deletions(-)


diff --git a/Lib/idlelib/GrepDialog.py b/Lib/idlelib/GrepDialog.py
--- a/Lib/idlelib/GrepDialog.py
+++ b/Lib/idlelib/GrepDialog.py
@@ -1,9 +1,14 @@
 import os
 import fnmatch
+import re  # for htest
 import sys
-from Tkinter import *
+from Tkinter import StringVar, BooleanVar, Checkbutton  # for GrepDialog
+from Tkinter import Tk, Text, Button, SEL, END  # for htest
 from idlelib import SearchEngine
+import itertools
 from idlelib.SearchDialogBase import SearchDialogBase
+# Importing OutputWindow fails due to import loop
+# EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow
 
 def grep(text, io=None, flist=None):
     root = text._root()
@@ -63,7 +68,7 @@
         if not path:
             self.top.bell()
             return
-        from idlelib.OutputWindow import OutputWindow
+        from idlelib.OutputWindow import OutputWindow  # leave here!
         save = sys.stdout
         try:
             sys.stdout = OutputWindow(self.flist)
@@ -77,29 +82,34 @@
         list.sort()
         self.close()
         pat = self.engine.getpat()
-        print "Searching %r in %s ..." % (pat, path)
+        print("Searching %r in %s ..." % (pat, path))
         hits = 0
-        for fn in list:
-            try:
-                with open(fn) as f:
-                    for lineno, line in enumerate(f, 1):
-                        if line[-1:] == '\n':
-                            line = line[:-1]
-                        if prog.search(line):
-                            sys.stdout.write("%s: %s: %s\n" %
-                                             (fn, lineno, line))
-                            hits += 1
-            except IOError as msg:
-                print msg
-        print(("Hits found: %s\n"
-              "(Hint: right-click to open locations.)"
-              % hits) if hits else "No hits.")
+        try:
+            for fn in list:
+                try:
+                    with open(fn) as f:
+                        for lineno, line in enumerate(f, 1):
+                            if line[-1:] == '\n':
+                                line = line[:-1]
+                            if prog.search(line):
+                                sys.stdout.write("%s: %s: %s\n" %
+                                                 (fn, lineno, line))
+                                hits += 1
+                except IOError as msg:
+                    print(msg)
+            print(("Hits found: %s\n"
+                  "(Hint: right-click to open locations.)"
+                  % hits) if hits else "No hits.")
+        except AttributeError:
+            # Tk window has been closed, OutputWindow.text = None,
+            # so in OW.write, OW.text.insert fails.
+            pass
 
     def findfiles(self, dir, base, rec):
         try:
             names = os.listdir(dir or os.curdir)
         except os.error as msg:
-            print msg
+            print(msg)
             return []
         list = []
         subdirs = []
@@ -120,7 +130,8 @@
             self.top.grab_release()
             self.top.withdraw()
 
-def _grep_dialog(parent):
+
+def _grep_dialog(parent):  # for htest
     from idlelib.PyShell import PyShellFileList
     root = Tk()
     root.title("Test GrepDialog")
@@ -140,10 +151,7 @@
     button.pack()
     root.mainloop()
 
-
 if __name__ == "__main__":
-    # A human test is a bit tricky since EditorWindow() imports this module.
-    # Hence Idle must be restarted after editing this file for a live test.
     import unittest
     unittest.main('idlelib.idle_test.test_grep', verbosity=2, exit=False)
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list