Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update string, test_str* from cpython 3.10.6
  • Loading branch information
CPython Developers authored and youknowone committed Aug 14, 2022
commit 17e12dea1ec11b871a9e5d3a4a26e2f9ad83fde2
46 changes: 22 additions & 24 deletions Lib/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,7 @@ def capwords(s, sep=None):

_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 = '$'
Expand All @@ -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

Expand Down Expand Up @@ -146,6 +141,9 @@ def convert(mo):
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__()


########################################################################
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_strftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def strftest1(self, now):
)

for e in expectations:
# musn't raise a value error
# mustn't raise a value error
try:
result = time.strftime(e[0], now)
except ValueError as error:
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_string_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def byte(i):

class TestLiterals(unittest.TestCase):

from test.support.warnings_helper import check_syntax_warning

def setUp(self):
self.save_path = sys.path[:]
self.tmpdir = tempfile.mkdtemp()
Expand Down Expand Up @@ -133,6 +131,7 @@ def test_eval_str_invalid_escape(self):
self.assertEqual(w, [])
self.assertEqual(exc.filename, '<string>')
self.assertEqual(exc.lineno, 1)
self.assertEqual(exc.offset, 1)

# TODO: RUSTPYTHON
@unittest.expectedFailure
Expand Down