summaryrefslogtreecommitdiffstats
path: root/Lib/idlelib/GrepDialog.py
diff options
context:
space:
mode:
authorTerry Jan Reedy <tjreedy@udel.edu>2013-06-22 22:37:34 (GMT)
committerTerry Jan Reedy <tjreedy@udel.edu>2013-06-22 22:37:34 (GMT)
commitb54b0ca02ab556392af315f8cff10fb477ba6928 (patch)
treec9f6c8c0f8a3eb309858c81157757f3b3f01a9fa /Lib/idlelib/GrepDialog.py
parentc77d9f38c2e1435c2d20af353abbb624f2767ba6 (diff)
parentde3beb2617bd9bf56326502ea5964ca809bce017 (diff)
downloadcpython-b54b0ca02ab556392af315f8cff10fb477ba6928.zip
cpython-b54b0ca02ab556392af315f8cff10fb477ba6928.tar.gz
cpython-b54b0ca02ab556392af315f8cff10fb477ba6928.tar.bz2
#18151 Merge from 3.3
Diffstat (limited to 'Lib/idlelib/GrepDialog.py')
-rw-r--r--Lib/idlelib/GrepDialog.py42
1 files changed, 18 insertions, 24 deletions
diff --git a/Lib/idlelib/GrepDialog.py b/Lib/idlelib/GrepDialog.py
index 1d8d51b..c0074e2 100644
--- a/Lib/idlelib/GrepDialog.py
+++ b/Lib/idlelib/GrepDialog.py
@@ -81,36 +81,24 @@ class GrepDialog(SearchDialogBase):
hits = 0
for fn in list:
try:
- f = open(fn, errors='replace')
+ with open(fn, errors='replace') 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 OSError as msg:
print(msg)
- continue
- lineno = 0
- while 1:
- block = f.readlines(100000)
- if not block:
- break
- for line in block:
- lineno = lineno + 1
- if line[-1:] == '\n':
- line = line[:-1]
- if prog.search(line):
- sys.stdout.write("%s: %s: %s\n" % (fn, lineno, line))
- hits = hits + 1
- if hits:
- if hits == 1:
- s = ""
- else:
- s = "s"
- print("Found", hits, "hit%s." % s)
- print("(Hint: right-click to open locations.)")
- else:
- print("No hits.")
+ print(("Hits found: %s\n"
+ "(Hint: right-click to open locations.)"
+ % hits) if hits else "No hits.")
def findfiles(self, dir, base, rec):
try:
names = os.listdir(dir or os.curdir)
- except OSError as msg:
+ except OSerror as msg:
print(msg)
return []
list = []
@@ -131,3 +119,9 @@ class GrepDialog(SearchDialogBase):
if self.top:
self.top.grab_release()
self.top.withdraw()
+
+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)