diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2013-04-03 23:09:20 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2013-04-03 23:09:20 (GMT) |
commit | 0f38908684505905fd0890f4effbe6f683b39954 (patch) | |
tree | b38d916a07ecd6e6cefd10ccb0fea619f0e2d172 /Lib | |
parent | a99e1711f7899fa49dfde4cd6b7514ad172ddff2 (diff) | |
download | cpython-0f38908684505905fd0890f4effbe6f683b39954.zip cpython-0f38908684505905fd0890f4effbe6f683b39954.tar.gz cpython-0f38908684505905fd0890f4effbe6f683b39954.tar.bz2 |
#17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_strptime.py | 2 | ||||
-rw-r--r-- | Lib/test/test_strptime.py | 6 | ||||
-rw-r--r-- | Lib/test/test_time.py | 6 |
3 files changed, 13 insertions, 1 deletions
diff --git a/Lib/_strptime.py b/Lib/_strptime.py index b0cd3d6..b089b08 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -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) diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py index 90aac5b..68e6a67 100644 --- a/Lib/test/test_strptime.py +++ b/Lib/test/test_strptime.py @@ -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") diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index da0f555..0e4d702 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -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)) |