Issue #5249: time.strftime returned malformed string when format string

contained non ascii character on windows.
This commit is contained in:
Hirokazu Yamamoto 2009-02-16 09:13:20 +00:00
parent debb98d91f
commit 575d133065
2 changed files with 11 additions and 3 deletions

View file

@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #5249: time.strftime returned malformed string when format string
contained non ascii character on windows.
- Issue #5186: Reduce hash collisions for objects with no __hash__ method by - Issue #5186: Reduce hash collisions for objects with no __hash__ method by
rotating the object pointer by 4 bits to the right. rotating the object pointer by 4 bits to the right.

View file

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