From b09184bf05b07b77c5ecfedd4daa846be3cbf0a9 Mon Sep 17 00:00:00 2001
From: Serhiy Storchaka <storchaka@gmail.com>
Date: Wed, 6 Apr 2022 19:53:50 +0300
Subject: [PATCH] bpo-47211: Remove function re.template() and flag re.TEMPLATE
 (GH-32300)

They were undocumented and never working.
---
 Lib/re/__init__.py                                        | 8 +-------
 Lib/re/_compiler.py                                       | 2 --
 Lib/re/_constants.py                                      | 2 --
 Lib/re/_parser.py                                         | 3 +--
 Lib/test/test_re.py                                       | 6 +++---
 .../next/Library/2022-04-04-11-58-07.bpo-47211.W4GFkB.rst | 2 ++
 Modules/_sre/sre.c                                        | 1 -
 Modules/_sre/sre_constants.h                              | 1 -
 8 files changed, 7 insertions(+), 18 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Library/2022-04-04-11-58-07.bpo-47211.W4GFkB.rst

diff --git a/Lib/re/__init__.py b/Lib/re/__init__.py
index b887722bbb2cdf..c9b511422f1e59 100644
--- a/Lib/re/__init__.py
+++ b/Lib/re/__init__.py
@@ -129,7 +129,7 @@
 # public symbols
 __all__ = [
     "match", "fullmatch", "search", "sub", "subn", "split",
-    "findall", "finditer", "compile", "purge", "template", "escape",
+    "findall", "finditer", "compile", "purge", "escape",
     "error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U",
     "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
     "UNICODE", "NOFLAG", "RegexFlag",
@@ -148,8 +148,6 @@ class RegexFlag:
     MULTILINE = M = _compiler.SRE_FLAG_MULTILINE # make anchors look for newline
     DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline
     VERBOSE = X = _compiler.SRE_FLAG_VERBOSE # ignore whitespace and comments
-    # sre extensions (experimental, don't rely on these)
-    TEMPLATE = T = _compiler.SRE_FLAG_TEMPLATE # disable backtracking
     DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation
     __str__ = object.__str__
     _numeric_repr_ = hex
@@ -231,10 +229,6 @@ def purge():
     _cache.clear()
     _compile_repl.cache_clear()
 
-def template(pattern, flags=0):
-    "Compile a template pattern, returning a Pattern object"
-    return _compile(pattern, flags|T)
-
 # SPECIAL_CHARS
 # closing ')', '}' and ']'
 # '-' (a range in character set)
diff --git a/Lib/re/_compiler.py b/Lib/re/_compiler.py
index bedd4b8f400164..5b4c4a3f30a40c 100644
--- a/Lib/re/_compiler.py
+++ b/Lib/re/_compiler.py
@@ -147,8 +147,6 @@ def _compile(data, pattern, flags):
             else:
                 emit(ANY)
         elif op in REPEATING_CODES:
-            if flags & SRE_FLAG_TEMPLATE:
-                raise error("internal: unsupported template operator %r" % (op,))
             if _simple(av[2]):
                 emit(REPEATING_CODES[op][2])
                 skip = _len(code); emit(0)
diff --git a/Lib/re/_constants.py b/Lib/re/_constants.py
index 327ba548118b3b..4c7e93e67ec607 100644
--- a/Lib/re/_constants.py
+++ b/Lib/re/_constants.py
@@ -202,7 +202,6 @@ def _makecodes(names):
 }
 
 # flags
-SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
 SRE_FLAG_IGNORECASE = 2 # case insensitive
 SRE_FLAG_LOCALE = 4 # honour system locale
 SRE_FLAG_MULTILINE = 8 # treat target as multiline string
@@ -245,7 +244,6 @@ def dump(f, d, prefix):
         dump(f, ATCODES, "SRE")
         dump(f, CHCODES, "SRE")
 
-        f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
         f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
         f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
         f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
diff --git a/Lib/re/_parser.py b/Lib/re/_parser.py
index ae44118564eb7d..eca2ffc28b45b1 100644
--- a/Lib/re/_parser.py
+++ b/Lib/re/_parser.py
@@ -61,12 +61,11 @@
     "x": SRE_FLAG_VERBOSE,
     # extensions
     "a": SRE_FLAG_ASCII,
-    "t": SRE_FLAG_TEMPLATE,
     "u": SRE_FLAG_UNICODE,
 }
 
 TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE
-GLOBAL_FLAGS = SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE
+GLOBAL_FLAGS = SRE_FLAG_DEBUG
 
 class Verbose(Exception):
     pass
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 553eb4cfe85b69..959582e2f12575 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -2432,11 +2432,11 @@ def test_flags_repr(self):
                          "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.TEMPLATE|re.DEBUG")
+                "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1")
         self.assertEqual(repr(~(re.I|re.S|re.X)),
-                         "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG")
+                         "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1")
         self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
-                         "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00")
+                         "re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01")
 
 
 class ImplementationTest(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2022-04-04-11-58-07.bpo-47211.W4GFkB.rst b/Misc/NEWS.d/next/Library/2022-04-04-11-58-07.bpo-47211.W4GFkB.rst
new file mode 100644
index 00000000000000..0bd5d1619ff784
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-04-11-58-07.bpo-47211.W4GFkB.rst
@@ -0,0 +1,2 @@
+Remove undocumented and never working function ``re.template()`` and flag
+``re.TEMPLATE``.
diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c
index 491734f243849b..bd9204da428af8 100644
--- a/Modules/_sre/sre.c
+++ b/Modules/_sre/sre.c
@@ -1323,7 +1323,6 @@ pattern_repr(PatternObject *obj)
         const char *name;
         int value;
     } flag_names[] = {
-        {"re.TEMPLATE", SRE_FLAG_TEMPLATE},
         {"re.IGNORECASE", SRE_FLAG_IGNORECASE},
         {"re.LOCALE", SRE_FLAG_LOCALE},
         {"re.MULTILINE", SRE_FLAG_MULTILINE},
diff --git a/Modules/_sre/sre_constants.h b/Modules/_sre/sre_constants.h
index 3e3643144a92c4..e53fa392ec5852 100644
--- a/Modules/_sre/sre_constants.h
+++ b/Modules/_sre/sre_constants.h
@@ -86,7 +86,6 @@
 #define SRE_CATEGORY_UNI_NOT_WORD 15
 #define SRE_CATEGORY_UNI_LINEBREAK 16
 #define SRE_CATEGORY_UNI_NOT_LINEBREAK 17
-#define SRE_FLAG_TEMPLATE 1
 #define SRE_FLAG_IGNORECASE 2
 #define SRE_FLAG_LOCALE 4
 #define SRE_FLAG_MULTILINE 8