Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
919f0bc8
Commit
919f0bc8
authored
Oct 21, 2019
by
Serhiy Storchaka
Committed by
GitHub
Oct 21, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-38208: Simplify string.Template by using __init_subclass__(). (GH-16256)
parent
06cd5b6a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
24 deletions
+22
-24
Lib/string.py
Lib/string.py
+22
-24
No files found.
Lib/string.py
View file @
919f0bc8
...
...
@@ -54,30 +54,7 @@ from collections import ChainMap as _ChainMap
_sentinel_dict = {}
class _TemplateMetaclass(type):
pattern = r"""
%
(
delim
)
s
(
?
:
(
?
P
<
escaped
>%
(
delim
)
s
)
|
# Escape sequence of two delimiters
(
?
P
<
named
>%
(
id
)
s
)
|
# delimiter and a Python identifier
{(
?
P
<
braced
>%
(
bid
)
s
)}
|
# delimiter and a braced identifier
(
?
P
<
invalid
>
)
# Other ill-formed delimiter exprs
)
"""
def __init__(cls, name, bases, dct):
super(_TemplateMetaclass, cls).__init__(name, bases, dct)
if 'pattern' in dct:
pattern = cls.pattern
else:
pattern = _TemplateMetaclass.pattern % {
'delim' : _re.escape(cls.delimiter),
'id' : cls.idpattern,
'bid' : cls.braceidpattern or cls.idpattern,
}
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
class Template(metaclass=_TemplateMetaclass):
class Template:
"""
A
string
class
for
supporting
$
-
substitutions
.
"""
delimiter = '$'
...
...
@@ -89,6 +66,24 @@ class Template(metaclass=_TemplateMetaclass):
braceidpattern = None
flags = _re.IGNORECASE
def __init_subclass__(cls):
super().__init_subclass__()
if 'pattern' in cls.__dict__:
pattern = cls.pattern
else:
delim = _re.escape(cls.delimiter)
id = cls.idpattern
bid = cls.braceidpattern or cls.idpattern
pattern = fr"""
{
delim
}(
?
:
(
?
P
<
escaped
>
{
delim
})
|
# Escape sequence of two delimiters
(
?
P
<
named
>
{
id
})
|
# delimiter and a Python identifier
{{(
?
P
<
braced
>
{
bid
})}}
|
# delimiter and a braced identifier
(
?
P
<
invalid
>
)
# Other ill-formed delimiter exprs
)
"""
cls.pattern = _re.compile(pattern, cls.flags | _re.VERBOSE)
def __init__(self, template):
self.template = template
...
...
@@ -146,6 +141,9 @@ class Template(metaclass=_TemplateMetaclass):
self.pattern)
return self.pattern.sub(convert, self.template)
# Initialize Template.pattern. __init_subclass__() is automatically called
# only for subclasses, not for the Template class itself.
Template.__init_subclass__()
########################################################################
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment