Commit 2b6dfec1 authored by Brett Cannon's avatar Brett Cannon

Raise a ValueError when there is data that was not covered in the format...

Raise a ValueError when there is data that was not covered in the format string.  Done to match behavior of pre-existing C-based strptime implementations.
parent a390c6e1
...@@ -423,6 +423,9 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"): ...@@ -423,6 +423,9 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
found = format_regex.match(data_string) found = format_regex.match(data_string)
if not found: if not found:
raise ValueError("time data did not match format") raise ValueError("time data did not match format")
if len(data_string) != found.end():
raise ValueError("unconverted data remains: %s" %
data_string[found.end():])
year = 1900 year = 1900
month = day = 1 month = day = 1
hour = minute = second = 0 hour = minute = second = 0
......
...@@ -227,6 +227,10 @@ class StrptimeTests(unittest.TestCase): ...@@ -227,6 +227,10 @@ class StrptimeTests(unittest.TestCase):
self.assertRaises(ValueError, _strptime.strptime, data_string="%d", self.assertRaises(ValueError, _strptime.strptime, data_string="%d",
format="%A") format="%A")
def test_unconverteddata(self):
# Check ValueError is raised when there is unconverted data
self.assertRaises(ValueError, _strptime.strptime, "10 12", "%m")
def helper(self, directive, position): def helper(self, directive, position):
"""Helper fxn in testing.""" """Helper fxn in testing."""
strf_output = time.strftime("%" + directive, self.time_tuple) strf_output = time.strftime("%" + directive, self.time_tuple)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment