From: Brett Cannon Date: Fri, 29 Aug 2003 02:34:22 +0000 (+0000) Subject: _strptime.srptime() escaped parentheses in the format string properly. X-Git-Tag: v2.3.1~125 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3b47b812ca8aa9d0bf7f42e79948110d91023c3f;p=thirdparty%2FPython%2Fcpython.git _strptime.srptime() escaped parentheses in the format string properly. Closes bug #796149 . --- diff --git a/Lib/_strptime.py b/Lib/_strptime.py index b3db42e0f69a..1d05869f0d72 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -381,7 +381,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"([\\.^$*+?i\(\){}\[\]|])") 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 919f7416e8e4..869954c1ec58 100644 --- a/Lib/test/test_strptime.py +++ b/Lib/test/test_strptime.py @@ -383,6 +383,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)""" diff --git a/Misc/NEWS b/Misc/NEWS index 9c6152bb2e5e..b2d1a2d6f146 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -30,6 +30,9 @@ Extension modules Library ------- +- Bug #796149: time.strptime() now handles having parentheses in the + format string properly. + - The email package handles some RFC 2231 parameters with missing CHARSET fields better.