Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16a7e4a

Browse files
authored
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 commitb09184b.
1 parent08e4e88 commit16a7e4a

File tree

10 files changed

+65
-5
lines changed

10 files changed

+65
-5
lines changed

‎Doc/whatsnew/3.11.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,11 @@ Deprecated
12731273
is now deprecated. Support will be removed in Python 3.13. (Contributed by
12741274
Jingchen Ye in:gh:`90224`.)
12751275

1276+
* The:func:`re.template` function and the corresponding:const:`re.TEMPLATE`
1277+
and:const:`re.T` flags are deprecated, as they were undocumented and
1278+
lacked an obvious purpose. They will be removed in Python 3.13.
1279+
(Contributed by Serhiy Storchaka and Miro Hrončok in:gh:`92728`.)
1280+
12761281

12771282
Pending Removal in Python 3.12
12781283
==============================

‎Lib/re/__init__.py

Lines changed: 22 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# unknown purpose, deprecated
151153
DEBUG=_compiler.SRE_FLAG_DEBUG# dump pattern after compilation
152154
__str__=object.__str__
153155
_numeric_repr_=hex
@@ -229,6 +231,18 @@ def purge():
229231
_cache.clear()
230232
_compile_repl.cache_clear()
231233

234+
deftemplate(pattern,flags=0):
235+
"Compile a template pattern, returning a Pattern object, deprecated"
236+
importwarnings
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+
withwarnings.catch_warnings():
243+
warnings.simplefilter("ignore",DeprecationWarning)# warn just once
244+
return_compile(pattern,flags|T)
245+
232246
# SPECIAL_CHARS
233247
# closing ')', '}' and ']'
234248
# '-' (a range in character set)
@@ -270,6 +284,13 @@ def _compile(pattern, flags):
270284
returnpattern
271285
ifnot_compiler.isstring(pattern):
272286
raiseTypeError("first argument must be string or compiled pattern")
287+
ifflags&T:
288+
importwarnings
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)
273294
p=_compiler.compile(pattern,flags)
274295
ifnot (flags&DEBUG):
275296
iflen(_cache)>=_MAXCACHE:

‎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
elifopinREPEATING_CODES:
111+
ifflags&SRE_FLAG_TEMPLATE:
112+
raiseerror("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 (unknown purpose, deprecated)
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
classState:
7172
# keeps track of state for parsing

‎Lib/test/test_re.py

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

2389+
deftest_template_function_and_flag_is_deprecated(self):
2390+
withself.assertWarns(DeprecationWarning)ascm:
2391+
template_re1=re.template(r'a')
2392+
self.assertIn('re.template()',str(cm.warning))
2393+
self.assertIn('is deprecated',str(cm.warning))
2394+
self.assertIn('function',str(cm.warning))
2395+
self.assertNotIn('flag',str(cm.warning))
2396+
2397+
withself.assertWarns(DeprecationWarning)ascm:
2398+
# we deliberately use more flags here to test that that still
2399+
# triggers the warning
2400+
# if paranoid, we could test multiple different combinations,
2401+
# but it's probably not worth it
2402+
template_re2=re.compile(r'a',flags=re.TEMPLATE|re.UNICODE)
2403+
self.assertIn('re.TEMPLATE',str(cm.warning))
2404+
self.assertIn('is deprecated',str(cm.warning))
2405+
self.assertIn('flag',str(cm.warning))
2406+
self.assertNotIn('function',str(cm.warning))
2407+
2408+
# while deprecated, is should still function
2409+
self.assertEqual(template_re1,template_re2)
2410+
self.assertTrue(template_re1.match('ahoy'))
2411+
self.assertFalse(template_re1.match('nope'))
2412+
23892413

23902414
defget_debug_out(pat):
23912415
withcaptured_stdout()asout:
@@ -2580,11 +2604,11 @@ def test_flags_repr(self):
25802604
"re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000")
25812605
self.assertEqual(
25822606
repr(~re.I),
2583-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1")
2607+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.TEMPLATE|re.DEBUG")
25842608
self.assertEqual(repr(~(re.I|re.S|re.X)),
2585-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1")
2609+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG")
25862610
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))),
2587-
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01")
2611+
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.TEMPLATE|re.DEBUG|0xffe00")
25882612

25892613

25902614
classImplementationTest(unittest.TestCase):

‎Misc/NEWS.d/3.11.0b1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ Suppress expression chaining for more :mod:`re` parsing errors.
13731373
13741374
Remove undocumented and never working function ``re.template()`` and flag
13751375
``re.TEMPLATE``.
1376+
This was later reverted in 3.11.0b2 and deprecated instead.
13761377

13771378
..
13781379
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The:func:`re.template` function and the corresponding:const:`re.TEMPLATE`
2+
and:const:`re.T` flags are restored after they were removed in 3.11.0b1,
3+
but they are now deprecated, so they might be removed from Python 3.13.

‎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
constchar*name;
13241324
intvalue;
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
#defineSRE_CATEGORY_UNI_NOT_WORD 15
8686
#defineSRE_CATEGORY_UNI_LINEBREAK 16
8787
#defineSRE_CATEGORY_UNI_NOT_LINEBREAK 17
88+
#defineSRE_FLAG_TEMPLATE 1
8889
#defineSRE_FLAG_IGNORECASE 2
8990
#defineSRE_FLAG_LOCALE 4
9091
#defineSRE_FLAG_MULTILINE 8

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp