diff options
author | Tim Peters <tim.peters@gmail.com> | 2020-05-11 21:19:20 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-11 21:19:20 -0500 |
commit | b1b4c790e7d3b5f4244450aefe3d8f01710c13f7 (patch) | |
tree | d0f03bf47219bdec01e64b56aed96df96ab427b1 /Lib/test/test_fnmatch.py | |
parent | d0919f0d6bb757b6bcfd7b2e15656d318c9d5cd9 (diff) | |
download | cpython-b1b4c790e7d3b5f4244450aefe3d8f01710c13f7.tar.gz cpython-b1b4c790e7d3b5f4244450aefe3d8f01710c13f7.zip |
bpo-40480: restore ability to join fnmatch.translate() results (GH-20049)
In translate(), generate unique group names across calls.
The restores the undocumented ability to get a valid regexp
by joining multiple translate() results via `|`.
Diffstat (limited to 'Lib/test/test_fnmatch.py')
-rw-r--r-- | Lib/test/test_fnmatch.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/Lib/test/test_fnmatch.py b/Lib/test/test_fnmatch.py index 4c173069503..10668e4f610 100644 --- a/Lib/test/test_fnmatch.py +++ b/Lib/test/test_fnmatch.py @@ -106,6 +106,7 @@ class FnmatchTestCase(unittest.TestCase): class TranslateTestCase(unittest.TestCase): def test_translate(self): + import re self.assertEqual(translate('*'), r'(?s:.*)\Z') self.assertEqual(translate('?'), r'(?s:.)\Z') self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\Z') @@ -122,9 +123,26 @@ class TranslateTestCase(unittest.TestCase): self.assertEqual(translate('*********A'), r'(?s:.*A)\Z') self.assertEqual(translate('A*********?[?]?'), r'(?s:A.*.[?].)\Z') # fancy translation to prevent exponential-time match failure - self.assertEqual(translate('**a*a****a'), - r'(?s:(?=(?P<g1>.*?a))(?P=g1)(?=(?P<g2>.*?a))(?P=g2).*a)\Z') - + t = translate('**a*a****a') + digits = re.findall(r'\d+', t) + self.assertEqual(len(digits), 4) + self.assertEqual(digits[0], digits[1]) + self.assertEqual(digits[2], digits[3]) + g1 = f"g{digits[0]}" # e.g., group name "g4" + g2 = f"g{digits[2]}" # e.g., group name "g5" + self.assertEqual(t, + fr'(?s:(?=(?P<{g1}>.*?a))(?P={g1})(?=(?P<{g2}>.*?a))(?P={g2}).*a)\Z') + # and try pasting multiple translate results - it's an undocumented + # feature that this works; all the pain of generating unique group + # names across calls exists to support this + r1 = translate('**a**a**a*') + r2 = translate('**b**b**b*') + r3 = translate('*c*c*c*') + fatre = "|".join([r1, r2, r3]) + self.assertTrue(re.match(fatre, 'abaccad')) + self.assertTrue(re.match(fatre, 'abxbcab')) + self.assertTrue(re.match(fatre, 'cbabcaxc')) + self.assertFalse(re.match(fatre, 'dabccbad')) class FilterTestCase(unittest.TestCase): |