]> granicus.if.org Git - python/commitdiff
#17572: Avoid chained exceptions while passing bad directives to time.strptime()...
authorEzio Melotti <ezio.melotti@gmail.com>
Wed, 3 Apr 2013 23:09:20 +0000 (02:09 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Wed, 3 Apr 2013 23:09:20 +0000 (02:09 +0300)
Lib/_strptime.py
Lib/test/test_strptime.py
Lib/test/test_time.py
Misc/NEWS

index b0cd3d619e10f92092edd7e06d1c0608dd419343..b089b08541a43a305810cda1f34003d3a3c35729 100644 (file)
@@ -326,7 +326,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
                     bad_directive = "%"
                 del err
                 raise ValueError("'%s' is a bad directive in format '%s'" %
-                                    (bad_directive, format))
+                                    (bad_directive, format)) from None
             # IndexError only occurs when the format string is "%"
             except IndexError:
                 raise ValueError("stray %% in format '%s'" % format)
index 90aac5b3f6775f3c1a666d58d0d2eecf68a27e98..68e6a676363082c9ce74cf11e306b042ae870ce7 100644 (file)
@@ -218,6 +218,12 @@ class StrptimeTests(unittest.TestCase):
             else:
                 self.fail("'%s' did not raise ValueError" % bad_format)
 
+    def test_strptime_exception_context(self):
+        # check that this doesn't chain exceptions needlessly (see #17572)
+        with self.assertRaises(ValueError) as e:
+            _strptime._strptime_time('', '%D')
+        self.assertIs(e.exception.__suppress_context__, True)
+
     def test_unconverteddata(self):
         # Check ValueError is raised when there is unconverted data
         self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m")
index da0f555d916b9c97ff271823111569d6312841b1..0e4d70284bf9906d02af3e3e423cf8075f0775a2 100644 (file)
@@ -193,6 +193,12 @@ class TimeTestCase(unittest.TestCase):
         self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
         self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
 
+    def test_strptime_exception_context(self):
+        # check that this doesn't chain exceptions needlessly (see #17572)
+        with self.assertRaises(ValueError) as e:
+            time.strptime('', '%D')
+        self.assertIs(e.exception.__suppress_context__, True)
+
     def test_asctime(self):
         time.asctime(time.gmtime(self.t))
 
index 03f9e2f8f7e1ab41321cb4865659a0b560d7fa68..4405779a758e82117a6c58d04d2f22d1a6acff0f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17572: Avoid chained exceptions while passing bad directives to
+  time.strptime().  Initial patch by Claudiu Popa.
+
 - Issue #14254: IDLE now handles readline correctly across shell restarts.
 
 - Issue #17614: IDLE no longer raises exception when quickly closing a file.