gh-92728: Restore re.template, but deprecate it (GH-93161) · python/cpython@16a7e4a (original) (raw)
`@@ -129,7 +129,7 @@
`
129
129
`# public symbols
`
130
130
`all = [
`
131
131
`"match", "fullmatch", "search", "sub", "subn", "split",
`
132
``
`-
"findall", "finditer", "compile", "purge", "escape",
`
``
132
`+
"findall", "finditer", "compile", "purge", "template", "escape",
`
133
133
`"error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U",
`
134
134
`"ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
`
135
135
`"UNICODE", "NOFLAG", "RegexFlag",
`
`@@ -148,6 +148,8 @@ class RegexFlag:
`
148
148
`MULTILINE = M = _compiler.SRE_FLAG_MULTILINE # make anchors look for newline
`
149
149
`DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline
`
150
150
`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 # unknown purpose, deprecated
`
151
153
`DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation
`
152
154
`str = object.str
`
153
155
`numeric_repr = hex
`
`@@ -229,6 +231,18 @@ def purge():
`
229
231
`_cache.clear()
`
230
232
`_compile_repl.cache_clear()
`
231
233
``
``
234
`+
def template(pattern, flags=0):
`
``
235
`+
"Compile a template pattern, returning a Pattern object, deprecated"
`
``
236
`+
import warnings
`
``
237
`+
warnings.warn("The re.template() function is deprecated "
`
``
238
`+
"as it is an undocumented function "
`
``
239
`+
"without an obvious purpose. "
`
``
240
`+
"Use re.compile() instead.",
`
``
241
`+
DeprecationWarning)
`
``
242
`+
with warnings.catch_warnings():
`
``
243
`+
warnings.simplefilter("ignore", DeprecationWarning) # warn just once
`
``
244
`+
return _compile(pattern, flags|T)
`
``
245
+
232
246
`# SPECIAL_CHARS
`
233
247
`# closing ')', '}' and ']'
`
234
248
`# '-' (a range in character set)
`
`@@ -270,6 +284,13 @@ def _compile(pattern, flags):
`
270
284
`return pattern
`
271
285
`if not _compiler.isstring(pattern):
`
272
286
`raise TypeError("first argument must be string or compiled pattern")
`
``
287
`+
if flags & T:
`
``
288
`+
import warnings
`
``
289
`+
warnings.warn("The re.TEMPLATE/re.T flag is deprecated "
`
``
290
`+
"as it is an undocumented flag "
`
``
291
`+
"without an obvious purpose. "
`
``
292
`+
"Don't use it.",
`
``
293
`+
DeprecationWarning)
`
273
294
`p = _compiler.compile(pattern, flags)
`
274
295
`if not (flags & DEBUG):
`
275
296
`if len(_cache) >= _MAXCACHE:
`