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

Appearance settings

Commit 919f0bc

Browse filesBrowse files
bpo-38208: Simplify string.Template by using __init_subclass__(). (GH-16256)
1 parent 06cd5b6 commit 919f0bc
Copy full SHA for 919f0bc

File tree

1 file changed

+22
-24
lines changed
Filter options

1 file changed

+22
-24
lines changed

‎Lib/string.py

Copy file name to clipboardExpand all lines: Lib/string.py
+22-24Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,7 @@ def capwords(s, sep=None):
5454

5555
_sentinel_dict = {}
5656

57-
class _TemplateMetaclass(type):
58-
pattern = r"""
59-
%(delim)s(?:
60-
(?P<escaped>%(delim)s) | # Escape sequence of two delimiters
61-
(?P<named>%(id)s) | # delimiter and a Python identifier
62-
{(?P<braced>%(bid)s)} | # delimiter and a braced identifier
63-
(?P<invalid>) # Other ill-formed delimiter exprs
64-
)
65-
"""
66-
67-
def __init__(cls, name, bases, dct):
68-
super(_TemplateMetaclass, cls).__init__(name, bases, dct)
69-
if 'pattern' in dct:
70-
pattern = cls.pattern
71-
else:
72-
pattern = _TemplateMetaclass.pattern % {
73-
'delim' : _re.escape(cls.delimiter),
74-
'id' : cls.idpattern,
75-
'bid' : cls.braceidpattern or cls.idpattern,
76-
}
77-
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
78-
79-
80-
class Template(metaclass=_TemplateMetaclass):
57+
class Template:
8158
"""A string class for supporting $-substitutions."""
8259

8360
delimiter = '$'
@@ -89,6 +66,24 @@ class Template(metaclass=_TemplateMetaclass):
8966
braceidpattern = None
9067
flags = _re.IGNORECASE
9168

69+
def __init_subclass__(cls):
70+
super().__init_subclass__()
71+
if 'pattern' in cls.__dict__:
72+
pattern = cls.pattern
73+
else:
74+
delim = _re.escape(cls.delimiter)
75+
id = cls.idpattern
76+
bid = cls.braceidpattern or cls.idpattern
77+
pattern = fr"""
78+
{delim}(?:
79+
(?P<escaped>{delim}) | # Escape sequence of two delimiters
80+
(?P<named>{id}) | # delimiter and a Python identifier
81+
{{(?P<braced>{bid})}} | # delimiter and a braced identifier
82+
(?P<invalid>) # Other ill-formed delimiter exprs
83+
)
84+
"""
85+
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
86+
9287
def __init__(self, template):
9388
self.template = template
9489

@@ -146,6 +141,9 @@ def convert(mo):
146141
self.pattern)
147142
return self.pattern.sub(convert, self.template)
148143

144+
# Initialize Template.pattern. __init_subclass__() is automatically called
145+
# only for subclasses, not for the Template class itself.
146+
Template.__init_subclass__()
149147

150148

151149
########################################################################

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.