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.
(cherry picked from commit 16a7e4a0b7)

Co-authored-by: Miro Hrončok <miro@hroncok.cz>
This commit is contained in:
Miss Islington (bot) 2022-05-24 23:32:20 -07:00 committed by GitHub
parent 7a5f190c9f
commit 74b205b3eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 65 additions and 5 deletions

View file

@ -129,7 +129,7 @@ import functools
# public symbols
__all__ = [
"match", "fullmatch", "search", "sub", "subn", "split",
"findall", "finditer", "compile", "purge", "escape",
"findall", "finditer", "compile", "purge", "template", "escape",
"error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U",
"ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
"UNICODE", "NOFLAG", "RegexFlag",
@ -148,6 +148,8 @@ 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 # unknown purpose, deprecated
DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation
__str__ = object.__str__
_numeric_repr_ = hex
@ -229,6 +231,18 @@ def purge():
_cache.clear()
_compile_repl.cache_clear()
def template(pattern, flags=0):
"Compile a template pattern, returning a Pattern object, deprecated"
import warnings
warnings.warn("The re.template() function is deprecated "
"as it is an undocumented function "
"without an obvious purpose. "
"Use re.compile() instead.",
DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning) # warn just once
return _compile(pattern, flags|T)
# SPECIAL_CHARS
# closing ')', '}' and ']'
# '-' (a range in character set)
@ -270,6 +284,13 @@ def _compile(pattern, flags):
return pattern
if not _compiler.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
if flags & T:
import warnings
warnings.warn("The re.TEMPLATE/re.T flag is deprecated "
"as it is an undocumented flag "
"without an obvious purpose. "
"Don't use it.",
DeprecationWarning)
p = _compiler.compile(pattern, flags)
if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE: