Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 953c6f5

Browse files
committed
Make sure parentheses are escaped when used in the format string.
Closes bug #796149 . Will be backported.
1 parent 6676f6e commit 953c6f5

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/_strptime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def pattern(self, format):
249249
processed_format = ''
250250
# The sub() call escapes all characters that might be misconstrued
251251
# as regex syntax.
252-
regex_chars = re_compile(r"([\\.^$*+?{}\[\]|])")
252+
regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
253253
format = regex_chars.sub(r"\\\1", format)
254254
whitespace_replacement = re_compile('\s+')
255255
format = whitespace_replacement.sub('\s*', format)

Lib/test/test_strptime.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ def test_defaults(self):
333333
"Default values for strptime() are incorrect;"
334334
" %s != %s" % (strp_output, defaults))
335335

336+
def test_escaping(self):
337+
# Make sure all characters that have regex significance are escaped.
338+
# Parentheses are in a purposeful order; will cause an error of
339+
# unbalanced parentheses when the regex is compiled if they are not
340+
# escaped.
341+
# Test instigated by bug #796149 .
342+
need_escaping = ".^$*+?{}\[]|)("
343+
self.failUnless(_strptime.strptime(need_escaping, need_escaping))
344+
336345
class Strptime12AMPMTests(unittest.TestCase):
337346
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
338347

0 commit comments

Comments
 (0)