]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Change time.strptime() to raise ValueError whenever there is an error in the
authorBrett Cannon <bcannon@gmail.com>
Wed, 2 Nov 2005 23:04:26 +0000 (23:04 +0000)
committerBrett Cannon <bcannon@gmail.com>
Wed, 2 Nov 2005 23:04:26 +0000 (23:04 +0000)
format string.  Before exceptions generated by the internal code propagated up
to the user and were not helpful.

Closes bug #1340337.

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

index 08d79603a01688baed19241b8d9b59e0c6d13ec4..ce8525b7169ba39e1e1f981d7ccd738fe947dd57 100644 (file)
@@ -287,7 +287,20 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
             _regex_cache.clear()
         format_regex = _regex_cache.get(format)
         if not format_regex:
-            format_regex = time_re.compile(format)
+            try:
+                format_regex = time_re.compile(format)
+            # KeyError raised when a bad format is found; can be specified as
+            # \\, in which case it was a stray % but with a space after it
+            except KeyError, err:
+                bad_directive = err.args[0]
+                if bad_directive == "\\":
+                    bad_directive = "%"
+                del err
+                raise ValueError("'%s' is a bad directive in format '%s'" %
+                                    (bad_directive, format))
+            # IndexError only occurs when the format string is "%"
+            except IndexError:
+                raise ValueError("stray %% in format '%s'" % format)
             _regex_cache[format] = format_regex
     finally:
         _cache_lock.release()
index f9763aa34c2c0c8a3adc4e901886cffbc497dc06..ba65649857ead0b028f7eb00dea10c822ed97c60 100644 (file)
@@ -197,10 +197,20 @@ class StrptimeTests(unittest.TestCase):
         """Create testing time tuple."""
         self.time_tuple = time.gmtime()
 
-    def test_TypeError(self):
-        # Make sure ValueError is raised when match fails
+    def test_ValueError(self):
+        # Make sure ValueError is raised when match fails or format is bad
         self.assertRaises(ValueError, _strptime.strptime, data_string="%d",
                           format="%A")
+        for bad_format in ("%", "% ", "%e"):
+            try:
+                _strptime.strptime("2005", bad_format)
+            except ValueError:
+                continue
+            except Exception, err:
+                self.fail("'%s' raised %s, not ValueError" %
+                            (bad_format, err.__class__.__name__))
+            else:
+                self.fail("'%s' did not raise ValueError" % bad_format)
 
     def test_unconverteddata(self):
         # Check ValueError is raised when there is unconverted data
index 9b3f3814bda1c1f35604c9eef8014dba8129d699..59f165a61e3a9c2840d56ceb300eaff461f84d46 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -276,6 +276,9 @@ Extension Modules
 Library
 -------
 
+- Bug #1340337: change time.strptime() to always return ValueError when there
+  is an error in the format string.
+
 - Patch #754022: Greatly enhanced webbrowser.py (by Oleg Broytmann).
 
 - Bug #729103: pydoc.py: Fix docother() method to accept additional