]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
SF bug #761337: datetime.strftime fails on trivial format string
authorRaymond Hettinger <python@rcn.com>
Fri, 27 Jun 2003 08:14:17 +0000 (08:14 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 27 Jun 2003 08:14:17 +0000 (08:14 +0000)
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.

Added a test case.

Lib/test/test_datetime.py
Modules/datetimemodule.c

index cca0c9d9c59f96333e568e8552d68bf1bd20a38f..c6dbb4895624f3451002b9dfb2bd7210a08122be 100644 (file)
@@ -831,6 +831,7 @@ class TestDate(HarmlessMixedComparison):
     def test_strftime(self):
         t = self.theclass(2005, 3, 2)
         self.assertEqual(t.strftime("m:%m d:%d y:%y"), "m:03 d:02 y:05")
+        self.assertEqual(t.strftime(""), "") # SF bug #761337
 
         self.assertRaises(TypeError, t.strftime) # needs an arg
         self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
index 164492e3b2715f2944afdc4d8e73cb4f2768d508..d8aed17f32e9675734584988efe784ddff04067c 100644 (file)
@@ -1175,7 +1175,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
         * a new format.  Since computing the replacements for those codes
         * is expensive, don't unless they're actually used.
         */
-       totalnew = PyString_Size(format);       /* realistic if no %z/%Z */
+       totalnew = PyString_Size(format) + 1;   /* realistic if no %z/%Z */
        newfmt = PyString_FromStringAndSize(NULL, totalnew);
        if (newfmt == NULL) goto Done;
        pnew = PyString_AsString(newfmt);