From 953c6f508b43bd7d4532aeb4644829122f80ae2a Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 29 Aug 2003 02:28:54 +0000 Subject: [PATCH] Make sure parentheses are escaped when used in the format string. Closes bug #796149 . Will be backported. --- Lib/_strptime.py | 2 +- Lib/test/test_strptime.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/_strptime.py b/Lib/_strptime.py index 537296fb87b..85db89d49ab 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -249,7 +249,7 @@ class TimeRE(dict): processed_format = '' # The sub() call escapes all characters that might be misconstrued # as regex syntax. - regex_chars = re_compile(r"([\\.^$*+?{}\[\]|])") + regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])") format = regex_chars.sub(r"\\\1", format) whitespace_replacement = re_compile('\s+') format = whitespace_replacement.sub('\s*', format) diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py index 76bfd63af80..4dcc82a38a6 100644 --- a/Lib/test/test_strptime.py +++ b/Lib/test/test_strptime.py @@ -333,6 +333,15 @@ class StrptimeTests(unittest.TestCase): "Default values for strptime() are incorrect;" " %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): """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""