]> granicus.if.org Git - python/commitdiff
Issue #19545: Avoid chained exceptions while passing stray % to
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 24 Nov 2013 16:15:37 +0000 (18:15 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 24 Nov 2013 16:15:37 +0000 (18:15 +0200)
time.strptime().  Initial patch by Claudiu Popa.

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

index 264d4fa2ed48133497a3f32890ed8bf97d3a9130..9058a6986152d65a9647681c16715dbf967b2fbd 100644 (file)
@@ -329,7 +329,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
                                     (bad_directive, format)) from None
             # IndexError only occurs when the format string is "%"
             except IndexError:
-                raise ValueError("stray %% in format '%s'" % format)
+                raise ValueError("stray %% in format '%s'" % format) from None
             _regex_cache[format] = format_regex
     found = format_regex.match(data_string)
     if not found:
index 68e6a676363082c9ce74cf11e306b042ae870ce7..d5bc39d0dd3862a25ca2109666006437f1106889 100644 (file)
@@ -223,6 +223,10 @@ class StrptimeTests(unittest.TestCase):
         with self.assertRaises(ValueError) as e:
             _strptime._strptime_time('', '%D')
         self.assertIs(e.exception.__suppress_context__, True)
+        # additional check for IndexError branch (issue #19545)
+        with self.assertRaises(ValueError) as e:
+            _strptime._strptime_time('19', '%Y %')
+        self.assertIs(e.exception.__suppress_context__, True)
 
     def test_unconverteddata(self):
         # Check ValueError is raised when there is unconverted data
index faf9779a4732dafccaa0f386c87c84ee672a43c6..d8d6029ea5a117db483569ca98579941c43e9255 100644 (file)
@@ -198,6 +198,10 @@ class TimeTestCase(unittest.TestCase):
         with self.assertRaises(ValueError) as e:
             time.strptime('', '%D')
         self.assertIs(e.exception.__suppress_context__, True)
+        # additional check for IndexError branch (issue #19545)
+        with self.assertRaises(ValueError) as e:
+            time.strptime('19', '%Y %')
+        self.assertIs(e.exception.__suppress_context__, True)
 
     def test_asctime(self):
         time.asctime(time.gmtime(self.t))
index 2a8057ad8018a391cce9697a3ee201ba5f2fd6fa..fd53f6259eab68a88aac1301c84f5ec17298564d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #19545: Avoid chained exceptions while passing stray % to
+  time.strptime().  Initial patch by Claudiu Popa.
+
 - Issue #19633: Fixed writing not compressed 16- and 32-bit wave files on
   big-endian platforms.