]> granicus.if.org Git - python/commitdiff
Backport of r55752: make time.strptime() behave better when whitespace is in
authorBrett Cannon <bcannon@gmail.com>
Mon, 4 Jun 2007 00:14:06 +0000 (00:14 +0000)
committerBrett Cannon <bcannon@gmail.com>
Mon, 4 Jun 2007 00:14:06 +0000 (00:14 +0000)
the format arguments.

Lib/_strptime.py
Lib/test/test_strptime.py
Misc/NEWS

index 476f9d8a36a0b26d06851c5720bd9e8c75552151..8b3e64f27afa52746bbe9ad081e83c200d63dce7 100644 (file)
@@ -253,7 +253,7 @@ class TimeRE(dict):
         regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
         format = regex_chars.sub(r"\\\1", format)
         whitespace_replacement = re_compile('\s+')
-        format = whitespace_replacement.sub('\s*', format)
+        format = whitespace_replacement.sub('\s+', format)
         while '%' in format:
             directive_index = format.index('%')+1
             processed_format = "%s%s%s" % (processed_format,
index f4747521bff6c08f3407f11f535e386ed8c2aa5f..92c722ab1551ed65f7cec2314ee9236030d45e0f 100644 (file)
@@ -190,6 +190,15 @@ class TimeRETests(unittest.TestCase):
                         "locale data that contains regex metacharacters is not"
                         " properly escaped")
 
+    def test_whitespace_substitution(self):
+        # When pattern contains whitespace, make sure it is taken into account
+        # so as to not allow to subpatterns to end up next to each other and
+        # "steal" characters from each other.
+        pattern = self.time_re.pattern('%j %H')
+        self.failUnless(not re.match(pattern, "180"))
+        self.failUnless(re.match(pattern, "18 0"))
+
+
 class StrptimeTests(unittest.TestCase):
     """Tests for _strptime.strptime."""
 
index a397e4d9fe62a2f53f3bde4ad135854e665d6541..10bf25c93db656e69fb8eb02030e3611b0e7ba4d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.5.2c1?
 Library
 -------
 
+- Bug #1730389: Have time.strptime() match spaces in a format argument with
+  ``\s+`` instead of ``\s*``.
+
 - SF 1668596/1720897: distutils now copies data files
   even if package_dir is empty.