Skip to content

Commit 5708818

Browse files
[3.14] gh-80504: Always show the full search path in IDLE Find in Files (GH-152740) (#152811)
gh-80504: Always show the full search path in IDLE Find in Files (GH-152740) The "In files:" field of the Find in Files dialog now always contains a full directory path, so the grep output shows which directory was searched. (cherry picked from commit f21f338) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent f128daa commit 5708818

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

Lib/idlelib/grep.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def grep(text, io=None, flist=None):
4040
dialog.open(text, searchphrase, io)
4141

4242

43+
def default_glob(path):
44+
"""Return the initial "In files:" pattern for a file path (gh-80504).
45+
46+
Always include a full directory so that grep output shows which
47+
directory was searched.
48+
"""
49+
dir, base = os.path.split(path)
50+
dir = os.path.abspath(dir) # An empty dir becomes the current directory.
51+
head, tail = os.path.splitext(base)
52+
if not tail:
53+
tail = ".py"
54+
return os.path.join(dir, "*" + tail)
55+
56+
4357
def walk_error(msg):
4458
"Handle os.walk error."
4559
print(msg)
@@ -103,11 +117,7 @@ def open(self, text, searchphrase, io=None):
103117
path = io.filename or ""
104118
else:
105119
path = ""
106-
dir, base = os.path.split(path)
107-
head, tail = os.path.splitext(base)
108-
if not tail:
109-
tail = ".py"
110-
self.globvar.set(os.path.join(dir, "*" + tail))
120+
self.globvar.set(default_glob(path))
111121

112122
def create_entries(self):
113123
"Create base entry widgets and add widget for search path."

Lib/idlelib/idle_test/test_grep.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ def close(self): # gui method
3737
_grep = Dummy_grep()
3838

3939

40+
class DefaultGlobTest(unittest.TestCase):
41+
42+
def test_no_path(self):
43+
# gh-80504: an unsaved editor or the Shell has no path, so the
44+
# pattern uses the current directory, not just '*.py'.
45+
self.assertEqual(grep.default_glob(''),
46+
os.path.join(os.getcwd(), '*.py'))
47+
48+
def test_full_path(self):
49+
path = os.path.join(os.path.abspath(os.sep), 'ab', 'foo.py')
50+
self.assertEqual(grep.default_glob(path),
51+
os.path.join(os.path.dirname(path), '*.py'))
52+
53+
def test_other_extension(self):
54+
path = os.path.join(os.path.abspath(os.sep), 'ab', 'foo.txt')
55+
self.assertEqual(grep.default_glob(path),
56+
os.path.join(os.path.dirname(path), '*.txt'))
57+
58+
4059
class FindfilesTest(unittest.TestCase):
4160

4261
@classmethod
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The "In files:" field of IDLE's Find in Files dialog now always contains a
2+
full directory path, even for an unsaved editor or the Shell. This shows
3+
in the grep output which directory was searched.

0 commit comments

Comments
 (0)