This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: time.strptime() with bad % code throws bad exception
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, steveha
Priority: normal Keywords:

Created on 2005-10-28 06:48 by steveha, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg26753 - (view) Author: Steve R. Hastings (steveha) * Date: 2005-10-28 06:48
I am using Python 2.4.2, the latest version currently
available for the system I am using, which is Ubuntu
5.10 for x86.

I was calling the time.strptime() function, and I had
accidentally typed an extra % symbol in the time format
string.  If you do this you get a baffling message in
the traceback.  The deepest frame of the traceback
looks like this:

  File "/usr/lib/python2.4/_strptime.py", line 256, in
pattern
    processed_format = "%s%s%s" % (processed_format,
KeyError: '-'


This means that I accidentally put "%-" in my format
string.

What happens if I accidentally put "% " in my format
string?  This:

  File "/usr/lib/python2.4/_strptime.py", line 256, in
pattern
    processed_format = "%s%s%s" % (processed_format,
KeyError: '\\'

It turns out that spaces in the format string are
replaced by r'\s*' by the time the exception happens. 
It really freaked me out to see Python complaining
about a backslash, when I hadn't put a backslash into
my format string.

I propose that this library call should be re-written
to catch the exception, and then throw a more sensible
exception, including a statement like: "unknown % code
in format string".  In a perfect world this would be
done in such a way that the last frame appearing in the
traceback would be for the call to time.strptime(), and
not show any lines from the guts of time.strptime()
about regular expression compiling.
msg26754 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2005-11-02 23:05
Logged In: YES 
user_id=357491

Rev. 41376 has the fix in the trunk.  I am not going to
backport since it is a semantic change.

Thanks, Steve.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42533
2005-10-28 06:48:14stevehacreate