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 17e12de

Browse filesBrowse files
CPython Developersyouknowone
CPython Developers
authored andcommitted
Update string, test_str* from cpython 3.10.6
1 parent 14e86e1 commit 17e12de
Copy full SHA for 17e12de

File tree

Expand file treeCollapse file tree

3 files changed

+24
-27
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+24
-27
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
########################################################################

‎Lib/test/test_strftime.py

Copy file name to clipboardExpand all lines: Lib/test/test_strftime.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def strftest1(self, now):
115115
)
116116

117117
for e in expectations:
118-
# musn't raise a value error
118+
# mustn't raise a value error
119119
try:
120120
result = time.strftime(e[0], now)
121121
except ValueError as error:

‎Lib/test/test_string_literals.py

Copy file name to clipboardExpand all lines: Lib/test/test_string_literals.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ def byte(i):
6363

6464
class TestLiterals(unittest.TestCase):
6565

66-
from test.support.warnings_helper import check_syntax_warning
67-
6866
def setUp(self):
6967
self.save_path = sys.path[:]
7068
self.tmpdir = tempfile.mkdtemp()
@@ -133,6 +131,7 @@ def test_eval_str_invalid_escape(self):
133131
self.assertEqual(w, [])
134132
self.assertEqual(exc.filename, '<string>')
135133
self.assertEqual(exc.lineno, 1)
134+
self.assertEqual(exc.offset, 1)
136135

137136
# TODO: RUSTPYTHON
138137
@unittest.expectedFailure

0 commit comments

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