]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-80504: Always show the full search path in IDLE Find in Files (#152740)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 1 Jul 2026 17:05:56 +0000 (20:05 +0300)
committerGitHub <noreply@github.com>
Wed, 1 Jul 2026 17:05:56 +0000 (13:05 -0400)
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.

Lib/idlelib/grep.py
Lib/idlelib/idle_test/test_grep.py
Misc/NEWS.d/next/IDLE/2026-07-01-16-00-00.gh-issue-80504.gReP.rst [new file with mode: 0644]

index 42048ff2395fe1950d11547d05f896ff61c37a61..5d604faec23f32d06d276790b5aa6a660302dee8 100644 (file)
@@ -40,6 +40,20 @@ def grep(text, io=None, flist=None):
     dialog.open(text, searchphrase, io)
 
 
+def default_glob(path):
+    """Return the initial "In files:" pattern for a file path (gh-80504).
+
+    Always include a full directory so that grep output shows which
+    directory was searched.
+    """
+    dir, base = os.path.split(path)
+    dir = os.path.abspath(dir)  # An empty dir becomes the current directory.
+    head, tail = os.path.splitext(base)
+    if not tail:
+        tail = ".py"
+    return os.path.join(dir, "*" + tail)
+
+
 def walk_error(msg):
     "Handle os.walk error."
     print(msg)
@@ -103,11 +117,7 @@ class GrepDialog(SearchDialogBase):
             path = io.filename or ""
         else:
             path = ""
-        dir, base = os.path.split(path)
-        head, tail = os.path.splitext(base)
-        if not tail:
-            tail = ".py"
-        self.globvar.set(os.path.join(dir, "*" + tail))
+        self.globvar.set(default_glob(path))
 
     def create_entries(self):
         "Create base entry widgets and add widget for search path."
index d67dba76911fcfa96bc8826d1cfbf38135a6cce2..94f217effb656e1ae8c74ecc861e0b67389f6a22 100644 (file)
@@ -37,6 +37,25 @@ class Dummy_grep:
 _grep = Dummy_grep()
 
 
+class DefaultGlobTest(unittest.TestCase):
+
+    def test_no_path(self):
+        # gh-80504: an unsaved editor or the Shell has no path, so the
+        # pattern uses the current directory, not just '*.py'.
+        self.assertEqual(grep.default_glob(''),
+                         os.path.join(os.getcwd(), '*.py'))
+
+    def test_full_path(self):
+        path = os.path.join(os.path.abspath(os.sep), 'ab', 'foo.py')
+        self.assertEqual(grep.default_glob(path),
+                         os.path.join(os.path.dirname(path), '*.py'))
+
+    def test_other_extension(self):
+        path = os.path.join(os.path.abspath(os.sep), 'ab', 'foo.txt')
+        self.assertEqual(grep.default_glob(path),
+                         os.path.join(os.path.dirname(path), '*.txt'))
+
+
 class FindfilesTest(unittest.TestCase):
 
     @classmethod
diff --git a/Misc/NEWS.d/next/IDLE/2026-07-01-16-00-00.gh-issue-80504.gReP.rst b/Misc/NEWS.d/next/IDLE/2026-07-01-16-00-00.gh-issue-80504.gReP.rst
new file mode 100644 (file)
index 0000000..93adb33
--- /dev/null
@@ -0,0 +1,3 @@
+The "In files:" field of IDLE's Find in Files dialog now always contains a
+full directory path, even for an unsaved editor or the Shell.  This shows
+in the grep output which directory was searched.