Index: Modules/timemodule.c =================================================================== --- Modules/timemodule.c (revision 69496) +++ Modules/timemodule.c (working copy) @@ -508,9 +508,11 @@ return NULL; } - /* Convert the unicode string to an ascii one */ - fmt = _PyUnicode_AsString(format); - + /* Convert the unicode string to an ascii one */ + format = PyUnicode_AsEncodedString(format, TZNAME_ENCODING, NULL); + if (format == NULL) + return NULL; + fmt = PyBytes_AS_STRING(format); fmtlen = strlen(fmt); /* I hate these functions that presume you know how big the output @@ -519,6 +521,7 @@ for (i = 1024; ; i += i) { outbuf = (char *)PyMem_Malloc(i); if (outbuf == NULL) { + Py_DECREF(format); return PyErr_NoMemory(); } buflen = strftime(outbuf, i, fmt, &buf); @@ -532,6 +535,7 @@ ret = PyUnicode_Decode(outbuf, buflen, TZNAME_ENCODING, NULL); PyMem_Free(outbuf); + Py_DECREF(format); return ret; } PyMem_Free(outbuf); @@ -539,6 +543,7 @@ /* VisualStudio .NET 2005 does this properly */ if (buflen == 0 && errno == EINVAL) { PyErr_SetString(PyExc_ValueError, "Invalid format string"); + Py_DECREF(format); return 0; } #endif