Skip to content

Commit 86084d0

Browse files
vstinnersobolevn
andauthored
[3.11] gh-110160: Fix flaky test_find_periodic_pattern in string_tests (… (#110183)
gh-110160: Fix flaky `test_find_periodic_pattern` in `string_tests` (#110170) (cherry picked from commit 06faa9a) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 0914b13 commit 86084d0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Lib/test/string_tests.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,12 @@ def reference_find(p, s):
327327
for i in range(len(s)):
328328
if s.startswith(p, i):
329329
return i
330+
if p == '' and s == '':
331+
return 0
330332
return -1
331333

332-
rr = random.randrange
333-
choices = random.choices
334-
for _ in range(1000):
334+
def check_pattern(rr):
335+
choices = random.choices
335336
p0 = ''.join(choices('abcde', k=rr(10))) * rr(10, 20)
336337
p = p0[:len(p0) - rr(10)] # pop off some characters
337338
left = ''.join(choices('abcdef', k=rr(2000)))
@@ -341,6 +342,13 @@ def reference_find(p, s):
341342
self.checkequal(reference_find(p, text),
342343
text, 'find', p)
343344

345+
rr = random.randrange
346+
for _ in range(1000):
347+
check_pattern(rr)
348+
349+
# Test that empty string always work:
350+
check_pattern(lambda *args: 0)
351+
344352
def test_find_shift_table_overflow(self):
345353
"""When the table of 8-bit shifts overflows."""
346354
N = 2**8 + 100

0 commit comments

Comments
 (0)