gh-92728: Restore re.template, but deprecate it (GH-93161)

Revert "bpo-47211: Remove function re.template() and flag re.TEMPLATE (GH-32300)"

This reverts commit b09184bf05.
This commit is contained in:
Miro Hrončok 2022-05-25 08:05:35 +02:00 committed by GitHub
parent 08e4e887f2
commit 16a7e4a0b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 65 additions and 5 deletions

View file

@ -2386,6 +2386,30 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer
self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt"))
def test_template_function_and_flag_is_deprecated(self):
with self.assertWarns(DeprecationWarning) as cm:
template_re1 = re.template(r'a')
self.assertIn('re.template()', str(cm.warning))
self.assertIn('is deprecated', str(cm.warning))
self.assertIn('function', str(cm.warning))
self.assertNotIn('flag', str(cm.warning))
with self.assertWarns(DeprecationWarning) as cm:
# we deliberately use more flags here to test that that still
# triggers the warning
# if paranoid, we could test multiple different combinations,
# but it's probably not worth it
template_re2 = re.compile(r'a', flags=re.TEMPLATE|re.UNICODE)
self.assertIn('re.TEMPLATE', str(cm.warning))
self.assertIn('is deprecated', str(cm.warning))
self.assertIn('flag', str(cm.warning))
self.assertNotIn('function', str(cm.warning))
# while deprecated, is should still function
self.assertEqual(template_re1, template_re2)
self.assertTrue(template_re1.match('ahoy'))
self.assertFalse(template_re1.match('nope'))
def get_debug_out(pat):
with captured_stdout() as out:
@ -2580,11 +2604,11 @@ class PatternReprTests(unittest.TestCase):
"re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000")
self.assertEqual(
repr(~re.I),
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1")
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.TEMPLATE|re.DEBUG")
self.assertEqual(repr(~(re.I|re.S|re.X)),
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1")
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG")
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01")
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00")
class ImplementationTest(unittest.TestCase):