Skip to content

Test failure in test_find_periodic_pattern in string_tests.py #105639

Closed as duplicate

Description

@sweeneyde

A recent test failure:

FAIL: test_find_periodic_pattern (test.test_bytes.ByteArrayAsStringTest.test_find_periodic_pattern) (p='', text='')
Cover the special path for periodic patterns.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/string_tests.py", line 335, in test_find_periodic_pattern
    self.checkequal(reference_find(p, text),
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/string_tests.py", line 60, in checkequal
    self.assertEqual(
AssertionError: -1 != 0

The issue is reference_find:

        def reference_find(p, s):
            for i in range(len(s)):
                if s.startswith(p, i):
                    return i
            return -1

When s == p == '', we get for i in range(0), and then -1 is returned instead of the actual result which should be 0.

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    testsTests in the Lib/test dir
    on Jun 10, 2023
  2. terryjreedy commented on Jun 11, 2023

    @terryjreedy
    Member

    My immediate question was "What could have happened that this should suddenly fail?" The answer is "Nothing!" For each CI run, reference_find is called 1000 times with random input. There is obviously a non-zero probabilty of generating this corner case for which the test as a whole is wrong. Hence we get an occasional bogus failure. Some possible fixes:

    • Do not generate this corner case; I have no idea how right now.
    • Do not test this easy-to-detect case: if not p or not text: continue after text is generated, or perhaps if not p: continue after p is generated.
    • Change reference_find to return 0 for this case, making sure to not introduce another bug.

    The test was added in #86138, with PR by Dennis Sweeny and Tim Peters.

  3. hugovk commented on Mar 7, 2025

    @hugovk
    Member

    Thanks for the report, however it's been subsequently resolved by PR #110170 and issue #110160.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions