Skip to content

Commit 67f69db

Browse files
authored
gh-105687: Remove deprecated objects from re module (#105688)
1 parent fb655e0 commit 67f69db

File tree

9 files changed

+13
-56
lines changed

9 files changed

+13
-56
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ Removed
340340
attribute instead.
341341
(Contributed by Nikita Sobolev in :gh:`105546`.)
342342

343+
* Remove undocumented, never working, and deprecated ``re.template`` function
344+
and ``re.TEMPLATE`` flag (and ``re.T`` alias).
345+
(Contributed by Serhiy Storchaka and Nikita Sobolev in :gh:`105687`.)
346+
343347

344348
Porting to Python 3.13
345349
======================

Lib/re/__init__.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
# public symbols
131131
__all__ = [
132132
"match", "fullmatch", "search", "sub", "subn", "split",
133-
"findall", "finditer", "compile", "purge", "template", "escape",
133+
"findall", "finditer", "compile", "purge", "escape",
134134
"error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U",
135135
"ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
136136
"UNICODE", "NOFLAG", "RegexFlag",
@@ -150,7 +150,6 @@ class RegexFlag:
150150
DOTALL = S = _compiler.SRE_FLAG_DOTALL # make dot match newline
151151
VERBOSE = X = _compiler.SRE_FLAG_VERBOSE # ignore whitespace and comments
152152
# sre extensions (experimental, don't rely on these)
153-
TEMPLATE = T = _compiler.SRE_FLAG_TEMPLATE # unknown purpose, deprecated
154153
DEBUG = _compiler.SRE_FLAG_DEBUG # dump pattern after compilation
155154
__str__ = object.__str__
156155
_numeric_repr_ = hex
@@ -233,17 +232,6 @@ def purge():
233232
_cache2.clear()
234233
_compile_template.cache_clear()
235234

236-
def template(pattern, flags=0):
237-
"Compile a template pattern, returning a Pattern object, deprecated"
238-
import warnings
239-
warnings.warn("The re.template() function is deprecated "
240-
"as it is an undocumented function "
241-
"without an obvious purpose. "
242-
"Use re.compile() instead.",
243-
DeprecationWarning)
244-
with warnings.catch_warnings():
245-
warnings.simplefilter("ignore", DeprecationWarning) # warn just once
246-
return _compile(pattern, flags|T)
247235

248236
# SPECIAL_CHARS
249237
# closing ')', '}' and ']'
@@ -297,13 +285,6 @@ def _compile(pattern, flags):
297285
return pattern
298286
if not _compiler.isstring(pattern):
299287
raise TypeError("first argument must be string or compiled pattern")
300-
if flags & T:
301-
import warnings
302-
warnings.warn("The re.TEMPLATE/re.T flag is deprecated "
303-
"as it is an undocumented flag "
304-
"without an obvious purpose. "
305-
"Don't use it.",
306-
DeprecationWarning)
307288
p = _compiler.compile(pattern, flags)
308289
if flags & DEBUG:
309290
return p

Lib/re/_compiler.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ def _compile(code, pattern, flags):
101101
else:
102102
emit(ANY)
103103
elif op in REPEATING_CODES:
104-
if flags & SRE_FLAG_TEMPLATE:
105-
raise error("internal: unsupported template operator %r" % (op,))
106104
if _simple(av[2]):
107105
emit(REPEATING_CODES[op][2])
108106
skip = _len(code); emit(0)

Lib/re/_constants.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
# update when constants are added or removed
1515

16-
MAGIC = 20221023
16+
MAGIC = 20230612
1717

1818
from _sre import MAXREPEAT, MAXGROUPS
1919

@@ -204,7 +204,6 @@ def _makecodes(*names):
204204
}
205205

206206
# flags
207-
SRE_FLAG_TEMPLATE = 1 # template mode (unknown purpose, deprecated)
208207
SRE_FLAG_IGNORECASE = 2 # case insensitive
209208
SRE_FLAG_LOCALE = 4 # honour system locale
210209
SRE_FLAG_MULTILINE = 8 # treat target as multiline string

Lib/re/_parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@
6161
"x": SRE_FLAG_VERBOSE,
6262
# extensions
6363
"a": SRE_FLAG_ASCII,
64-
"t": SRE_FLAG_TEMPLATE,
6564
"u": SRE_FLAG_UNICODE,
6665
}
6766

