Issue #19545: Avoid chained exceptions while passing stray % to

time.strptime().  Initial patch by Claudiu Popa.
This commit is contained in:
Serhiy Storchaka 2013-11-24 18:17:11 +02:00
commit b5d386314f
4 changed files with 12 additions and 1 deletions

View 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:

View file

@ -223,6 +223,10 @@ def test_strptime_exception_context(self):
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

View file

@ -198,6 +198,10 @@ def test_strptime_exception_context(self):
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))

View file

@ -68,6 +68,9 @@ Core and Builtins
Library
-------
- Issue #19545: Avoid chained exceptions while passing stray % to
time.strptime(). Initial patch by Claudiu Popa.
- Issue #3158: doctest can now find doctests in functions and methods
written in C.