mirror of
https://github.com/python/cpython.git
synced 2025-12-10 02:50:09 +00:00
Make _strptime escape regex syntax in format string to prevent use in internal regex.
This commit is contained in:
parent
482c5f7eb7
commit
1e91d8eb03
2 changed files with 24 additions and 1 deletions
|
|
@ -373,8 +373,17 @@ class TimeRE(dict):
|
|||
return '%s)' % regex
|
||||
|
||||
def pattern(self, format):
|
||||
"""Return re pattern for the format string."""
|
||||
"""Return re pattern for the format string.
|
||||
|
||||
Need to make sure that any characters that might be interpreted as
|
||||
regex syntax is escaped.
|
||||
|
||||
"""
|
||||
processed_format = ''
|
||||
# The sub() call escapes all characters that might be misconstrued
|
||||
# as regex syntax.
|
||||
regex_chars = re_compile(r"([\\.^$*+?{}\[\]|])")
|
||||
format = regex_chars.sub(r"\\\1", format)
|
||||
whitespace_replacement = re_compile('\s+')
|
||||
format = whitespace_replacement.sub('\s*', format)
|
||||
while format.find('%') != -1:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue