Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2852ee2

Browse files
committed
Use suggestions
1 parent 6c6520b commit 2852ee2

4 files changed

Lines changed: 38 additions & 17 deletions

File tree

Doc/whatsnew/3.15.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ Summary --- release highlights
7070
New features
7171
============
7272

73-
.. tkinter.Text is not currently documented - being tracked in :gh:``135658``
74-
7573
.. _whatsnew315-sampling-profiler:
7674

7775
High frequency statistical sampling profiler
@@ -358,13 +356,13 @@ tarfile
358356
tkinter
359357
-------
360358

361-
* The ``tkinter.Text.search`` method now supports two additional
362-
arguments: ``nolinestop`` which allows the search to
359+
* The :meth:`!tkinter.Text.search` method now supports two additional
360+
arguments: *nolinestop* which allows the search to
363361
continue across line boundaries;
364-
and ``strictlimits`` which restricts the search to within the specified range.
362+
and *strictlimits* which restricts the search to within the specified range.
365363
(Contributed by Rihaan Meher in :gh:`130848`)
366364

367-
* A new method ``tkinter.Text.search_all`` has been introduced.
365+
* A new method :meth:`!tkinter.Text.search_all` has been introduced.
368366
This method allows for searching for all matches of a pattern
369367
using Tcl's ``-all`` and ``-overlap`` options.
370368
(Contributed by Rihaan Meher in :gh:`130848`)

Lib/test/test_tkinter/test_text.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ def test_debug(self):
2727
def test_search(self):
2828
text = self.text
2929

30+
# pattern and index are obligatory arguments.
3031
self.assertRaises(tkinter.TclError, text.search, None, '1.0')
3132
self.assertRaises(tkinter.TclError, text.search, 'a', None)
3233
self.assertRaises(tkinter.TclError, text.search, None, None)
34+
35+
# Invalid text index.
3336
self.assertRaises(tkinter.TclError, text.search, '', 0)
3437

38+
# Check if we are getting the indices as strings -- you are likely
39+
# to get Tcl_Obj under Tk 8.5 if Tkinter doesn't convert it.
3540
text.insert('1.0', 'hi-test')
3641
self.assertEqual(text.search('-test', '1.0', 'end'), '1.2')
3742
self.assertEqual(text.search('test', '1.0', 'end'), '1.3')
@@ -45,19 +50,37 @@ def test_search(self):
4550
result = text.search('line', '1.0', 'end', nolinestop=True, regexp=True)
4651
self.assertEqual(result, '2.8')
4752

48-
all_res = text.search_all('test', '1.0', 'end')
49-
self.assertIsInstance(all_res, tuple)
50-
self.assertGreaterEqual(len(all_res), 2)
51-
self.assertEqual(str(all_res[0]), '1.10')
52-
self.assertEqual(str(all_res[1]), '1.31')
53+
strict_res = text.search('test', '1.0', '1.20', strictlimits=True)
54+
self.assertEqual(strict_res, '1.10')
5355

56+
def test_search_all(self):
57+
text = self.text
58+
text.insert('1.0',
59+
'ababa ababa\n'
60+
'ababababa\n'
61+
'aba aba')
5462

55-
overlap_res = text.search_all('test', '1.0', 'end', overlap=True)
63+
all_res = text.search_all('aba', '1.0', 'end')
64+
all_res_strs = [str(i) for i in all_res]
65+
self.assertIsInstance(all_res, tuple)
66+
self.assertGreaterEqual(len(all_res), 3)
67+
self.assertEqual(str(all_res[0]), '1.0')
68+
self.assertEqual(str(all_res[1]), '1.6')
69+
70+
overlap_res = text.search_all('aba', '1.0', 'end', overlap=True)
71+
overlap_res_strs = [str(i) for i in overlap_res]
5672
self.assertIsInstance(overlap_res, tuple)
57-
self.assertGreaterEqual(len(overlap_res), len(all_res))
73+
self.assertGreater(len(overlap_res), len(all_res))
5874

59-
strict_res = text.search('test', '1.0', '1.20', strictlimits=True)
60-
self.assertEqual(strict_res, '1.10')
75+
# Check that overlap actually finds overlapping matches
76+
self.assertIn('2.0', overlap_res_strs)
77+
self.assertIn('2.2', overlap_res_strs)
78+
self.assertIn('2.4', overlap_res_strs)
79+
self.assertNotIn('2.2', all_res_strs)
80+
81+
# Ensure all results are valid text indices
82+
for i in overlap_res:
83+
self.assertRegex(str(i), r'^\d+\.\d+$')
6184

6285
def test_count(self):
6386
text = self.text

Lib/tkinter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4067,7 +4067,7 @@ def search_all(self, pattern, index, stopindex=None, *,
40674067
elide=None, nolinestop=None, overlap=None,
40684068
strictlimits=None):
40694069
"""Search all occurrences of PATTERN from INDEX to STOPINDEX.
4070-
Return a list of indices where matches begin."""
4070+
Return a tuple of indices where matches begin."""
40714071
args = [self._w, 'search', '-all']
40724072
if forwards: args.append('-forwards')
40734073
if backwards: args.append('-backwards')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add support for ``-nolinestop``, and ``-strictlimits`` options to ``!tkinter.Text.search``. Also add the ``tkinter.Text.search_all`` method for ``-all`` and ``overlap`` options
1+
Add support for ``-nolinestop``, and ``-strictlimits`` options to :meth:`!tkinter.Text.search`. Also add the :meth:`!tkinter.Text.search_all` method for ``-all`` and ``-overlap`` options.

0 commit comments

Comments
 (0)