]> granicus.if.org Git - python/commitdiff
Make _strptime.TimeRE().pattern() use ``\s+`` for matching whitespace instead
authorBrett Cannon <bcannon@gmail.com>
Sun, 3 Jun 2007 23:13:41 +0000 (23:13 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sun, 3 Jun 2007 23:13:41 +0000 (23:13 +0000)
of ``\s*``.  This prevents patterns from "stealing" bits from other patterns in
order to make a match work.

Closes bug #1730389.  Will be backported.

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

index 9e7823abdd983952bd01215b716425484d9666f4..166cf82203656e57a5f2e128444db0d9b715dd3a 100644 (file)
@@ -250,7 +250,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 562033ed20faeddac05a3b23da8520519dd6197d..cf61b89f200c7e09710717c65805f8d2c6dfe250 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -222,6 +222,9 @@ Core and builtins
 Library
 -------
 
+- Bug #1730389: Change time.strptime() to use ``\s+`` instead of ``\s*`` when
+  matching spaces in the specified format argument.
+
 - SF 1668596/1720897: distutils now copies data files
   even if package_dir is empty.