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: datetime.strftime fails on trivial format string
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: jaraco, rhettinger
Priority: normal Keywords:

Created on 2003-06-26 17:54 by jaraco, last changed 2022-04-10 16:09 by admin. This issue is now closed.

Messages (2)
msg16609 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2003-06-26 17:54
Simply put, strftime can't handle the empty string.

>>> import datetime
>>> now = datetime.datetime.utcnow()
>>> now.strftime( '%d' )
'26'
>>> now.strftime( '' )
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
SystemError: C:\Code\23b1
\Objects\stringobject.c:3315: bad argument to internal 
function

This is inconsistent with the docs and the time.strftime 
function.

>>> import time
>>> time.strftime( '', time.gmtime() )
''

Do I need to make any more justification for having the 
empty format string return an empty string?
msg16610 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-06-27 08:16
Logged In: YES 
user_id=80475

The interning of short strings violates the refcnt==1 
assumption for _PyString_Resize.

A simple fix is to boost the initial value of "totalnew" by 1. 
Combined with an NULL argument to 
PyString_FromStringAndSize, this assures that resulting 
format string is not interned.  This will remain true even if 
the implementation of PyString_FromStringAndSize changes 
because only the uninitialized strings that can be interned 
are those of zero length.

Fixed and added a test case.  See:
  Modules/datetimemodule.c 1.67
  Lib/test/test_datetime.py 1.45
History
Date User Action Args
2022-04-10 16:09:26adminsetgithub: 38716
2003-06-26 17:54:12jaracocreate