Commit 2dc08149 authored by Brett Cannon's avatar Brett Cannon

Make sure parentheses are escaped when used in the format string.

Closes bug #796149 .  Will be backported.
parent 2811a839
...@@ -249,7 +249,7 @@ class TimeRE(dict): ...@@ -249,7 +249,7 @@ class TimeRE(dict):
processed_format = '' processed_format = ''
# The sub() call escapes all characters that might be misconstrued # The sub() call escapes all characters that might be misconstrued
# as regex syntax. # as regex syntax.
regex_chars = re_compile(r"([\\.^$*+?{}\[\]|])") regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
format = regex_chars.sub(r"\\\1", format) format = regex_chars.sub(r"\\\1", format)
whitespace_replacement = re_compile('\s+') whitespace_replacement = re_compile('\s+')
format = whitespace_replacement.sub('\s*', format) format = whitespace_replacement.sub('\s*', format)
......
...@@ -333,6 +333,15 @@ class StrptimeTests(unittest.TestCase): ...@@ -333,6 +333,15 @@ class StrptimeTests(unittest.TestCase):
"Default values for strptime() are incorrect;" "Default values for strptime() are incorrect;"
" %s != %s" % (strp_output, defaults)) " %s != %s" % (strp_output, defaults))
def test_escaping(self):
# Make sure all characters that have regex significance are escaped.
# Parentheses are in a purposeful order; will cause an error of
# unbalanced parentheses when the regex is compiled if they are not
# escaped.
# Test instigated by bug #796149 .
need_escaping = ".^$*+?{}\[]|)("
self.failUnless(_strptime.strptime(need_escaping, need_escaping))
class Strptime12AMPMTests(unittest.TestCase): class Strptime12AMPMTests(unittest.TestCase):
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)""" """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment