diff options
author | Brett Cannon <bcannon@gmail.com> | 2005-11-02 23:04:26 +0000 |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2005-11-02 23:04:26 +0000 |
commit | 4cf002c4831aed555b420d3a359dbbf8f6275d23 (patch) | |
tree | 7894237a96e85441866dbdf0fec20bd0deb50908 /Lib/_strptime.py | |
parent | 742b12e42b63a3df90310f6d43eba0a52dfd8f25 (diff) | |
download | cpython-4cf002c4831aed555b420d3a359dbbf8f6275d23.tar.gz |
Change time.strptime() to raise ValueError whenever there is an error in the
format string. Before exceptions generated by the internal code propagated up
to the user and were not helpful.
Closes bug #1340337.
Diffstat (limited to 'Lib/_strptime.py')
-rw-r--r-- | Lib/_strptime.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/_strptime.py b/Lib/_strptime.py index 08d79603a0..ce8525b716 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -287,7 +287,20 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"): _regex_cache.clear() format_regex = _regex_cache.get(format) if not format_regex: - format_regex = time_re.compile(format) + try: + format_regex = time_re.compile(format) + # KeyError raised when a bad format is found; can be specified as + # \\, in which case it was a stray % but with a space after it + except KeyError, err: + bad_directive = err.args[0] + if bad_directive == "\\": + bad_directive = "%" + del err + raise ValueError("'%s' is a bad directive in format '%s'" % + (bad_directive, format)) + # IndexError only occurs when the format string is "%" + except IndexError: + raise ValueError("stray %% in format '%s'" % format) _regex_cache[format] = format_regex finally: _cache_lock.release() |