[Python-checkins] cpython (3.3): Issue #19545: Avoid chained exceptions while passing stray % to

serhiy.storchaka python-checkins at python.org
Sun Nov 24 17:17:45 CET 2013


http://hg.python.org/cpython/rev/ce1578f4d105
changeset:   87510:ce1578f4d105
branch:      3.3
parent:      87508:27347b955d4a
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Nov 24 18:15:37 2013 +0200
summary:
  Issue #19545: Avoid chained exceptions while passing stray % to
time.strptime().  Initial patch by Claudiu Popa.

files:
  Lib/_strptime.py          |  2 +-
  Lib/test/test_strptime.py |  4 ++++
  Lib/test/test_time.py     |  4 ++++
  Misc/NEWS                 |  3 +++
  4 files changed, 12 insertions(+), 1 deletions(-)


diff --git a/Lib/_strptime.py b/Lib/_strptime.py
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -329,7 +329,7 @@
                                     (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:
diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py
--- a/Lib/test/test_strptime.py
+++ b/Lib/test/test_strptime.py
@@ -223,6 +223,10 @@
         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
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -198,6 +198,10 @@
         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))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list