Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 575d133

Browse files
author
Hirokazu Yamamoto
committed
Issue #5249: time.strftime returned malformed string when format string
contained non ascii character on windows.
1 parent debb98d commit 575d133

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #5249: time.strftime returned malformed string when format string
16+
contained non ascii character on windows.
17+
1518
- Issue #5186: Reduce hash collisions for objects with no __hash__ method by
1619
rotating the object pointer by 4 bits to the right.
1720

Modules/timemodule.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,11 @@ time_strftime(PyObject *self, PyObject *args)
508508
return NULL;
509509
}
510510

511-
/* Convert the unicode string to an ascii one */
512-
fmt = _PyUnicode_AsString(format);
513-
511+
/* Convert the unicode string to an ascii one */
512+
format = PyUnicode_AsEncodedString(format, TZNAME_ENCODING, NULL);
513+
if (format == NULL)
514+
return NULL;
515+
fmt = PyBytes_AS_STRING(format);
514516
fmtlen = strlen(fmt);
515517

516518
/* I hate these functions that presume you know how big the output
@@ -519,6 +521,7 @@ time_strftime(PyObject *self, PyObject *args)
519521
for (i = 1024; ; i += i) {
520522
outbuf = (char *)PyMem_Malloc(i);
521523
if (outbuf == NULL) {
524+
Py_DECREF(format);
522525
return PyErr_NoMemory();
523526
}
524527
buflen = strftime(outbuf, i, fmt, &buf);
@@ -532,13 +535,15 @@ time_strftime(PyObject *self, PyObject *args)
532535
ret = PyUnicode_Decode(outbuf, buflen,
533536
TZNAME_ENCODING, NULL);
534537
PyMem_Free(outbuf);
538+
Py_DECREF(format);
535539
return ret;
536540
}
537541
PyMem_Free(outbuf);
538542
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
539543
/* VisualStudio .NET 2005 does this properly */
540544
if (buflen == 0 && errno == EINVAL) {
541545
PyErr_SetString(PyExc_ValueError, "Invalid format string");
546+
Py_DECREF(format);
542547
return 0;
543548
}
544549
#endif

0 commit comments

Comments
 (0)