Skip to content

Commit 2e8f218

Browse files
committed
Revert "bpo-47211: Remove function re.template() and flag re.TEMPLATE (pythonGH-32300)"
This reverts commit b09184b. Fixes python#92728
1 parent 7108bdf commit 2e8f218

File tree

7 files changed

+17
-5
lines changed

7 files changed

+17
-5
lines changed

Lib/re/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
# public symbols
130130
__all__ = [
131131
"match", "fullmatch", "search", "sub", "subn", "split",
132-
"findall", "finditer", "compile", "purge", "escape",
132+
"findall", "finditer", "compile", "purge", "template", "escape",
133133
"error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U",
134134
"ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
135135
"UNICODE", "NOFLAG", "RegexFlag",
@@ -148,6 +148,8 @@ class RegexFlag:
148148
MULTILINE = M = _compiler.SRE_FLAG_MULTILINE # make anchors look for newline
149149
DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline
150150
VERBOSE = X = _compiler.SRE_FLAG_VERBOSE # ignore whitespace and comments
151+
# sre extensions (experimental, don't rely on these)
152+
TEMPLATE = T = _compiler.SRE_FLAG_TEMPLATE # disable backtracking
151153
DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation
152154
__str__ = object.__str__
153155
_numeric_repr_ = hex
@@ -229,6 +231,10 @@ def purge():
229231
_cache.clear()
230232
_compile_repl.cache_clear()
231233

234+
def template(pattern, flags=0):
235+
"Compile a template pattern, returning a Pattern object"
236+
return _compile(pattern, flags|T)
237+
232238
# SPECIAL_CHARS
233239
# closing ')', '}' and ']'
234240
# '-' (a range in character set)

Lib/re/_compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def _compile(data, pattern, flags):
108108
else:
109109
emit(ANY)
110110
elif op in REPEATING_CODES:
111+
if flags & SRE_FLAG_TEMPLATE:
112+
raise error("internal: unsupported template operator %r" % (op,))
111113
if _simple(av[2]):
112114
emit(REPEATING_CODES[op][2])
113115
skip = _len(code); emit(0)

Lib/re/_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def _makecodes(*names):
204204
}
205205

206206
# flags
207+
SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
207208
SRE_FLAG_IGNORECASE = 2 # case insensitive
208209
SRE_FLAG_LOCALE = 4 # honour system locale
209210
SRE_FLAG_MULTILINE = 8 # treat target as multiline string

Lib/re/_parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@
6161
"x": SRE_FLAG_VERBOSE,
6262
# extensions
6363
"a": SRE_FLAG_ASCII,
64+
"t": SRE_FLAG_TEMPLATE,
6465
"u": SRE_FLAG_UNICODE,
6566
}
6667

6768
TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE
68-
GLOBAL_FLAGS = SRE_FLAG_DEBUG
69+
GLOBAL_FLAGS = SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE
6970

7071
class State:
7172
# keeps track of state for parsing

Lib/test/test_re.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,11 +2580,11 @@ def test_flags_repr(self):
25802580
"re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000")
25812581
self.assertEqual(
25822582
repr(~re.I),
2583-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1")
2583+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.TEMPLATE|re.DEBUG")
25842584
self.assertEqual(repr(~(re.I|re.S|re.X)),
2585-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1")
2585+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG")
25862586
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
2587-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01")
2587+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00")
25882588

25892589

25902590
class ImplementationTest(unittest.TestCase):

Modules/_sre/sre.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,7 @@ pattern_repr(PatternObject *obj)
13231323
const char *name;
13241324
int value;
13251325
} flag_names[] = {
1326+
{"re.TEMPLATE", SRE_FLAG_TEMPLATE},
13261327
{"re.IGNORECASE", SRE_FLAG_IGNORECASE},
13271328
{"re.LOCALE", SRE_FLAG_LOCALE},
13281329
{"re.MULTILINE", SRE_FLAG_MULTILINE},

Modules/_sre/sre_constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#define SRE_CATEGORY_UNI_NOT_WORD 15
8686
#define SRE_CATEGORY_UNI_LINEBREAK 16
8787
#define SRE_CATEGORY_UNI_NOT_LINEBREAK 17
88+
#define SRE_FLAG_TEMPLATE 1
8889
#define SRE_FLAG_IGNORECASE 2
8990
#define SRE_FLAG_LOCALE 4
9091
#define SRE_FLAG_MULTILINE 8

0 commit comments

Comments
 (0)