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)
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."
_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
--- /dev/null
+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.