6867
TYPE_FLAGS = SRE_FLAG_ASCII | SRE_FLAG_LOCALE | SRE_FLAG_UNICODE
69-
GLOBAL_FLAGS = SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE
68+
GLOBAL_FLAGS = SRE_FLAG_DEBUG
7069

7170
class State:
7271
# keeps track of state for parsing

Lib/test/test_re.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,30 +2398,6 @@ def test_bug_gh91616(self):
23982398
self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\Z', "a.txt")) # reproducer
23992399
self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\Z', "a.txt"))
24002400

2401-
def test_template_function_and_flag_is_deprecated(self):
2402-
with self.assertWarns(DeprecationWarning) as cm:
2403-
template_re1 = re.template(r'a')
2404-
self.assertIn('re.template()', str(cm.warning))
2405-
self.assertIn('is deprecated', str(cm.warning))
2406-
self.assertIn('function', str(cm.warning))
2407-
self.assertNotIn('flag', str(cm.warning))
2408-
2409-
with self.assertWarns(DeprecationWarning) as cm:
2410-
# we deliberately use more flags here to test that that still
2411-
# triggers the warning
2412-
# if paranoid, we could test multiple different combinations,
2413-
# but it's probably not worth it
2414-
template_re2 = re.compile(r'a', flags=re.TEMPLATE|re.UNICODE)
2415-
self.assertIn('re.TEMPLATE', str(cm.warning))
2416-
self.assertIn('is deprecated', str(cm.warning))
2417-
self.assertIn('flag', str(cm.warning))
2418-
self.assertNotIn('function', str(cm.warning))
2419-
2420-
# while deprecated, is should still function
2421-
self.assertEqual(template_re1, template_re2)
2422-
self.assertTrue(template_re1.match('ahoy'))
2423-
self.assertFalse(template_re1.match('nope'))
2424-
24252401
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing')
24262402
def test_regression_gh94675(self):
24272403
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*'
@@ -2615,11 +2591,11 @@ def test_flags_repr(self):
26152591
"re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000")
26162592
self.assertEqual(
26172593
repr(~re.I),
2618-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.TEMPLATE|re.DEBUG")
2594+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1")
26192595
self.assertEqual(repr(~(re.I|re.S|re.X)),
2620-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG")
2596+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1")
26212597
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
2622-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00")
2598+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01")
26232599

26242600

26252601
class ImplementationTest(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove deprecated ``re.template``, ``re.T``, ``re.TEMPLATE``,
2+
``sre_constans.SRE_FLAG_TEMPLATE``.

Modules/_sre/sre.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,6 @@ pattern_repr(PatternObject *obj)
13351335
const char *name;
13361336
int value;
13371337
} flag_names[] = {
1338-
{"re.TEMPLATE", SRE_FLAG_TEMPLATE},
13391338
{"re.IGNORECASE", SRE_FLAG_IGNORECASE},
13401339
{"re.LOCALE", SRE_FLAG_LOCALE},
13411340
{"re.MULTILINE", SRE_FLAG_MULTILINE},

Modules/_sre/sre_constants.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* See the sre.c file for information on usage and redistribution.
1212
*/
1313

14-
#define SRE_MAGIC 20221023
14+
#define SRE_MAGIC 20230612
1515
#define SRE_OP_FAILURE 0
1616
#define SRE_OP_SUCCESS 1
1717
#define SRE_OP_ANY 2
@@ -85,7 +85,6 @@
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
8988
#define SRE_FLAG_IGNORECASE 2
9089
#define SRE_FLAG_LOCALE 4
9190
#define SRE_FLAG_MULTILINE 8

0 commit comments

Comments
 (0)